修复 SharedElementTransition 动画显示不佳
Fix SharedElementTransition animation not showing well
我正在尝试在具有共享 ImageView
的 2 个 Activity 之间创建共享元素过渡动画。
第一个有一个 RecyclerView
带有项目点击侦听器,点击后,它应该启动另一个 activity 与转换。
动画在关闭已启动的 activity 时运行正常,但在启动时却没有。
点击 RecyclerView
的 item 后,它扩大了一点,冻结了一点,然后显示了一个空白的黑色 "window" 然后是另一个 ImageView,但它没有动画流利。
这是打开另一个的代码activity:
private void openActivity(WallpapersAdapter.WallsHolder wallsHolder, int index, final HashMap<String, String> data) {
Toast.makeText(this,data.get(WallpapersActivity.WALL),Toast.LENGTH_SHORT).show();
final Intent intent = new Intent(wallsActivity, ViewerActivity.class);
intent.putExtra("wallUrl", data.get(WallpapersActivity.WALL));
intent.putExtra("wallName", data.get(WallpapersActivity.NAME));
String indextext = Integer.toString(index);
intent.putExtra("indexText", indextext);
String transName = "view_" + indextext;
ViewCompat.setTransitionName(wallsHolder.view, transName);
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
wallsActivity, wallsHolder.view, transName);
startActivity(intent, options.toBundle());
}
编辑:
这是初始视图的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:foreground="?selectableItemBackground">
<ProgressBar
android:id="@+id/progress"
style="?progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="24dp" />
<jahirfiquitiva.project.views.SquareImageView
android:id="@+id/wall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
tools:ignore="ContentDescription,UnusedAttribute" />
<LinearLayout
android:id="@+id/titlebg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
style="@style/AboutContent"
android:alpha="0.3"
android:background="#000000"
tools:ignore="UnusedAttribute">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center"
android:textColor="#ffffffff"
android:textSize="@dimen/abc_text_size_medium_material"
tools:text="Title" />
</LinearLayout>
</FrameLayout>
提前致谢。
编辑 2:
这是一段展示它的样子的视频:https://drive.google.com/file/d/0Bw52d3_ZiSb9YWc2Z1Z4MnkwNm8/view?usp=sharing
好的,我写了这个简短的程序来试验这个...我将 textViews 动画化为 sharedElements...它正在工作...这是我所做的:
在我的 RecyclerView 适配器中
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
ViewCompat.setTransitionName(((RecyclerViewHolder) holder).mTextView,"myTransition"+position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
mEventListener.clicked((RecyclerViewHolder) holder);
}
});
}
MainActivity 的点击事件从我开始另一个 Activity
@Override
public void clicked(final MyAdapter.RecyclerViewHolder holder) {
final Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra("TRANSITION_KEY", ViewCompat.getTransitionName(holder.mTextView));
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
this, holder.mTextView, ViewCompat.getTransitionName(holder.mTextView));
startActivity(intent, options.toBundle());
}
其他Activity的onCreate
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
TextView textView = (TextView) findViewById(R.id.textView2);
Intent intent = getIntent();
ViewCompat.setTransitionName(textView, intent.getStringExtra("TRANSITION_KEY"));
}
具体问题的解决方案
除了按照前面的说明操作外,删除 onClick 中的 Toast。
在 : res/transition/image_transition.xml
添加 transitionSet xml
<?xml version="1.0" encoding="utf-8"?>
<transitionSet>
<changeImageTransform/>
<changeBounds/>
</transitionSet>
从您的 styles.xml 中删除这些(如果已添加)
<item name="android:windowAnimationStyle">@style/WindowTransitionAnimation</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
<item name="android:windowEnterTransition">@android:transition/move</item>
<item name="android:windowExitTransition">@android:transition/move</item>
我正在尝试在具有共享 ImageView
的 2 个 Activity 之间创建共享元素过渡动画。
第一个有一个 RecyclerView
带有项目点击侦听器,点击后,它应该启动另一个 activity 与转换。
动画在关闭已启动的 activity 时运行正常,但在启动时却没有。
点击 RecyclerView
的 item 后,它扩大了一点,冻结了一点,然后显示了一个空白的黑色 "window" 然后是另一个 ImageView,但它没有动画流利。
这是打开另一个的代码activity:
private void openActivity(WallpapersAdapter.WallsHolder wallsHolder, int index, final HashMap<String, String> data) {
Toast.makeText(this,data.get(WallpapersActivity.WALL),Toast.LENGTH_SHORT).show();
final Intent intent = new Intent(wallsActivity, ViewerActivity.class);
intent.putExtra("wallUrl", data.get(WallpapersActivity.WALL));
intent.putExtra("wallName", data.get(WallpapersActivity.NAME));
String indextext = Integer.toString(index);
intent.putExtra("indexText", indextext);
String transName = "view_" + indextext;
ViewCompat.setTransitionName(wallsHolder.view, transName);
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
wallsActivity, wallsHolder.view, transName);
startActivity(intent, options.toBundle());
}
编辑: 这是初始视图的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:foreground="?selectableItemBackground">
<ProgressBar
android:id="@+id/progress"
style="?progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="24dp" />
<jahirfiquitiva.project.views.SquareImageView
android:id="@+id/wall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
tools:ignore="ContentDescription,UnusedAttribute" />
<LinearLayout
android:id="@+id/titlebg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
style="@style/AboutContent"
android:alpha="0.3"
android:background="#000000"
tools:ignore="UnusedAttribute">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center"
android:textColor="#ffffffff"
android:textSize="@dimen/abc_text_size_medium_material"
tools:text="Title" />
</LinearLayout>
</FrameLayout>
提前致谢。
编辑 2: 这是一段展示它的样子的视频:https://drive.google.com/file/d/0Bw52d3_ZiSb9YWc2Z1Z4MnkwNm8/view?usp=sharing
好的,我写了这个简短的程序来试验这个...我将 textViews 动画化为 sharedElements...它正在工作...这是我所做的:
在我的 RecyclerView 适配器中
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
ViewCompat.setTransitionName(((RecyclerViewHolder) holder).mTextView,"myTransition"+position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
mEventListener.clicked((RecyclerViewHolder) holder);
}
});
}
MainActivity 的点击事件从我开始另一个 Activity
@Override
public void clicked(final MyAdapter.RecyclerViewHolder holder) {
final Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra("TRANSITION_KEY", ViewCompat.getTransitionName(holder.mTextView));
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
this, holder.mTextView, ViewCompat.getTransitionName(holder.mTextView));
startActivity(intent, options.toBundle());
}
其他Activity的onCreate
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
TextView textView = (TextView) findViewById(R.id.textView2);
Intent intent = getIntent();
ViewCompat.setTransitionName(textView, intent.getStringExtra("TRANSITION_KEY"));
}
具体问题的解决方案
除了按照前面的说明操作外,删除 onClick 中的 Toast。
在 : res/transition/image_transition.xml
添加 transitionSet xml<?xml version="1.0" encoding="utf-8"?>
<transitionSet>
<changeImageTransform/>
<changeBounds/>
</transitionSet>
从您的 styles.xml 中删除这些(如果已添加)
<item name="android:windowAnimationStyle">@style/WindowTransitionAnimation</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
<item name="android:windowEnterTransition">@android:transition/move</item>
<item name="android:windowExitTransition">@android:transition/move</item>