关于ObjectAnimator

基本使用

java 复制代码
    ObjectAnimator animator = ObjectAnimator.ofFloat(view,"alpha",1,0,1);
    animator.start();

关键点在于第二个参数:String propertyName 如果需要使用这个参数对应的效果,需要操作的view中有对应的set属性方法(set 函数的命名必须采用骆驼拼写法)。 例如:alpha 对应 setAlpha()方法

引用自《Android自定义控件开发入门与实战》

所以ValueAnimator 一般在回调中操作,而ObjectAnimator方便在自定义view中调用相应的方法。

关于ofObject()的使用

ofObject()可以帮助我们处理的值不限于int或者float,我们可以传自定义的值进行处理,如对象。

使用实例:

java 复制代码
ObjectAnimator animator = ObjectAnimator.ofObject(view,"jump",new JumpEvaluator(),new Point(0,0),new Point(30,30));
animator.setDuration(299);
animator.start();
java 复制代码
public class JumpEvaluator implements TypeEvalutor<Point>{
    private Point point = new Point();
   
    @Override
    public Point evaluate(float fraction, Point startValue, Point endValue) { 
        //自己的逻辑
        //point.x = (int)(startValue.x + fraction *(endValue.x - startValue.x));
        //if (fraction*2<=1){ 
        //    point.y = (int)(startValue.y + fraction*2*(endValue.y - startValue.y));
        // }else { 
        //    point.y = endValue.y; 
        //} 
    return point;
}

注意

对于ofFloat(),ofInt()最后一个可变参数,如果只传一个值,因为对应的get方法,而float,int 对应默认值为0,所以系统会报警告但不至于崩溃,而ofObject()对应的属性,没有默认的get方法,所以单个参数会崩溃

其他

void setRepeatCount(int value) //设置循环次数,设置为 INFINITE 表示无限循环

相关推荐
·云扬·1 小时前
MySQL Binlog三种记录格式详解
android·数据库·mysql
月明泉清2 小时前
Android中对于点击事件的深度梳理(二)
android
遇见火星2 小时前
Linux 服务可用性监控实战:端口、进程、接口怎么监控?
android·linux·运维
njsgcs2 小时前
基于memos和agentscope的ai工具和记忆调用助手
android
特立独行的猫a3 小时前
从XML到Compose的UI变革:现代(2026)Android开发指南
android·xml·ui·compose·jetpack
xiangxiongfly9153 小时前
Android 共享元素转场效果
android·动画·共享元素转场效果
我是阿亮啊3 小时前
Android 中线程和进程详解
android·线程·进程·进程间通信
我命由我123454 小时前
Android 开发问题:Duplicate class android.support.v4.app.INotificationSideChannel...
android·java·开发语言·java-ee·android studio·android-studio·android runtime
似霰4 小时前
Android 平台智能指针使用与分析
android·c++
有位神秘人4 小时前
Android中BottomSheetDialog的折叠、半展开、底部固定按钮等方案实现
android