android那些事--属性动画05-ObjectAnimator的高级使用

如果看了objectanimator(真正的劳动者)TypeEvaluator的使用那么你对自定义TypeEvaluator和对任意属性做动画这两个概念就应该很熟悉了.下面这个例子就是对二者的综合使用.

我们知道补间动画的效果很局限于移动,缩放,旋转,和淡入淡出这四种效果.通过ObjectAnimator我们可以完成对任意属性设置动画效果,通过TypeEvaluator我们可以改变动画的效果显示状态.

需求:移动小球,并改变小球颜色.

blic class MyView extends View {

    private Paint mPaint;
    private Point mPoint;

    private String color;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
        mPaint.setColor(Color.parseColor(color));
        invalidate();
    }

    public MyView(Context context) {
        super(context);
        init();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.BLUE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mPoint == null) {
            mPoint = new Point(50, 50);
            drawCircle(canvas);
            startAnimation();
        } else {
            drawCircle(canvas);
        }
    }

    private void drawCircle(Canvas canvas) {
        canvas.drawCircle(mPoint.getX(), mPoint.getY(), 50, mPaint);
    }

    private void startAnimation() {
        Point startPoint = new Point(50, 50);
        Point endPoint = new Point(getWidth() - 50, getHeight() - 50);

        ValueAnimator animator = ValueAnimator
                .ofObject(new PointEvaluator(), startPoint, endPoint);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

                mPoint = ((Point) animation.getAnimatedValue());
                LogUtil.i(mPoint.getX() + "------" + mPoint.getY());
                invalidate();
            }
        });


        ObjectAnimator colorAnimator = ObjectAnimator.ofObject(this, "color", new ColorEvaluator(),
                "#0000ff", "#ff0000");

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(animator).with(colorAnimator);
        animatorSet.setDuration(5000);
        animatorSet.start();
    }
}

颜色的控制

public class ColorEvaluator implements TypeEvaluator<String> {

    private int currentRed = -1;
    private int currentGreen = -1;
    private int currentBlue = -1;

    @Override
    public String evaluate(float fraction, String startValue, String endValue) {

        int startRed = Integer.parseInt(startValue.substring(1, 3), 16);
        int startGreen = Integer.parseInt(startValue.substring(3, 5), 16);
        int startBlue = Integer.parseInt(startValue.substring(5, 7), 16);
        int endRed = Integer.parseInt(endValue.substring(1, 3), 16);
        int endGreen = Integer.parseInt(endValue.substring(3, 5), 16);
        int endBlue = Integer.parseInt(endValue.substring(5, 7), 16);

        if (currentRed == -1) {
            currentRed = startRed;
        }
        if (currentBlue == -1) {
            currentBlue = startBlue;
        }
        if (currentGreen == -1) {
            currentGreen = startGreen;
        }
        int diffRed = Math.abs(startRed - endRed);
        int diffGreen = Math.abs(startGreen - endGreen);
        int diffBlue = Math.abs(startBlue - endBlue);
        if (currentRed != endRed) {
            currentRed = getCurrentColor(startRed, endRed, diffRed, 0, fraction);
        } else if (currentGreen != endGreen) {
            currentGreen = getCurrentColor(startGreen, endGreen, diffGreen, diffRed, fraction);
        } else if (currentBlue != endBlue) {
            currentBlue = getCurrentColor(startBlue, endBlue, diffBlue, diffRed + diffGreen, fraction);
        }
        String currentColor = "#" + getHexString(currentRed) +
                getHexString(currentGreen) +
                getHexString(currentBlue);
        LogUtil.i(currentColor);
        return currentColor;
    }

    private int getCurrentColor(int startColor, int endColor,
                                int diffColor, int offset, float fraction) {
        int currentColor = 0;
        if (startColor > endColor) {
            currentColor = (int) (startColor - (fraction * diffColor - offset));
            if (currentColor < endColor) {
                currentColor = endColor;
            }
        } else {
            currentColor = (int) (startColor + (fraction * diffColor - offset));
            if (currentColor > endColor) {
                currentColor = endColor;
            }
        }
        return currentColor;
    }

    private String getHexString(int value) {
        String hexString = Integer.toHexString(value);
        if (hexString.length() == 1) {
            hexString = "0" + hexString;
        }
        return hexString;
    }
}

效果:


这里写图片描述

推荐阅读更多精彩内容

  • 参考于:http://blog.csdn.net/guolin_blog/article/details/4353...
    墨染书阅读 2,337评论 0 2
  • 我可能时常会动情, 可人总是会动情的; 我时常感到踌躇不定, 生活也总是如此, 才叫生活。 我渴望一种偶然, 像灵...
    黄庆子阅读 68评论 0 2
  • 早晨出门上班,为稍微多走几步路顺带运动一下,就没有选择开车,也不骑共享单车,凭着双脚径直往地铁站走去。 途径邻院小...
    紫螳螂阅读 93评论 2 2
  • 你不能说,你每做一件事情都要出来讲,做成了还好说,如果做不成呢?消耗的是身边人对你的信任。 -1- 就在刚刚,舅妈...
    子豪同学c阅读 726评论 0 1
  • 一个人的旅行不孤单却未必安全,许多人的旅行很热闹却难免走马观花,朋友三两一起出行相互照应兴趣相投最好不过。 投身大...
    任静66阅读 96评论 0 0