How to create ObjectAnimator with valueType - pathType programmatically?

送分小仙女□ 提交于 2019-12-08 03:36:37

问题


I would like to create a ObjectAnimator programmatically that represents this xml -

 <objectAnimator
        android:duration="@integer/eye_anim_duration"
        android:interpolator="@android:interpolator/anticipate_overshoot"
        android:propertyName="pathData"
        android:repeatCount="0"
        android:repeatMode="restart"
        android:valueFrom="@string/big"
        android:valueTo="@string/small"
        android:valueType="pathType"
        />

In similar fashion as I can create a ObjectAnimator for color change -

 ValueAnimator animator = ObjectAnimator.ofInt(vector, "fillColor", getResources().getColor(R.color.light_green), getResources().getColor(R.color.red));
        animator.setDuration(3000);

回答1:


Using PathAnimatorInflater class and rewriting the setupAnimatorForPath to accept Strings instead TypedArray it is possible. Here is a method that uses this -

private ObjectAnimator getAnimator(AnimatedVectorDrawable vector, int in, int out, int delay) {
    ObjectAnimator animator = new ObjectAnimator();
    animator.setTarget(vector);
    animator.setPropertyName("pathData");
    TypeEvaluator evaluator = PathAnimatorInflater.setupAnimatorForPath(animator, getResources().getString(in), getResources().getString(out));
    if (evaluator == null) {
        return null;
    }
    animator.setEvaluator(evaluator);
    animator.setRepeatMode(ValueAnimator.REVERSE);
    animator.setDuration(3000);
    animator.setStartDelay(delay);
    return animator;
}

But to use this with AnimatedVectorDrawable it needs some changes in Vector classes also.



来源:https://stackoverflow.com/questions/36161057/how-to-create-objectanimator-with-valuetype-pathtype-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!