1

In my app the user manipulates a image, rotates, pinch, zoom etc. This is make within onDraw() using canvas.translate(), canvas.rotate() etc. Of course the image is big and after the manipulation of the image the user performs interactions with other Views. The thing is I don't want to redraw the whole big image over and over again only the visible part. So I'd like, after the user finishes operations, SAVE the current VISIBLE image on the screen to memory/bitmap/cache and redraw only that.

How can I achieve this ?

Alin
  • 14,809
  • 40
  • 129
  • 218

4 Answers4

3

In the constructor of your custom View, enable the drawing cache like this: setDrawingCacheEnabled(true);. Then whenever you want to save the state of your Canvas you can call getDrawingCache(), which returns the desired Bitmap.

Documentation: getDrawingCache()

Thomas
  • 1,508
  • 2
  • 22
  • 35
  • You have an answer here but unfortunatelly as I said I am working with images and it seems that buildDrawingCache should not have my view if it is bigger than the screen as the visible portion of the view is only drawn and the cache holds only what is drawn.... any way of fixing this ? I need to cache only the visible screen while my view is larger than the screen... – Alin Dec 12 '11 at 09:15
  • Your first sentence is not totally clear to me. And can you also clarify the difference between drawn and visible in this context? – Thomas Dec 12 '11 at 11:16
  • I read that in order to be able to cache the content of a view, the view must not be bigger than the size of the screen. So working with a 2000x1000 px image in a custom view on a 800x480 screen makes it impossible to cache the view. I did a test and the cached bitmap is empty. Or am I missing sometething ? – Alin Dec 12 '11 at 11:35
  • No, I think you're right then. Isn't it possible to make the view as big as the screen, and let the user rotate/resize the contents of the view, instead of the view itself? Please correct me if I'm wrong. – Thomas Dec 12 '11 at 13:07
1

In your on draw initialize your Canvas with a bitmap. keep a reference of this bitmap and save it whenever required.

Abhinava
  • 1,030
  • 9
  • 19
1

I've found a really great article about what I needed HERE

To kepp thing clear, first I need to create a bitmap with the size of my View, then create a canvas for it, draw on canvas and then save it.

public void saveScreenshot() {
            Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            canvas.draw(myGiganticImage)

            File file = new File(context.getFilesDir() + "/cache.jpg");
            FileOutputStream fos;
            try {
                fos = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
                fos.close();

                cachedBackground = BitmapFactory.decodeFile(context.getFilesDir()  + "/cache.jpg"
                invalidate()                
            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            }
        }

I simply call this on onDraw() when I need it. It will save a bitmap on internal memory which I can use to draw, instead of the big picture.

Alin
  • 14,809
  • 40
  • 129
  • 218
0

Following may help you :

(1)

 View v = view.getRootView();
    v.setDrawingCacheEnabled(true);
    Bitmap b = v.getDrawingCache();

(2) This is a similar to another question at How to capture the android device screen content?

(3) You can try using this library http://code.google.com/p/android-screenshot-library/ It introduces an Android Screenshot Library (ASL) which enables to programmatically capture screenshots from Android devices without requirement of having root access privileges.

Community
  • 1
  • 1
Shraddha
  • 1,052
  • 11
  • 22
  • (1) where do I need to run this code? in my activity ? If so, it's not suited for my needs as I need to request getDrawingCache() inside onDraw, just before drawing some other elements. And this probably won't work as I explained earlier. (2) and (3) I don't want to use that library as it has some service starting at device boot. THank you for your help. – Alin Dec 13 '11 at 06:27