Android之旅

Android动画·Property之ValueAnimator

2017-11-16  本文已影响1人  Android师哥
night_rain.png

属性动画是一种不断地对值进行操作的机制,并将值赋值到指定对象的指定属性上,可以是任意对象的任意属性。所以我们仍然可以将一个View进行移动或者缩放,但同时也可以对自定义View中的Point对象进行动画操作了。我们只需要告诉系统动画的运行时长,需要执行哪种类型的动画,以及动画的初始值和结束值,剩下的工作就可以全部交给系统去完成了

说白了就是属性动画产生一系列的值,然后不断的改变控件本身的属性,来达到一个动画的效果,接下来看看ValueAnimator!
先看一张动图


222.gif
    private void propertyInt() {
        int left = btn_main_1.getLeft();
        int right = btn_main_1.getRight();
        //获取按钮的宽度
        int viewWidth = right - left;
        DisplayMetrics mDisplayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
        //获取屏幕的宽度
        int animViewWdith = mDisplayMetrics.widthPixels;
        //创建动画(int)
        ValueAnimator mValueAnimatorInt = ValueAnimator.ofInt(viewWidth, animViewWdith, viewWidth);
        //设置动画执行时间
        mValueAnimatorInt.setDuration(1500);
        //添加动画数值监听器
        mValueAnimatorInt.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                btn_main_1.getLayoutParams().width = value;
                btn_main_1.requestLayout();
            }
        });
        //添加动画状态监听器
        mValueAnimatorInt.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                btn_main_1.setEnabled(false);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                btn_main_1.setEnabled(true);
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        //开启动画
        mValueAnimatorInt.start();
    }
private void propertyFloat() {
        //创建动画(float)
        ValueAnimator mValueAnimatorFloat = ValueAnimator.ofFloat(1.0f, 0.0f, 1.0f);
        //动画持续时间
        mValueAnimatorFloat.setDuration(3000);
        //动画数值监听
        mValueAnimatorFloat.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float value = (float) valueAnimator.getAnimatedValue();
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                attributes.alpha = value;
                getWindow().setAttributes(attributes);
            }
        });
        //动画状态监听
        mValueAnimatorFloat.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                btn_main_2.setEnabled(false);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                btn_main_2.setEnabled(true);
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        //开启动画
        mValueAnimatorFloat.start();
    }
private void propertyObject() {
        //左侧按钮的位置
        int leftTop = btn_test_1.getTop();
        int leftButtom = btn_test_1.getBottom();
        int leftLeft = btn_test_1.getLeft();
        int leftRight = btn_test_1.getRight();
        //右侧按钮的位置
        int rightTop = btn_test_2.getTop();
        int rightButtom = btn_test_2.getBottom();
        int rightLeft = btn_test_2.getLeft();
        int rightRight = btn_test_2.getRight();
        //开始时俩个按钮的位置
        AddressInfo mLeft = new AddressInfo(leftTop, leftButtom, leftLeft, leftRight, rightTop, rightButtom, rightLeft, rightRight);
        //结束时俩个按钮的位置
        AddressInfo mRight = new AddressInfo(rightTop, rightButtom, rightLeft, rightRight, leftTop, leftButtom, leftLeft, leftRight);
        btn1(mLeft, mRight);
    }

动画

 private void btn1(AddressInfo mLeft, AddressInfo mRight) {
        //创建动画(参数1:数值计算器
        //         参数2:开始状态的对象
        //         参数3:结束状态的对象)
        ValueAnimator mObjValueAnimator = ValueAnimator.ofObject(new MyTypeEvaluator(), mLeft, mRight);
        //动画执行时间
        mObjValueAnimator.setDuration(3000);
        //动画数值监听
        mObjValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                AddressInfo mAddressInfo = (AddressInfo) valueAnimator.getAnimatedValue();
                //设置左侧的按钮位置
                btn_test_1.layout(mAddressInfo.getLeftLeft(), mAddressInfo.getTopLeft(), mAddressInfo.getRightLeft(), mAddressInfo.getButtomLeft());
                //设置右侧的按钮位置
                btn_test_2.layout(mAddressInfo.getLeftRight(), mAddressInfo.getTopRight(), mAddressInfo.getRightRight(), mAddressInfo.getButtomRight());
            }
        });
        //动画状态监听
        mObjValueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                btn_main_3.setEnabled(false);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                btn_main_3.setEnabled(true);
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        //开启动画
        mObjValueAnimator.start();
    }

TypeEvaluator

/**
     * 计算数值
     */
    private class MyTypeEvaluator implements TypeEvaluator<AddressInfo> {
        @Override
        public AddressInfo evaluate(float v, AddressInfo addressInfo, AddressInfo t1) {
            int topLeft = (int) (addressInfo.getTopLeft() + (v * (t1.getTopLeft() - addressInfo.getTopLeft())));
            int buttomLeft = (int) (addressInfo.getButtomLeft() + (v * (t1.getButtomLeft() - addressInfo.getButtomLeft())));
            int leftLeft = (int) (addressInfo.getLeftLeft() + (v * (t1.getLeftLeft() - addressInfo.getLeftLeft())));
            int rightLeft = (int) (addressInfo.getRightLeft() + (v * (t1.getRightLeft() - addressInfo.getRightLeft())));
            int topRight = (int) (addressInfo.getTopRight() + (v * (t1.getTopRight() - addressInfo.getTopRight())));
            int buttomRight = (int) (addressInfo.getButtomRight() + (v * (t1.getButtomRight() - addressInfo.getButtomRight())));
            int leftRight = (int) (addressInfo.getLeftRight() + (v * (t1.getLeftRight() - addressInfo.getLeftRight())));
            int rightRight = (int) (addressInfo.getRightRight() + (v * (t1.getRightRight() - addressInfo.getRightRight())));
            AddressInfo mAddressInfo = new AddressInfo(topLeft, buttomLeft, leftLeft, rightLeft, topRight, buttomRight, leftRight, rightRight);
            return mAddressInfo;
        }
    }

Entity

public class AddressInfo {
    private int topLeft;
    private int buttomLeft;
    private int leftLeft;
    private int rightLeft;

    private int topRight;
    private int buttomRight;
    private int leftRight;
    private int rightRight;

    public AddressInfo(int topLeft, int buttomLeft, int leftLeft, int rightLeft, int topRight, int buttomRight, int leftRight, int rightRight) {
        this.topLeft = topLeft;
        this.rightRight = rightRight;
        this.leftRight = leftRight;
        this.buttomRight = buttomRight;
        this.topRight = topRight;
        this.rightLeft = rightLeft;
        this.leftLeft = leftLeft;
        this.buttomLeft = buttomLeft;
    }

    public int getTopLeft() {
        return topLeft;
    }

    public int getLeftRight() {
        return leftRight;
    }

    public int getButtomLeft() {
        return buttomLeft;
    }

    public int getLeftLeft() {
        return leftLeft;
    }

    public int getRightLeft() {
        return rightLeft;
    }

    public int getTopRight() {
        return topRight;
    }

    public int getButtomRight() {
        return buttomRight;
    }

    public int getRightRight() {
        return rightRight;
    }
}

不足之处还请指出,一起学习!

上一篇下一篇

猜你喜欢

热点阅读