Android getResource() undefined error

前端 未结 2 2061
天命终不由人
天命终不由人 2021-02-15 15:42

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

2条回答
  •  礼貌的吻别
    2021-02-15 16:27

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

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

    You also don't want to load a bitmap in your draw method as it is very memory intensive and will slow down your application, you should save a member variable of the bitmap in the constructor of the overlay so that it only gets loaded once.

提交回复
热议问题