思路:
1.基于时间的显示映射。如:给定度数,显示圆弧,加上时序,即可有圆弧动画的效果
2.给定时序。用于驱动动画的一帧帧绘制
方案一、基于ObjectAnimator。动画运作时会调用degree对应set函数(基于放射调用),即setDegree。
ObjectAnimator ani=ObjectAnimator.ofInt(myView, "degree", 0,300);
ani.start();
注:1>混编后,默认会将setDegree混掉,导致找不到函数,故混编后这种机制会失效。解决方法是1.proguard中防止该段代码混编(具体方法百度) 2.使用方法二
2>ObjectAnimator在3.0后才支持,可使用NineOldAndroids库,效果完全一样。
方案二、仍然基于ObjectAnimator。但基于回调,这种方法未用到反射,故混编时仍ok
ObjectAnimator ani=ObjectAnimator.ofInt(myView, new Prop(), 0,300);
ani.start();
class Prop extends Property<View, Integer> { public Prop() { // TODO Auto-generated constructor stub super(Integer.class, "kk"); } @Override public void set(View object, Integer value) { // TODO Auto-generated method stub ((MyView1)object).setDegree(value); } @Override public Integer get(View object) { // TODO Auto-generated method stub return null; } };
方案三、用animation提供时序。interpolatedTime为0~1,即时间的百分比。
Animation ani=new Animation() { @Override protected void applyTransformation(float interpolatedTime, Transformation t) { // TODO Auto-generated method stub myView.setDegree((int)(interpolatedTime*300f)); } }; ani.setDuration(3000); myView.startAnimation(ani);
//===========================================================================
自定义视图,setDegress可改变圆弧角度:
private class MyView1 extends ImageView { public int degree = 0; public MyView1(Context ct) { // TODO Auto-generated constructor stub super(ct); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); Rect r = new Rect(); getLocalVisibleRect(r); canvas.drawArc(new RectF(r), 0, degree, true, pt); } public void setDegree(int degree) { this.degree = degree; invalidate(); } }
效果: