gpt4 book ai didi

android - 2 个 Activity 之间的 SharedElementTransition 动画不正确

转载 作者:行者123 更新时间:2023-11-29 00:05:06 25 4
gpt4 key购买 nike

我一直在努力让我的 sharedELementTransitions 工作得很好而且很顺利。但是我有一个问题,我的 ImageView 在返回动画上跳跃并且无法正确缩放。 Example我已经尝试尽可能多地复制现有的 android 项目,甚至是使用几乎相同的代码运行完美动画的项目,但似乎没有什么可以修复它。有没有人遇到过类似的问题?如果是这样,您是如何解决的?

最佳答案

检查这个解决方案是否适合我(退出和返回转换都有效)

主要 Activity .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>

第二个 Activity .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>

关于android - 2 个 Activity 之间的 SharedElementTransition 动画不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34448227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com