0

I'm developing an app that draws a circle into an image when the user touch the screen, and also the user can move this circle moving the finger over the screen. But in the action of move, I can see a lot of lag...

The user open an image of this gallery, and after that, what I do is:

1)the user touch the screen

2)calculate de correspondence between screen-point and image-point (+100ms)

3)drawing:(+200ms)

-create a bitmap with the size of the original image

-create a canvas based on the previous bitmap

-canvas.drawimage, draw the original image

-canvas.drawcircle, draw the circle

4)set the result bitmap to an imageview(+100ms)

Every time that the user move his finger, I waste 400ms doing all the process...a lot of time

I know that the resolution of the image it's very important, but I'm using a 640x480 image ...so it's not a very very big image...and I am testing my app in a samsung galaxy s2, so I was specting better results...

Loktar
  • 34,764
  • 7
  • 90
  • 104
culebrins
  • 428
  • 9
  • 21

1 Answers1

0

Now you're allocating image in each frame, which can be very costly and result in GC running every 5th - 10th draw. For me it would be much better to derive and override drawing method in ImageView. In such way you can save CPU a lot of work by just drawing circle after calling super.draw(). Something like this:

new ImageView(null){
    Paint paint = new Paint();
    int cx,cy,radius = 10;
    public void setPoint(int x,int y){
        cx = x;cy = y;
    }
    public void draw(android.graphics.Canvas canvas) {
        super.draw(canvas);
        canvas.drawCircle(cx, cy, radius, paint);
    };
}

After setting an image (once) and finger position (after touch event) just call invalidate().

Zielony
  • 16,239
  • 6
  • 34
  • 39
  • I've deleted the "create a bitmap with the size of the original image" and now it's faster...but I'm going to try to optimize a litle bit more my app. I'm not sure if I've understand you. Do I have to create a class wich extends from imageview and overwrite the draw method? Wich is the difference between generate the result image inside a method and assing the result to to an imageview, or calculate everything in the draw method? I will be doing the same work in both methods. – culebrins Nov 27 '12 at 13:34
  • Using overriden draw method you have to draw bitmap only once. Setting bitmap with circle needs two passes - draw to buffer and draw on screen. It also uses less memory, because you don't have to allocate additional bitmap to buffer your drawing. – Zielony Nov 27 '12 at 15:28