android activity跳转动画_叠化转场是什么意思

android activity跳转动画_叠化转场是什么意思Android Reveal圆形Activity转场动画

大家好,又见面了,我是你们的朋友全栈君。

一、效果

二、知识点

CircularReveal动画、透明主题、转场动画(非必须)

三、方案

假设有两个Activity A和B。Reveal圆形Activity转场动画效果先从A到B,那么基本方案如下:

  1. 确定要显示的圆形动画中心起点位置
  2. 通过Intent将起点位置从Activity A传递B
  3. Activity B主题需要是透明的,同时先隐藏布局视图
  4. 在Activity A中启动Activity B,Activity A先不销毁
  5. Activity B启动之后开始动画,在动画启动时显布局视图
  6. 销毁Activity A,如果需要返回则不销毁

四、实现

4.1 初始界面Activity A

在Activity A中需要定义好主题、布局以及启动Activity B的方法。因为当不需要执行返回动画的时候,要把Activity A销毁,这时候一定是在后台销毁的,所以要把主题相关设置为透明,不然会在Activity B中显示Activity A销毁界面。

<style name="FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>
复制代码

然后是布局设置,这一步比较简单,这里以启动界面为例,显示一张铺满全屏的图片,下面覆盖一个进度条。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".SplashActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@mipmap/wallace" />

    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_marginBottom="180dp" />

</FrameLayout>
复制代码

在Activity A中启动Activity B代码如下,使用转场动画API执行,当然也可以使用ActivityCompat.startActivity(this, intent, null); overridePendingTransition(0, 0);这种方式。在这段代码中,把Activity A中开始执行Reveal圆形动画的坐标点传递给Activity B,因为动画是在Activity B中执行的。

public void presentActivity(View view) {
    ActivityOptionsCompat options = ActivityOptionsCompat.
            makeSceneTransitionAnimation(this, view, "transition");
    int revealX = (int) (view.getX() + view.getWidth() / 2);
    int revealY = (int) (view.getY() + view.getHeight() / 2);

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra(MainActivity.EXTRA_CIRCULAR_REVEAL_X, revealX);
    intent.putExtra(MainActivity.EXTRA_CIRCULAR_REVEAL_Y, revealY);

    ActivityCompat.startActivity(this, intent, options.toBundle());

    //ActivityCompat.startActivity(this, intent, null);  overridePendingTransition(0, 0);
}
复制代码
4.2 动画界面Activity B

在Activity B中同样需要定义好主题、布局以及执行动画的方法。上面方案中也说到,Activity B需要是透明主题,而且布局文件不能为透明,随便设置一个背景即可。因为动画效果是从Activity A过度到Activity B,也就是启动Activity B一切准备就绪之后,显示其布局。同时开始执行ViewAnimationUtils.createCircularReveal动画,createCircularReveal会把根布局慢慢展开。这样就形成了上面的动画效果。

主题设置如下:

<style name="AppTheme.Transparent" parent="AppTheme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>
复制代码

布局设置如下,注意根布局背景设置:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_dark"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
复制代码

最后就是执行动画的代码,先把根据不设置为不可见,然后在跟布局测量完毕之后开始执行动画。

if (savedInstanceState == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
        intent.hasExtra(EXTRA_CIRCULAR_REVEAL_X) &&
        intent.hasExtra(EXTRA_CIRCULAR_REVEAL_Y)) {
    rootLayout.setVisibility(View.INVISIBLE);
    revealX = intent.getIntExtra(EXTRA_CIRCULAR_REVEAL_X, 0);
    revealY = intent.getIntExtra(EXTRA_CIRCULAR_REVEAL_Y, 0);
    ViewTreeObserver viewTreeObserver = rootLayout.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                revealActivity(revealX, revealY);
                rootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }
} else {
    rootLayout.setVisibility(View.VISIBLE);
}
 
复制代码
protected void revealActivity(int x, int y) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        float finalRadius = (float) (Math.max(rootLayout.getWidth(), rootLayout.getHeight()) * 1.1);
        // create the animator for this view (the start radius is zero) 
        Animator circularReveal = ViewAnimationUtils.createCircularReveal(rootLayout, x, y, 0, finalRadius);
        circularReveal.setDuration(400);
        circularReveal.setInterpolator(new AccelerateInterpolator());
        // make the view visible and start the animation 
        rootLayout.setVisibility(View.VISIBLE);
        circularReveal.start();
    } else {
        finish();
    }
}
复制代码

最后实现效果如下:

代码地址:CircularRevealActivity:github.com/Geekince/An…

五、参考:


安卓开发交流QQ群:410079135

转载于:https://juejin.im/post/5bdc5615e51d4543fd7c9c8d

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107217.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • managementobjectsearch_beanpropertyrowmapper

    managementobjectsearch_beanpropertyrowmapperclassWin32_Service:Win32_BaseService{ booleanAcceptPause; booleanAcceptStop; stringCaption; uint32CheckPoint; stringCreationClassName; stringDescription; booleanDesktopInteract; st

    2022年10月2日
    4
  • git clone时出现的两种报错及解决办法[通俗易懂]

    git clone时出现的两种报错及解决办法[通俗易懂]参考:https://blog.csdn.net/u010887744/article/details/53957613 https://blog.csdn.net/wpyily/article/details/48130515第一种报错:fatal:HTTPrequestfailed解决一: 执行#gitconfig–globalhttp.sslVerifyfalse解决二:由于…

    2022年7月21日
    82
  • 大学计算机系最努力的同学都是如何学习的?

    经常会被同学们问到这个问题,要怎么努力才能找到好工作?学习好就能进好公司?屁!被学校教育坑的一把鼻涕一把泪的老学长来回答一波。我上大学时,连续三年得过国家励志奖学金,英语过六级,以为软考有用,还考了个软件设计师证书,以为四级有用,也考了个软件测试工程师证书,看人家用c++写软件,自学MFC写了几个桌面小程序。自以为很牛逼的去找工作,结果要价2500,都没人收,2011年,一个计算机系top级毕业生,薪资还没流水线的高。现在工作近十年,也辗转几个大厂,做校招、社招面试也不下于上百场,这

    2022年4月11日
    51
  • Nginx服务器安装阿里云SSL证书教程[通俗易懂]

    Nginx服务器安装阿里云SSL证书教程[通俗易懂]Nginx配置SSL证书,大致分为5个步骤:步骤1:申请一张测试试用证书步骤2:下载证书并上传到服务器步骤3:在服务器配置证书步骤4(可选):配置HTTP强制跳转HTTPS步骤54:开放HTTPS访问及验证下面开始我们的配置步骤1:申请一张测试试用证书打开阿里云控制台官网,搜索SSL,点击进入SSL证书(应用安全)选择左侧SSL证书,点击免费证书点击立即购买,会弹出右侧购买栏,选择DV单域名证书【免费试用】,点击下方购买由于该证书限制只能有一个,所以接

    2022年10月3日
    4
  • CreatePipe匿名管道通信

    CreatePipe匿名管道通信管道(Pipe)实际是用于进程间通信的一段共享内存,创建管道的进程称为管道服务器,连接到一个管道的进程为管道客户机。一个进程在向管道写入数据后,另一进程就可以从管道的另一端将其读取出来。匿名管道(AnonymousPipes)是在父进程和子进程间单向传输数据的一种未命名的管道,只能在本地计算机中使用,而不可用于网络间的通信。      匿名管道实施细则      匿名管道由Cre

    2022年7月26日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号