1

I've got the following problem

I want to create many instances of a view (dinamically). Each view can have one of 6 possible icons. What I wanted to do create a static Bitmap so that I can use in in the Draw function withouth having the same icon on memory 40 times....

like so:

public static final Bitmap InstinctBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.tech), SmallIconSide, SmallIconSide, false);

Obviously this doesn't work because getResources() is not static. Any workaround? Or is this a bad Idea?

The onDraw method of the view keeps telling me that is a bad Idea to instatiate objects in it, so this is the choice I made

Any help would be greatly appreciated.

Kara
  • 6,115
  • 16
  • 50
  • 57
aarelovich
  • 5,140
  • 11
  • 55
  • 106
  • i think its a bad idea .. please add some more information !! – r4jiv007 Dec 31 '13 at 20:49
  • There is really nothing more to add. I'll explain it again. I have Custom View. I need to generate several of these views (about 40) in the program. Each (depending on a configuration file) can have one of six possible icons. To draw that Icon in the OnDraw method I need to turn it into a Bitmap. If I manage to create a static Bitmap there will only be 6 loaded images in memory. Otherwise there will be 40. Is that better? (I've been told I m no clear when asking) – aarelovich Dec 31 '13 at 21:05
  • I agree with @r4jiv007, and would use an LruCache instead for this. However, the general approach to gaining static access to a Context (and thus getResources()) can be found here: http://stackoverflow.com/questions/2002288/static-way-to-get-context-on-android – Eric Schlenz Dec 31 '13 at 21:13

0 Answers0