一直对各种开源控件上绚丽的动画效果很是羡慕,但只停留在拿来主义上,并不能真正去学习到什么。所以特意找了郭神的三篇属性动画博文认真看了一遍,不得不说,郭神真是厉害,讲的太好了。
郭神Blog:http://blog.csdn.net/guolin_blog
看完的同时,随便手打了例子。
<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>
网友评论