1 Circular Reveal 圆形揭露动画概述
Android5.0是一个里程碑式的版本,不但提升了流畅度,更是增加了很多炫酷的效果:
Touch feedback(触摸反馈)
Reveal effect(揭露效果)
Activity transitions(Activity转换效果)
Curved motion(曲线运动)
View state changes (视图状态改变)
Animate Vector Drawables(可绘矢量动画)
今天主要讲解揭露动画。
RevealAnimator 揭露动画,这时Android5.0添加的新动画,动画效果为利用圆形缩放实现一个view的显示或者隐藏,最低支持的版本为api21,低版本使用会报异常,如果想要兼容api19以上的版本,可以自己实现这种缩放动画。
因为懒文章后面用到揭露动画的地方没有加版本限制,真实开发不能这么做。
RevealAnimator 是个隐藏的类所以在AndroidDeveloper上没有RevealAnimator 相关信息,所以只需要关注生成揭露动画的函数。
Circular Reveal 圆形揭露动画由ViewAnimationUtils.createCircularReveal生成。
public static Animator createCircularReveal(View view,
int centerX, int centerY, float startRadius, float endRadius)
参数说明:
view :要执行动画效果的View
centerX:圆心x坐标
centerY:圆心y坐标
startRadius:开始时的圆半径
endRadius:结束时的圆半径
返回值是一个Animator。
一般要显示把开始半径设置为0,结束设置响应值,如果要隐藏view,需要把开始半径设置为最大值,结束半径设置为0
重要特性:
揭露动画是一个异步动画,它的回调方法都不能保证在准确的时间里调用,但误差不会很大。
揭露对象要先于动画开始前显示(View.VISIBLE),因为动画开始时如果被操作对象处于隐藏状态,那么动画就不会有效果,所以就算是不可见的对象,开始动画前也需要设置为可见。
2 createCircularReveal的简单使用
首先要说明一点,看到api那么简单我直接应用到Activity中,但是换了很多个模拟器都无法出现揭露动画效果,最终利用 ActivityOptionsCompat.makeSceneTransitionAnimation和startActivity(intent,activityOptionsCompat.toBundle());启动的Activity才支持揭露动画。
2.1 简单使用,代码示例
XML文件:
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/targetView"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#ff00ff"
android:orientation="vertical"
android:gravity="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:textSize="20sp"
android:text="我是内容区域 我是内容区域
我是内容区域 我是内容区域
我是内容区域
我是内容区域
我是内容区域我是内容区域" />
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="开始动画"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
Activity文件
public class Main11Activity extends AppCompatActivity {
LinearLayout targetView ;
@Override
protected void onCreate(Bundle savedInstanceState) { <