Android Reveal圆形Activity转场动画

简介: 一、效果 图片较大无法上传~~ 地址:https://user-gold-cdn.xitu.io/2018/11/2/166d4b91aecdf577?imageslim 二、知识点 CircularReveal动画、透明主题、转场动画(非必须) 三、方案 假设有两个Activity A和B。
一、效果

图片较大无法上传~~

地址:https://user-gold-cdn.xitu.io/2018/11/2/166d4b91aecdf577?imageslim

二、知识点

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:https://link.juejin.im/?target=https%3A%2F%2Fgithub.com%2FGeekince%2FAndevUI%2Ftree%2Fmaster%2FCircularRevealActivity

五、参考:
●  https://link.juejin.im/?target=https%3A%2F%2Fandroid.jlelse.eu%2Fa-little-thing-that-matter-how-to-reveal-an-activity-with-circular-revelation-d94f9bfcae28
●  https://link.juejin.im/?target=https%3A%2F%2Fcodesnipps.simolation.com%2Fpost%2Fandroid%2Fcreate-circular-reveal-animation-when-starting-activitys%2F


原文发布时间为:2018-11-07

本文作者:不二金宇

本文来自云栖社区合作伙伴“Android开发中文站”,了解相关信息可以关注“Android开发中文站”。


相关文章
|
3月前
|
Android开发 开发者
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
41 1
|
3月前
|
数据库 Android开发 开发者
Android基础知识:请解释Activity的生命周期。
Android基础知识:请解释Activity的生命周期。
39 2
|
2月前
|
Android开发
[Android 四大组件] --- Activity
[Android 四大组件] --- Activity
22 1
|
3月前
|
Android开发
Android基础知识:什么是Fragment?与Activity的区别是什么?
Android基础知识:什么是Fragment?与Activity的区别是什么?
270 54
|
3月前
|
XML 开发工具 Android开发
Android动画效果-更新中
Android动画效果-更新中
59 1
|
4月前
|
XML Android开发 数据格式
[Android]动画
[Android]动画
31 0
|
4月前
|
XML Java Android开发
Android App开发手机阅读中实现平滑翻书效果和卷曲翻书动画实战(附源码 简单易懂 可直接使用)
Android App开发手机阅读中实现平滑翻书效果和卷曲翻书动画实战(附源码 简单易懂 可直接使用)
65 0
|
4月前
|
XML Java Android开发
Android App开发手机阅读中贝塞尔曲线的原理讲解及实现波浪起伏动画实战(附源码和演示视频 可直接使用)
Android App开发手机阅读中贝塞尔曲线的原理讲解及实现波浪起伏动画实战(附源码和演示视频 可直接使用)
44 0
|
4月前
|
XML Java Android开发
Android App开发实战项目之仿手机QQ动感影集动画播放(附源码和演示视频 可直接使用)
Android App开发实战项目之仿手机QQ动感影集动画播放(附源码和演示视频 可直接使用)
28 0
|
4月前
|
XML Java Android开发
Android App开发动画特效之实现百叶窗动画和马赛克动画效果实战演示(附源码和演示视频 可直接使用)
Android App开发动画特效之实现百叶窗动画和马赛克动画效果实战演示(附源码和演示视频 可直接使用)
59 0