Daniel Benedykt
Daniel Benedykt

Reputation: 6553

Android - drawing cache - when is it useful?

I am reading about setDrawingCacheEnabled and getDrawingCache and I was wondering when is it good to use it or when its not good.

Basically in my case I have an HorizontalScrollView with many things inside it so its scrolls left/right and most of the things are not visible.

If I use setDrawingCacheEnabled(true) on the views, does it help? or this is only when I use custom views and I call getDrawingCache()?

Is there any other 'cache' way to use in a HorizontalScrollView?

Upvotes: 17

Views: 12803

Answers (2)

Kevin Read
Kevin Read

Reputation: 2193

It's definitely useful for screenshots as Marcel said. It is also very useful performance-wise, as that is what it was created for. It does use up more memory, as you render the view into a bitmap first.

What you do is, you setDrawingCacheEnabled to true, call getDrawingCache which returns a bitmap and store this bitmap. In onDraw, you do draw the bitmap you got if the cache is on, or the view otherwise. This can be very nice when scrolling.

Upvotes: 4

Mojo Risin
Mojo Risin

Reputation: 8142

TouchInterceptor.java - This is class responsible for reordering your playlist in the default music player. It uses setDrawingCacheEnabled when you start dragging the current view. Basically, it creates a bitmap from the ListView item and drag it. Take a closer look at onInterceptTouchEvent method.

Upvotes: 5

Related Questions