关于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 表示无限循环

相关推荐
执明wa13 小时前
LayoutInflater详解: XML是如何变成View的?
android·xml·开发语言·android studio
404_coder14 小时前
源码视角下的 Android 开机流程:从 Zygote、SystemServer 到 Launcher
android
二流小码农14 小时前
鸿蒙开发:以登录案例了解代码架构MVVM
android·ios·harmonyos
用户693717500138415 小时前
从代码生产者到 AI 协作者:软件工程师的角色重构
android·前端·后端
GitLqr15 小时前
别在 Flutter 的 main() 里乱锁屏幕方向,小心 iPad 分屏功能被你搞没了
android·flutter·ios
sg_knight15 小时前
MySQL 存储过程详解:从入门到实战
android·数据库·mysql·database·dba·关系型数据库·db
爱笑鱼15 小时前
Binder(四):ioctl(BINDER_WRITE_READ) 之后,事务怎样到达目标进程?
android
AFinalStone15 小时前
Android 7系统休眠唤醒(二)开机全链路—BootROM到Launcher
android·电源管理·休眠唤醒
Mr YiRan16 小时前
Android NDK开发之统计到未被回收的图片
android