活动和共享视图之间的animation:在animation结尾处出现毛病/黑客行为?

所以,我面临的问题是我在两个活动和两个共享视图之间做的animation看起来不太好。

问题是,当它从Activity2返回到Activity1时,它的“小问题”,TextViews在animation结束时被共享闪烁,在Activity2中显示“更大的文本”几分之一秒,所以它“闪烁”。

活动1(有三个项目的RecyclerView):

在这里输入图像说明

活动2(详情):

在这里输入图像说明

我在做animation的同时拍摄了屏幕。 当从Activity2返回到Activit2时,您可以看到最后的文字闪烁。 这个video(36MB,对不起,大小)显示:

https://drive.google.com/file/d/0B3wIZ9CS9Kj_a0MyVFlzX1YtY0E/view?usp=sharing

问题是:我做错了什么? 为什么这样闪烁? 我看到其他angular色的video,他们都非常顺利,很好。

我已经testing了不同types的转换(changeBounds,explode等等),但总是有一些奇怪的事情发生。 任何提示,想法将不胜感激=)

我的代码

MainActivity(Activity1):

package se.snapcode.lollipoptest; import android.app.Activity; import android.app.ActivityOptions; import android.content.Intent; import android.os.Bundle; import android.support.v4.view.GestureDetectorCompat; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.util.Pair; import android.view.GestureDetector; import android.view.Menu; import android.view.MenuItem; import android.support.v7.widget.RecyclerView; import android.view.MotionEvent; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private RecyclerView mRecyclerView; private MyAdapter mAdapter; private RecyclerView.LayoutManager mLayoutManager; GestureDetectorCompat gestureDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); // use this setting to improve performance if you know that changes // in content do not change the layout size of the RecyclerView mRecyclerView.setHasFixedSize(true); // use a linear layout manager mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); // specify an adapter (see also next example) String[] strings = new String[3]; strings[0] = "A1"; strings[1] = "A2"; strings[2] = "A3"; mAdapter = new MyAdapter(strings); mRecyclerView.setAdapter(mAdapter); mAdapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(View view, int position) { final TextView headerView = (TextView)view.findViewById(R.id.textView1); final TextView textView = (TextView)view.findViewById(R.id.textView2); Intent intent = new Intent(MainActivity.this, DetailsActivity.class); intent.putExtra("header", headerView.getText().toString()); intent.putExtra("text", textView.getText().toString()); ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this, Pair.create((View)headerView, "header"), Pair.create((View)textView, "text")); startActivity(intent, options.toBundle()); } }); RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST); mRecyclerView.addItemDecoration(itemDecoration); // this is the default; this call is actually only necessary with custom ItemAnimators mRecyclerView.setItemAnimator(new DefaultItemAnimator()); } } 

详细活动(Activity2):

 package se.snapcode.lollipoptest; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class DetailsActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_details); String header = getIntent().getStringExtra("header"); String text = getIntent().getStringExtra("text"); TextView tv1 = (TextView)findViewById(R.id.tv_details_header); tv1.setText(header); TextView tv2 = (TextView)findViewById(R.id.tv_details_text); tv2.setText(text); } } 

和布局,首先是在RecyclerView列表中的my_text_view:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:focusable="true" android:background="?android:attr/selectableItemBackground" android:colorControlHighlight="@android:color/holo_blue_light" android:padding="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="MY HEADER IS HERE" android:transitionName="header" android:id="@+id/textView1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="This is some text that is of relevance" android:transitionName="text" android:id="@+id/textView2" /> </LinearLayout> 

和activity_details.xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="se.snapcode.lollipoptest.DetailsActivity"> <TextView android:id="@+id/tv_details_header" android:text="A1" android:layout_width="wrap_content" android:transitionName="header" android:textSize="48dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_details_text" android:text="Some text of lesser importance" android:layout_width="wrap_content" android:textSize="24dp" android:transitionName="text" android:layout_height="wrap_content" /> </LinearLayout> 

和过渡的XML(in / res / transition):

 <?xml version="1.0" encoding="utf-8"?> <transitionSet xmlns:android="http://schemas.android.com/apk/res/android"> <changeBounds/> <explode /> </transitionSet> 

和styles.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="AppTheme" parent="android:Theme.Material.Light"> <!-- enable window content transitions --> <item name="android:windowContentTransitions">true</item> <!-- specify enter and exit transitions --> <item name="android:windowEnterTransition">@android:transition/slide_left</item> <item name="android:windowExitTransition">@android:transition/slide_right</item> <!-- specify shared element transitions --> <item name="android:windowSharedElementEnterTransition"> @transition/change_image_transform</item> <item name="android:windowSharedElementExitTransition"> @transition/change_image_transform</item> </style> </resources> 

Solutions Collecting From Web of "活动和共享视图之间的animation:在animation结尾处出现毛病/黑客行为?"

问题是,你正在尝试使用ChangeBounds转换将TextView的大小设置为共享元素。 这不会因为ChangeBounds工作方式ChangeBounds起作用。 ChangeBounds转换分析转换开始和结束时视图的布局边界以及两者之间的animation。 ChangeBounds适用于任意视图,所以在转换过程中它不会在你的TextView上调用setTextSize() …如果你希望看到TextView的大小无缝地增加,那么你需要使用自定义的转换/在animation中减less。 有一些关于如何在这个StackOverflow答案中做到这一点的信息。