Canvas animation slows down

I am creating a custom view where a an arc of 45% rotates around a circular image. I have achieved this, but the animation slows down as the animation proceeds and after sometime it stops, can any one help me out with what is the problem with the same,

Here's the code

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();
    }

}

Answers


Got this to fix by using an external thread

look at the answer Android canvas update issues


Need Your Help

octave/ gnuplot - Plot RGB pixel intensities

gnuplot octave

I am trying to read an input image using octave and plot its RGB values as a 3 dimension plot.

How to Prevent PDF Document from Being download to users PC?

asp.net pdf

How to Prevent PDF Document from Being download to users PC? it can be viewed on browser but should not download the file to PC.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.