2个活动之间的SharedElementTransition没有正确动画

时间:2015-12-24 06:19:36

标签: android shared-element-transition

我一直在努力尝试将我的sharedELementTransitions工作得很顺利。但我有一个问题,我的ImageView跳转,并没有在返回动画上正确缩放。 Example我尝试尽可能多地复制现有的Android项目,甚至是使用几乎相同的代码运行完美动画的项目,似乎没有什么能解决它。有没有人有类似的问题?如果是这样,你是如何解决它的?

1 个答案:

答案 0 :(得分:2)

检查此解决方案是否适合我(退出和返回转换都正常)

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ListView listview = (ListView) findViewById(R.id.listView);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            ImageView imageView = (ImageView) view.findViewById(R.id.imageListItem);
            TextView textView = (TextView) view.findViewById(R.id.DummyText);

            Intent intent = new Intent(MainActivity.this, SecondActivity.class);

            Pair<View, String> p1 = Pair.create((View) textView, "newTrans");
            Pair<View, String> p2 = Pair.create((View) imageView, "profileAnimation");

            ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, p1, p2);
            startActivity(intent, optionsCompat.toBundle());

        }
    });

    listview.setAdapter(new DummyAdapter());

}

private class DummyAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return 20;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.list_item, parent, false);
        }
        return convertView;

    }
}

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout         xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.hb.androidlanimations.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@color/colorPrimary"
    android:dividerHeight="2dp"></ListView>

 </android.support.design.widget.CoordinatorLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">

<ImageView
    android:id="@+id/imageListItem"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:background="@drawable/ic_launcher"
    android:transitionName="profileAnimation" />

<TextView
    android:id="@+id/DummyText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="DummyText"
    android:transitionName="newTrans" />

 </RelativeLayout>

SecondActivity.java

public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
}
}

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:paddingTop="30dp">

<ImageView
    android:id="@+id/imgMain"
    android:layout_width="125dp"
    android:layout_height="125dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="@drawable/ic_launcher"
    android:transitionName="profileAnimation" />

<TextView
    android:id="@+id/DummyActivity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Dummy Text"
    android:transitionName="newTrans" />

    </RelativeLayout>

在你的风格中使用这个项目:

  <item name="android:windowContentTransitions">true</item>