属性动画基础概念

属性动画一些基础概念:

ViewPropertyAnimator

使⽤用 View.animate() 创建对象,以及使⽤用 ViewPropertyAnimator.translationX() 等⽅方法来设置动画;
可以连续调⽤用来设置多个动画;setDuration() 来设置持续时间、 setStartDelay() 来设置开始延时;
以及其他⼀一些便便捷⽅方法。

        view.animate()
                .translationX(Utils.dp2px(200))
                .translationY(100)
                .rotation(180)
                .alpha(0.5f)
                .setStartDelay(1000)
                .start();

ObjectAnimator

使⽤用 ObjectAnimator.ofXxx() 来创建对象,以及使⽤用 ObjectAnimator.start() 来主动启动动画。它的优势在于,可以为⾃自定义属性设置动画。

ObjectAnimator animator = ObjectAnimator.ofObject(view, "radius",Utils.dp2px(200));

另外,⾃自定义属性需要设置 getter 和 setter ⽅方法,并且 setter ⽅方法⾥里里需要调⽤用 invalidate() 来触发重绘:

public float getRadius() {
    return radius;
}
public void setRadius(float radius) {
    this.radius = radius;
    invalidate();
}

Interpolator

插值器器,⽤用于设置时间完成度到动画完成度的计算公式,直⽩白地说即设置动画的速度曲线,通过setInterpolator(Interpolator) ⽅方法来设置。

PropertyValuesHolder

⽤于设置更加详细的动画,例如多个属性应⽤用于同⼀个对象:

PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("radius",Utils.dp2px(200));
PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("offset",Utils.dp2px(100));
ObjectAnimator animator = PropertyValuesHolder.ofPropertyValuesHolder(view,holder1, holder2);

或者配合使用 Keyframe ,对一个属性分多个段:

Keyframe keyframe1 = Keyframe.ofFloat(0, Utils.dpToPixel(100));
Keyframe keyframe2 = Keyframe.ofFloat(0.5f, Utils.dpToPixel(250));
Keyframe keyframe3 = Keyframe.ofFloat(1, Utils.dpToPixel(200));
PropertyValuesHolder holder = PropertyValuesHolder.ofKeyframe("radius",keyframe1,keyframe2, keyframe3);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view,holder);

TypeEvaluator

⽤于设置动画完成度到属性具体值的计算公式。默认的 ofInt() ofFloat() 已经有了了自带的IntEvaluator FloatEvaluator ,但有的时候需要⾃己设置 Evaluator。

class PointEvaluator implements TypeEvaluator<Point> {
        @Override
        public Point evaluate(float fraction, Point startValue, Point endValue) {
            // (1, 1)   (5, 5)     fraction: 0.2   x: 1 + (5 - 1) * 0.2 y: 1 + (5 - 1) * 0.2
            float x = startValue.x + (endValue.x - startValue.x) * fraction;
            float y = startValue.y + (endValue.y - startValue.y) * fraction;
            return new Point((int) x, (int) y);
        }
    }

另外,对于不支持的类型,也可以使用 ofObject() 来在创建 Animator 的同时就设置上Evaluator,比如 PointView:

public class PointView extends View {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    Point point = new Point(0, 0);

    public PointView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    {
        paint.setStrokeWidth(Utils.dpToPixel(15));
        paint.setStrokeCap(Paint.Cap.ROUND);
    }

    public Point getPoint() {
        return point;
    }

    public void setPoint(Point point) {
        this.point = point;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawPoint(point.x, point.y, paint);
    }
}

在使用时:

ObjectAnimator animator = ObjectAnimator.ofObject(view, "province", new ProvinceEvaluator(), "澳门特别行政区");
        animator.setDuration(10000);
        animator.start();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章