ViewAnimationUtils初体验

关于ViewAnimationUtils的使用

viewAnimationUtils是Android5.0之后出现的新的动画api。目前为止里面只有一个方法:createCircularReveal()。

这里写图片描述

其中:
View 代表 操作的控件
centerX和centerY 代表 动画开始的横纵坐标
startRadius和endRadius 代表 动画初始的波纹半径和结束的波纹半径
返回的是一个 Animator 对象

方法很简单,我们来看看效果
这里写图片描述

实现思路:
1.使用自定义view实现,首先获取到手指触碰手机的坐标,在onTouchEvent()方法里面就可以拿到:

@Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                float x = event.getX();
                float y = event.getY();

                break;


        }

        return super.onTouchEvent(event);
    }

2.测量控件的长度和高度,计算出波纹最大的半径,可以在OnMessure方法里面得到:

 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        width = getMeasuredWidth();
        height = getMeasuredHeight();
        radius = (float) Math.hypot(width,height);
    }

3.开始动画,有两种动画 扩散动画和收缩动画。
扩散动画:

 Animator circularReveal = ViewAnimationUtils.createCircularReveal(this,
                        x, y, 0, radius);

                circularReveal.setDuration(1000);
                circularReveal.start();

收缩动画:

 Animator circularReveal = ViewAnimationUtils.createCircularReveal(this,
                        x, y, radius, 0);


                circularReveal.setDuration(1000);
                circularReveal.start();

主要代码就是这些,是不是很简单,还有一些小细节:
1.设置图片状态标识,用于判断什么时候该用哪种动画。动画结束时设置透明度来隐藏或显示图片。
2.设置加载状态标识,动画正在播放时,不响应新的动画。

最后贴上全部代码:


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewAnimationUtils;


/**
 * Created by XieMinFeng.
 * Date: 2017-07-04
 * Time: 14:19
 * Desc:
 */

public class AnimView extends View {

    private float width;
    private float height;

    private float radius;

    //标识 判断图片是否显示
    boolean isShow = true;
    //标识 判断图片是否正在执行动画
    boolean isAniming = false;

    public AnimView(Context context) {
        super(context);
    }

    public AnimView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AnimView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        width = getMeasuredWidth();
        height = getMeasuredHeight();
        radius = (float) Math.hypot(width,height);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                float x = event.getX();
                float y = event.getY();

                if (!isAniming) {
                    showAnim((int) x, (int) y);
                }
                break;


        }

        return super.onTouchEvent(event);
    }


    void showAnim(int x, int y) {

        isAniming = true;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            if (isShow) { //要隐藏

                Animator circularReveal = ViewAnimationUtils.createCircularReveal(this,
                        x, y, radius, 0);


                circularReveal.setDuration(1000);
                circularReveal.start();

                circularReveal.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);


                        setAlpha(0);
                        isShow = false;
                        isAniming = false;

                    }
                });


            } else {  //要显示


                isShow = true;
                setAlpha(1);


                Animator circularReveal = ViewAnimationUtils.createCircularReveal(this,
                        x, y, 0, radius);

                circularReveal.setDuration(1000);
                circularReveal.start();

                circularReveal.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        isAniming = false;
                    }
                });

            }

        }

    }
}

使用很简单,直接在xml文件使用即可

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <com.benhuan.view.AnimView
        android:background="@drawable/meizhi"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值