我一直在努力让我的 sharedELementTransitions 工作得又好又顺畅。但是我有一个问题,我的 ImageView 跳跃并且在返回动画上不能正确缩放。示例我尝试尽可能多地复制现有的 android 项目,即使是运行完美动画且代码几乎相同的项目,但似乎没有任何解决办法。有没有人遇到过类似的问题?如果是这样,你是如何解决的?
问问题
114 次
1 回答
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>
于 2015-12-24T06:48:07.097 回答