修复SharedElementTransition动画无法正常显示

时间:2015-07-03 13:59:38

标签: android animation android-recyclerview shared-element-transition android-5.1.1-lollipop

我正在尝试使用共享ImageView在两个活动之间创建共享元素过渡动画。

第一个有RecyclerView项目点击监听器,点击后,它应该开始转换的其他活动。

关闭已启动的活动时动画效果正常,但启动时却没有。

点击RecyclerView的项目后,它会稍微扩展,冻结一点,然后显示一个空白的黑色“窗口”,然后显示另一个ImageView,但它不能流畅地动画。

这是打开其他活动的代码:

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

1 个答案:

答案 0 :(得分:1)

好的我写了这个简短的程序来试验这个...我正在将textViews设为动画作为共享元素...而且它正在工作......这就是我所做的:

在我的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());
}

其他活动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。

将transitionSet xml添加到:res / transition / image_transition.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>