Android动画详解(三)

我们日常开发中,很多情况下都是在系统原有的View上进行Animator操作的;在Android 3.1之后,开发团队新增了ViewPropertyAnimator类,它是专门针对于View而设计的动画类。于ObjectAnimator/ValueAnimator相比较,它使用及其的简单。

1.使用方式

  1. ObjectAnimator
//透明度
        final ObjectAnimator alphaObjectAnimator = ObjectAnimator
                .ofFloat(imageView,"alpha",1f,0f,1f);
        //旋转
        final ObjectAnimator rotateObjectAnimator = ObjectAnimator
                .ofFloat(imageView,"rotation",0f,360f);
        //缩放
        final ObjectAnimator scaleXObjectAnimator = ObjectAnimator
                .ofFloat(imageView,"scaleX",1f,2f,1f);
        final ObjectAnimator scaleYObjectAnimator = ObjectAnimator
                .ofFloat(imageView,"scaleY",1f,2f,1f);
        float translateY = imageView.getTranslationY();
        Log.e("wdl", "translateY: "+translateY);
        final ObjectAnimator translateYObjectAnimator = ObjectAnimator.
                ofFloat(imageView,"translationY",
                translateY,-600f,translateY);
  1. ViewPropertyAnimator
    实现透明度从1-0-1,旋转360,X轴放大2倍,添加加减速插值器,时长5s.
 imageView.animate().alpha(1).alpha(0).alphaBy(1)
                .rotation(360)
                .setInterpolator(new AccelerateDecelerateInterpolator())
                .scaleX(2)
                .rotation(5000);

通过观察源码可以知道,view.animator()获取ViewPropertyAnimator对象,内部自带animator.start()。使用链式调用即简洁又方便。

 /**
     * This method returns a ViewPropertyAnimator object, which can be used to animate
     * specific properties on this View.
     *
     * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
     */
    public ViewPropertyAnimator animate() {
        if (mAnimator == null) {
            mAnimator = new ViewPropertyAnimator(this);
        }
        return mAnimator;
    }

下面为ViewPropertyAnimator的一些常用属性做一些归纳。。引用一张大佬整理好的图。

使用了大佬归纳好的图,原文链接https://hencoder.com/ui-1-6/;谢谢大佬

Android动画详解(三)

后面不带有By的意味从原本的值增加至/增加到括号中的值。
后面带有By的意味从原本的值增加/增到括号中的值。

本文参考:
https://hencoder.com/ui-1-6/
https://blog.****.net/guolin_blog/article/details/44171115
https://wx.zsxq.com/dweb/#