将图像和文本组合成可绘制的

时间:2021-03-31 20:32:56

I want to create a drawable, which consists of a map pin(bubble) and some text. The bubble should be in the background and the text in the foreground.

我想创建一个drawable,它由一个map pin(bubble)和一些文本组成。气泡应该在背景中,文本在前景中。

This drawable should be passed in super(drawable) of the class BalloonItemizedOverlay which extends ItemizedOverlay<Item>.

这个drawable应该在BalloonItemizedOverlay类的super(drawable)中传递,它扩展了ItemizedOverlay

In other words, I want to show text in the bubble that appears in the map.

换句话说,我想在地图中显示的气泡中显示文字。

I am using the Hello Mapview tutorial

我正在使用Hello Mapview教程

1 个解决方案

#1


62  

This method takes a drawable from your resources, draws some text on top of it and returns the new drawable. All you need to do is give it the resource id of your bubble, and the text you want on top. Then you can pass the returned drawable wherever you want it.

这个方法从你的资源中抽取一个drawable,在它上面绘制一些文本并返回新的drawable。您需要做的就是为它提供泡泡的资源ID以及您想要的文本。然后你可以在任何你想要的地方传递返回的drawable。

public BitmapDrawable writeOnDrawable(int drawableId, String text){

        Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
        Paint paint = new Paint(); 
        paint.setStyle(Style.FILL);  
        paint.setColor(Color.BLACK); 
        paint.setTextSize(20); 

        Canvas canvas = new Canvas(bm);
        canvas.drawText(text, 0, bm.getHeight()/2, paint);

        return new BitmapDrawable(bm);
    }

To preserve density you need this constructor

要保持密度,您需要此构造函数

BitmapDrawable (Resources res, Bitmap bitmap)

So, keeping your context, last return should be something like

所以,保持你的背景,最后的回归应该是这样的

return new BitmapDrawable(context.getResources(), bm);

This prevent an undesired resized drawable.

这可以防止不希望的尺寸调整的可绘制。

#1


62  

This method takes a drawable from your resources, draws some text on top of it and returns the new drawable. All you need to do is give it the resource id of your bubble, and the text you want on top. Then you can pass the returned drawable wherever you want it.

这个方法从你的资源中抽取一个drawable,在它上面绘制一些文本并返回新的drawable。您需要做的就是为它提供泡泡的资源ID以及您想要的文本。然后你可以在任何你想要的地方传递返回的drawable。

public BitmapDrawable writeOnDrawable(int drawableId, String text){

        Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
        Paint paint = new Paint(); 
        paint.setStyle(Style.FILL);  
        paint.setColor(Color.BLACK); 
        paint.setTextSize(20); 

        Canvas canvas = new Canvas(bm);
        canvas.drawText(text, 0, bm.getHeight()/2, paint);

        return new BitmapDrawable(bm);
    }

To preserve density you need this constructor

要保持密度,您需要此构造函数

BitmapDrawable (Resources res, Bitmap bitmap)

So, keeping your context, last return should be something like

所以,保持你的背景,最后的回归应该是这样的

return new BitmapDrawable(context.getResources(), bm);

This prevent an undesired resized drawable.

这可以防止不希望的尺寸调整的可绘制。