美文网首页
安卓转场动画、共享元素动画、Pair数据结构

安卓转场动画、共享元素动画、Pair数据结构

作者: it奔跑在路上 | 来源:发表于2020-02-28 17:20 被阅读0次

    需要注意的事,转场动画需要安卓系统大于等于5.0以上,可用

        Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
    

    进行判断

    动画效果如下:
    GIF.gif
    public 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>
    

    相关文章

      网友评论

          本文标题:安卓转场动画、共享元素动画、Pair数据结构

          本文链接:https://www.haomeiwen.com/subject/aurqhhtx.html