android 在使用ViewAnimationUtils.createCircularReveal()无法兼容低版本的情况下,另行实现圆形scale动画

ViewAnimationUtils.createCircularReveal()的简介:

      ViewAnimationUtils.createCircularReveal()是安卓5.0才引入的,快速实现圆形缩放动画的api,效果如下图所示:

       如果要在你的程序中使用它,必须要设置最低的 api 版本是 21,往下版本的,在运行程序的时候就会抛出 .createCircularReveal() not found

异常。其源码如下:

1 public static Animator createCircularReveal(View view,int centerX,  int centerY, float startRadius, float endRadius) {
2     return new RevealAnimator(view, centerX, centerY, startRadius, endRadius);
3 }

第一个参数view:是你要进行圆形缩放的 view;

第二和第三个参数:分别是开始缩放点的 x 和 y 坐标;

第四和第五:分别是开始的半径和结束的半径。

在兼容低版本下模仿实现上述效果:

      实现思路:

           1-》实现圆形,使用 xml 自定义背景,实现圆形,再设置到 view ;

           2-》使用传统的 scaleX 和 scaleY ,在所要缩放的 view 里同时实现缩放。

shape 代码:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <shape
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:shape="oval"
 5     android:useLevel="false">
 6     <!--oval是 shape的属性之一,意思是 椭圆-->
 7     <!--solid 是shape 的孩子之一,作用是实现填充-->
 8     <solid android:color="#ff49fdfa"/>
 9     <!--size 也是shape 的孩子之一,作用是实现 长宽限制-->
10     <size
11         android:width="300dp"
12         android:height="300dp" />
13 </shape>

布局代码:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6 
 7     <LinearLayout
 8         android:id="@+id/linearTestScale"
 9         android:orientation="vertical"
10         android:layout_width="10dp"
11         android:background="@drawable/a"
12         android:layout_height="10dp">
13 
14         </LinearLayout>
15     <Button
16         android:id="@+id/btnTestScale"
17         android:text="xxx"
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         />
21 </LinearLayout>

java代码:

 1 package com.LGH.ui.activity;
 2 
 3 import android.animation.AnimatorSet;
 4 import android.animation.ObjectAnimator;
 5 import android.app.Activity;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.view.animation.LinearInterpolator;
 9 import android.widget.Button;
10 import android.widget.LinearLayout;
11 
12 import io.github.froger.instamaterial.R;
13 
14 /**
15  * Created by Administrator on 2015/6/29.
16  */
17 public class test extends Activity{
18 
19     Button btnTestScale;
20     LinearLayout linearTestScale;
21 
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.test);
26         btnTestScale = (Button) findViewById(R.id.a);
27         linearTestScale = (LinearLayout) findViewById(R.id.aaa);
28         btnTestScale.setOnClickListener(new View.OnClickListener() {
29             @Override
30             public void onClick(View v) {
31                 ObjectAnimator revealAnimator = ObjectAnimator.ofFloat( //缩放X 轴的
32                         linearTestScale, "scaleX", 0, 200);
33                 ObjectAnimator revealAnimator1 = ObjectAnimator.ofFloat(//缩放Y 轴的
34                         linearTestScale, "scaleY", 0, 200);
35                 AnimatorSet set = new AnimatorSet();
36                 set.setDuration(500);//设置播放时间
37                 set.setInterpolator(new LinearInterpolator());//设置播放模式,这里是平常模式
38                 set.playTogether(revealAnimator, revealAnimator1);//设置一起播放
39                 set.start();
40             }
41         });
42     }
43 }

原文地址:https://www.cnblogs.com/linguanh/p/4610174.html