安卓使用动画启动Acitvity

news/2024/7/5 2:47:08

1.检查系统版本

动画过渡Activity适用于*Android5.0(API21)*及以上,在代码增加中检查版本增强代码健壮性。

2.指定自定义过渡动画。

过渡可以在xml文件中指定,也可以直接在代码中指定。使用Window.requestFeature()声明启动窗口过渡,这里需要注意的是该声明要在setContentView()方法之前调用不然会报错。使用过渡动画一共有4个方法:
1.Window.setEnterTransition()设置Activity进入时的动画。
2.Window.setExitTransition()设置Activity退出时的动画
3.Window.setSharedElementEnterTransition()设置具有共享元素的Activity进入时的动画。
4.Window.setSharedElementExitTransition()设置具有共享元素的Activity退出时的动画。
切换的Activity和当前Activity中没有共享元素的时候使用1,2指定过渡动画,切换的Activity和当前Activity中有共享元素的时候使用3,4指定过渡动画

3.启动使用过渡动画的Activity

startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
示例:直接利用系统自带的过渡效果
布局文件:

<?xml version="1.0" encoding="utf-8"?>
<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">

    <Button
            android:text="jump"
            android:id="@+id/btn_main"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    <ImageView
        android:id="@+id/img_main"
        android:src="@drawable/img_12"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6"
        android:scaleType="fitXY"/>
</LinearLayout>
    <!--Layout for MainActivity-->

第一个Activity的代码:

   private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){  //检查sdk版本是否大于等于5.0
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);  //通过getWindow()方法拿到window对象,设置启用过渡
           //getWindow().setExitTransition(new Explode());  // 第一个Activity退出时,第二个启动因为第二个设置了启动动画,所以这里退出时的动画不启用。
            getWindow().setEnterTransition(new Explode());
            getWindow().setAllowEnterTransitionOverlap(true);
        }
        setContentView(R.layout.main);
        btn = findViewById(R.id.btn_main);
        btn.setOnClickListener(this);  //设置点击事件
    }

    private void setTransition(Context context) {
            Intent intent = new Intent();
            intent.setClass(this, AnimationActivity.class);
            startActivity(intent,
                    ActivityOptions.makeSceneTransitionAnimation(this).toBundle());  //调用ActivityOptions.makeSceneTransitionAnimation()传入当前上下文
    }
}

第二个Activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        getWindow().setEnterTransition(new Explode());
        setContentView(R.layout.animation);
        findViewById(R.id.btn_ani).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(this, MainActivity.class);
        startActivity(intent,
                ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
    }

效果:result1

也可以只设置第一个Acticity的过渡动画,不过此时第二个Activity要调用 Activity.finishAfterTransition() 方法,而非 Activity.finish()方法。第一个Activity退出时的过渡动画启用。

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animation);
        findViewById(R.id.btn_ani).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(this, MainActivity.class);
        this.finishAfterTransition();
    }

效果:
在这里插入图片描述
如果您发现哪里有错,感谢指正!

上官鑫 原文链接


http://www.niftyadmin.cn/n/13608.html

相关文章

嫁给程序员老公,我后悔了

01 在我还没长开的时候&#xff0c;就常听人说&#xff0c;有两种男人不能嫁&#xff1a;一种是兵哥哥&#xff0c;另一种是程序员。前者见不着&#xff0c;后者死的早。 一想到不等头发花白&#xff0c;就要踟蹰独行&#xff0c;我就害怕的厉害。所以&#xff0c;很长一段时…

网页前端知识汇总(六)——如何让网页全部内容显示成灰色

最近很多做网站前端的技术员是不是都接到了老板的任务&#xff0c;让网站的网页显示效果都变成灰色&#xff0c;这个也是随某些事件的发生或者某些专题内容觉得需要这样做的&#xff0c;大部分用于大家都不愿意看到的专题事件如某某烈士&#xff0c;逝去的伟人等&#xff1b;大…

基础医学概论练习题(含答案)

基础医学概论练习题&#xff08;含答案&#xff09; 1. 人体最大、最复杂的关节是&#xff08; &#xff09; A.肩关节 B.肘关节 C.膝关节 D.腕关节 E.髋关节 2. 全身活动度最大的关节是&#xff08; &#xff09; A.肩关节 B.肘关节 C.膝关节 D.腕关节 …

VS2022开发Arduino(90%转载10%原创)

先上转载链接 VS2022开发Arduino&#xff08;提供Visual.Micro.Processing.Sketch.dll&#xff09;_hb2cpc的博客-CSDN博客_vs开发arduino Visual Studio 2022开发Arduino详述_liht_1634的博客-CSDN博客_visualstudio arduino 其中破解部分编译出错&#xff0c;此处为原创&am…

代码随想录刷题|买卖股票问题的总结

目录 总结 121.买卖股票的最佳时机 问题描述 特点分析 动态规划思路 122.买卖股票的最佳时机Ⅱ 问题描述 特点分析 动态规划思路 123.买卖股票的最佳时机III 问题描述 特点分析 动态规划思路 188.买卖股票的最佳时机IV 问题描述 特点分析 动态规划思路 309.最…

TPM零知识学习五 —— tpm2-abrmd源码安装

tpm2-abrmd包的的源码安装方法参考&#xff1a; tpm2-abrmd/INSTALL.md at master tpm2-software/tpm2-abrmd GitHub TPM模拟器和TPM2-TSS安装_jianming21的博客-CSDN博客_tpm2-tss 可信平台模块TPM&#xff08;Trusted Platform Module&#xff09;介绍及tpm-tools安装使…

Git(第一篇)——Git的下载与安装(史上最全最详细)

Git&#xff08;第一篇&#xff09;——Git的下载与安装&#xff08;史上最全最详细&#xff09; 目录Git&#xff08;第一篇&#xff09;——Git的下载与安装&#xff08;史上最全最详细&#xff09;git的下载git的安装git的下载 如果你还没有下载Git&#xff0c;可直接到git…

双软企业认定条件及需提交的材料

双软企业认证&#xff1a;指的是“软件产品登记”和“软件企业认证”&#xff0c;一般简称为双软企业认证。 软件产品登记 软件产品登记的条件 软件产品的登记首先应具备两个前提&#xff0c;即1&#xff09;取得本企业开发或拥有知识产权的软件产品的证明材料。指软件产品登记…