PropertyValuesHolder与Keyframe 笔记

PropertyValuesHolder

ValueAnimator和ObjectAnimator都有对应的ofPorpertyValuesHolder方法

PorpertyValuesHolde创建实例的方法

java 复制代码
public static PropertyValuesHolder ofFloat(String PropertyName,float... values)
public static PropertyValuesHolder ofInt(String PropertyName,int... values)
public static PropertyValuesHolder ofObject(String PropertyName,TypeEvaluator evaluator,Object... values)
public static PropertyValuesHolder ofKeyFrame(String PropertyName,KeyFrame... values)

ofFloat,ofInt使用示例

java 复制代码
PropertyValuesHolder holder = PropertyValuesHolder.ofFLoat("alpha",0.1f,0.3f,0.7f,1f);
ObjectAnimator animator = ObjectAnimator.ofPorpertyValuesHolder(view,holder);
animator.setDuration(2000);
animator.start();

ofObject方法参数,可以实现一个自定义的插值器,使用实例:

java 复制代码
    public class CharEvaLuator implements TypeEvaluator<Character>{
        @Override
        public Character evaluate(float fraction, Character startValue, Character endValue) {
            int startInt = startValue;
            int endInt = endValue;
            int curInt = startInt + fraction * (endInt- startInt);
            char result = (Char)curInt;
            return result;
        }
    }
    
    PropertyValuesHolder holder = PropertyValuesHolder.ofObject("CharText",new CharEvaluator,
    new Character('A'),new Character('Z'));
    ObjectAnimator animator = ObjectAnimator.ofPorpertyValuesHolder(tv,holder);
    animator.setDuration(2000);
    animator.start();
    
    //注意因为TextView 中setText(CharSequence text)参数,需要内部自定义setCharText(Charactor text)结合ofObject使用

Keyframe

keyframe 我个人感觉可以理解为好比做动画时绘制的帧。

java 复制代码
public static KeyFrame ofFloat(float fraction,float duration)
//fraction 当前显示的进度,value 当前动画所在数值位置

ofFloat,ofInt使用实例:

ini 复制代码
KeyFrame frame0 = KeyFrame.ofFloat(0f,0);
KeyFrame frame1 = KeyFrame.ofFloat(0.5f,50);
KeyFrame frame2 = KeyFrame.ofFloat(1f,100);
PropertyValuesHolder holder = PropertyValuesHolder.ofKeyframe("alpha",frame0,frame1,frame2);
ObjetAnimator animator = ObjetAnimator.ofPropertyValuesHolder(view,holder);
animator.setDuration(2000);
animator.start();

//也可以给除第一帧以外的设置插值器
frame1.setInterpolator(new BounceInterpolator());

ofObject()同样也可以实现一些自定义的属性参数,例如

ini 复制代码
KeyFrame frame0 = KeyFrame.ofObject(0,new Character('A'));
KeyFrame frame1 = KeyFrame.ofObject(0,new Character('B'));
KeyFrame frame2 = KeyFrame.ofObject(0,new Character('C'));

//结合上面的CharEvaLuator自定义插值器使用
PropertyValuesHolder holder = PropertyValuesHolder.ofKeyframe("CharText",frame0,frame1,frame2);
holder.setEvalutar(new CharEvaLuator());
ObjetAnimator animator = ObjetAnimator.ofPropertyValuesHolder(view,holder);
animator.setDuration(2000);
animator.start();
注意
  1. 多个KeyFrame,都会默认把第一个frame设为首帧,最后一个设为尾帧,不管是不是你期望的效果

  2. 必须存在2个或2个以上的frame,不然会报数组越界

相关推荐
2603_9494621027 分钟前
Flutter for OpenHarmony社团管理App实战:预算管理实现
android·javascript·flutter
王泰虎2 小时前
安卓开发日记,因为JCenter 关闭导致加载不了三方库应该怎么办
android
2601_949543016 小时前
Flutter for OpenHarmony垃圾分类指南App实战:主题配置实现
android·flutter
2601_949833397 小时前
flutter_for_openharmony口腔护理app实战+知识实现
android·javascript·flutter
晚霞的不甘7 小时前
Flutter for OpenHarmony从基础到专业:深度解析新版番茄钟的倒计时优化
android·flutter·ui·正则表达式·前端框架·鸿蒙
鸟儿不吃草8 小时前
android的Retrofit请求https://192.168.43.73:8080/报错:Handshake failed
android·retrofit
Minilinux20188 小时前
Android音频系列(09)-AudioPolicyManager代码解析
android·音视频·apm·audiopolicy·音频策略
李子红了时8 小时前
【无标题】
android
Android系统攻城狮9 小时前
Android tinyalsa深度解析之pcm_close调用流程与实战(一百零四)
android·pcm·tinyalsa·音频进阶·音频性能实战·android hal
weixin_411191849 小时前
LifecycleEventObserver和DefaultLifecycleObserver使用
android