Combine image and text to drawable

自闭症网瘾萝莉.ら 提交于 2019-11-26 19:09:54

问题


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.

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

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

I am using the Hello Mapview tutorial


回答1:


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

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.



来源:https://stackoverflow.com/questions/6691818/combine-image-and-text-to-drawable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!