结合图片和文字来绘制对象对象、文字、图片

2023-09-12 04:02:30 作者:つ看眼泪往下掉

我想创建一个可绘制的,它由一个映射引脚(泡沫)和一些文字。气泡应该在背景和前景中的文字。

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.

这drawabale应通过在类BalloonItemizedOverlay延伸ItemizedOverlay超(绘制)。

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

换句话说,我要显示在显示在地图上的泡沫文本。

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

我现在用的是你好的MapView 教程

I am using the Hello Mapview tutorial

推荐答案

此方法需要从你的资源被拉伸,画在它上面的一些文字,并返回新的绘制。所有你需要做的就是把你的泡沫的资源ID和文本你想在上面。然后,你可以通过返回的绘制任何您想要的。

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.

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);
    }

要preserve密度则需要此构造

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);

这prevent不期望的大小绘制。

This prevent an undesired resized drawable.