安卓转场动画、共享元素动画、Pair数据结构
2020-02-28 本文已影响0人
it奔跑在路上
需要注意的事,转场动画需要安卓系统大于等于5.0以上,可用
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
进行判断
动画效果如下:
GIF.gifpublic class MainActivity extends AppCompatActivity {
private ImageView imageView;
private String url = "https://travel.12306.cn/imgs/resources/uploadfiles/images/a9b9c76d-36ba-4e4a-8e02-9e6a1a991da0_news_W540_H300.jpg";
private String elementName = "elementName";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.iv);
Glide.with(this).load(url).into(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
intent.putExtra("urls", url);
intent.putExtra("elementName", elementName);
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, imageView, elementName);
startActivity(intent, optionsCompat.toBundle());
}
});
}
}
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:layout_width="match_parent"
android:text="安卓转场动画、共享元素动画、Pair数据结构"
android:gravity="center"
android:layout_height="match_parent" />
</LinearLayout>
public class AnotherActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
ImageView imageView = (ImageView) findViewById(R.id.iv);
// 传递图片的链接
String url = getIntent().getStringExtra("urls");
// 接收共享元素动画参数
String elementName = getIntent().getStringExtra("elementName");
// 设置共享元素动画参数
imageView.setTransitionName(elementName);
Glide.with(this).load(url).into(imageView);
}
}
<LinearLayout 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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:id="@+id/iv"
android:layout_height="200dp" />
<TextView
android:layout_width="match_parent"
android:text="我是Another界面"
android:gravity="center"
android:layout_height="match_parent" />
</LinearLayout>