如果看了objectanimator(真正的勞動者)和TypeEvaluator的使用那么你對自定義TypeEvaluator和對任意屬性做動畫這兩個概念就應(yīng)該很熟悉了.下面這個例子就是對二者的綜合使用.
我們知道補(bǔ)間動畫的效果很局限于移動,縮放,旋轉(zhuǎn),和淡入淡出這四種效果.通過ObjectAnimator我們可以完成對任意屬性設(shè)置動畫效果,通過TypeEvaluator我們可以改變動畫的效果顯示狀態(tài).
需求:移動小球,并改變小球顏色.
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;
}
}
效果: