Android动画详解(三)

本文深入探讨Android中的ViewPropertyAnimator类,对比ObjectAnimator,介绍其简化动画操作的优势。通过实例展示如何实现透明度、旋转、缩放等动画效果,并解析其内部机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们日常开发中,很多情况下都是在系统原有的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/;谢谢大佬

在这里插入图片描述

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

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值