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

ViewAnimationUtils.createCircularReveal()的简介:

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

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

异常。其源码如下:

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

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

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

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

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

实现思路:

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

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

shape 代码:

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

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
 
     <LinearLayout
         android:id="@+id/linearTestScale"
         android:orientation="vertical"
         android:layout_width="10dp"
         android:background="@drawable/a"
         android:layout_height="10dp">
 
         </LinearLayout>
     <Button
         android:id="@+id/btnTestScale"
         android:text="xxx"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         />
 </LinearLayout>

java代码:

 package com.LGH.ui.activity; 
 import android.animation.AnimatorSet;
 import android.animation.ObjectAnimator;
 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.animation.LinearInterpolator;
 import android.widget.Button;
 import android.widget.LinearLayout;
 
 import io.github.froger.instamaterial.R;
 
 /**
  * Created by Administrator on 2015/6/29.
  */
 public class test extends Activity{
 
     Button btnTestScale;
     LinearLayout linearTestScale;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.test);
         btnTestScale = (Button) findViewById(R.id.a);
         linearTestScale = (LinearLayout) findViewById(R.id.aaa);
         btnTestScale.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 ObjectAnimator revealAnimator = ObjectAnimator.ofFloat( //缩放X 轴的
                         linearTestScale, "scaleX", 0, 200);
                 ObjectAnimator revealAnimator1 = ObjectAnimator.ofFloat(//缩放Y 轴的
                         linearTestScale, "scaleY", 0, 200);
                 AnimatorSet set = new AnimatorSet();
                 set.setDuration(500);//设置播放时间
                 set.setInterpolator(new LinearInterpolator());//设置播放模式,这里是平常模式
                 set.playTogether(revealAnimator, revealAnimator1);//设置一起播放
                 set.start();
             }
         });
     }
 }
 

 
原文地址:https://www.cnblogs.com/zhujiabin/p/5279607.html