一直對各種開源控件上絢麗的動(dòng)畫效果很是羨慕际插,但只停留在拿來主義上哼审,并不能真正去學(xué)習(xí)到什么谐腰。所以特意找了郭神的三篇屬性動(dòng)畫博文認(rèn)真看了一遍,不得不說棺蛛,郭神真是厲害怔蚌,講的太好了爹土。
郭神Blog:http://blog.csdn.net/guolin_blog
看完的同時(shí)迫筑,隨便手打了例子轴踱。
<pre>
public class MyAnimView extends View
{
private Paint mPaint;
private PointModel pointModel;
private String color;
public MyAnimView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public MyAnimView(Context context)
{
this(context, null);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if (pointModel == null)
{
pointModel = new PointModel();
pointModel.setX(50);
pointModel.setY(50);
drawCircle(canvas);
sartAnimation();
} else
{
drawCircle(canvas);
}
}
public String getColor()
{
return color;
}
public void setColor(String color)
{
this.color = color;
mPaint.setColor(Color.parseColor(color));
invalidate();
}
private void drawCircle(Canvas canvas)
{
canvas.drawCircle(pointModel.getX(), pointModel.getY(), 50, mPaint);
}
public void sartAnimation()
{
PointModel start = new PointModel();
start.setX(50);
start.setY(50);
PointModel end = new PointModel();
end.setX(getWidth() - 50);
end.setY(getHeight() - 50);
ValueAnimator valueAnimator = ValueAnimator.ofObject(new PointEvaluator(), start, end);
valueAnimator.setDuration(5000);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator animation)
{
pointModel = (PointModel) animation.getAnimatedValue();
invalidate();
}
});
ObjectAnimator objectAnimator = ObjectAnimator.ofObject(this,"color",new ColorEvaluator(),"#0000FF", "#FF0000");
objectAnimator.setDuration(5000);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(valueAnimator).with(objectAnimator);
animatorSet.setDuration(5000);
animatorSet.start();
}
private void init()
{
mPaint = new Paint();
mPaint.setColor(0xFF40A5DB);
}
</code>
</pre>