画布动画变慢了

时间:2013-05-02 12:46:52

标签: android performance android-canvas

我正在创建一个自定义视图,其中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();
    }

}

1 个答案:

答案 0 :(得分:0)

通过使用外部线程

来解决此问题

看看答案 Android canvas update issues