2013-05-02 140 views
2

我正在创建一个自定义视图,其中45%的弧线围绕圆形图像旋转。 我已经实现了这一点,但动画减慢作为动画收益和某个时候停止后,任何人可以帮助我什么是具有相同的问题,画布动画放慢

下面的代码

public class GlowCircle extends ImageView { 

    private Context context; 
    private Paint paintBitmap,paintRing; 
    private Bitmap imageBitmap; 
    private Path path; 
    private int width,height; 
    private float radius; 
    private RectF oval,dst; 
    private String tag="GlowCircle"; 
    private int i=0; 
    private float strokeWith; 
    private float strokeMultiplier=1/10f; 

// Constructors 

    public GlowCircle(Context context) { 
     super(context); 
     initView(context); 

    } 

    public GlowCircle(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initView(context); 

    } 

    public GlowCircle(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     initView(context); 
    } 



// Overriden Methods  

    @Override 
    public void setImageResource(int resId) { 
     super.setImageResource(resId); 
     initView(context); 
    } 

    @Override 
    public void setImageBitmap(Bitmap bm) { 
     super.setImageBitmap(bm); 
     initView(context); 
    } 

    @Override 
    public void setImageURI(Uri uri) { 
     super.setImageURI(uri); 
     initView(context); 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, 
      int bottom) { 
     super.onLayout(changed, left, top, right, bottom); 
     width=getMeasuredWidth(); 
     height=getMeasuredHeight(); 


     if (width<height){ //radius is smallest measurement with 1px padding 
      strokeWith=strokeMultiplier*width; 
      radius=(width/2f)-strokeWith; 

     }else{ 
      strokeWith=strokeMultiplier*height; 
      radius=(height/2f)-strokeWith; 
     } 
     paintRing.setStrokeWidth(strokeWith); 

    Log.v(tag, "width: "+width+"str: "+strokeWith+"ra: "+radius); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     if (imageBitmap==null){ 
      Log.v(tag, "bitmap null"); 
      return; 
     } 
     dst.set(strokeWith, strokeWith, width-strokeWith, height-strokeWith); 
     oval.set(strokeWith/2, strokeWith/2, width-strokeWith/2, height-strokeWith/2); 
     canvas.save(); 
     path.addCircle(width/2, height/2, radius, Direction.CCW); 
     canvas.clipPath(path); 
     canvas.drawBitmap(imageBitmap, null ,dst, paintBitmap); 
     canvas.restore(); 
     if (i>360){ 
      i=0; 
     } 
     canvas.drawArc(oval, i++, 45,false, paintRing); 
     invalidate(); 

    } 


// Class Functions 

    private void initView(Context context){ 

     this.context=context; 
     paintBitmap=new Paint(); 
     paintRing=new Paint(); 
     paintRing.setStyle(Style.STROKE); 
     paintBitmap.setAntiAlias(true); 
     paintRing.setAntiAlias(true); 
     paintBitmap.setFilterBitmap(true); 
     paintBitmap.setDither(true);// May slow try to remove but this may crash on lower devices 
     paintRing.setColor(Color.RED); 
     BitmapDrawable bitmapDrawable=(BitmapDrawable) getDrawable(); // this enables you to get the image; 
     imageBitmap=bitmapDrawable.getBitmap(); 

     path=new Path(); 
     dst=new RectF(); 
     oval=new RectF(); 
    } 

} 

回答