Android error getResource () undefined

I want to draw a bitmap on the draw method in MyPositionOverlay, extends the Overlay class, but I get this error: getResource () method is undefined for type MyPositionOverlay

Where am I mistaken?

Here is a code form drawing method:

Bitmap bmp = BitmapFactory.decodeResource(getResource(), R.drawable.icon); canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null); 

thanks

+7
android
source share
2 answers

The getResources () method is not a member of the Overlay class. getResources () is a member of the Context class. You need to pass the Context link to your Overlay subclass so that it can load the Drawable resource:

 Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon); 

You also do not want to load the bitmap in your drawing method, since it uses memory very much and slows down your application, you must save the member variable in the bitmap in the overlay constructor so that it gets loaded once.

+14
source share

using

  Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon); 

or

 Bitmap bmp = BitmapFactory.decodeResource(Context.getResources(), R.drawable.icon); 
+1
source share

All Articles