安卓的getResource()未定义错误错误、未定义、getResource

2023-09-05 07:19:53 作者:你触及不到的最美好、

我想提请位图上MyPositionOverlay绘制方法扩展覆盖类,但我得到这个错误:该方法的getResource()是未定义的类型MyPositionOverlay

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

我哪里错了?

下面是code形式的抽奖方式:

Here is code form draw method:

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

感谢

推荐答案

在getResources()方法不是覆盖类的成员。 getResources()是Context类中的一员。你需要通过一个上下文的引用您的覆盖子类,以便它可以加载绘制资源:

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.