将图像和文本合并为可绘制 [英] Combine image and text to drawable

查看:29
本文介绍了将图像和文本合并为可绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个drawable,它由一个地图图钉(气泡)和一些文本组成.气泡应该在背景中,文本在前景中.

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应该传入BalloonItemizedOverlay类的super(drawable),它扩展了ItemizedOverlay.

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.

我正在使用 Hello Mapview 教程

I am using the Hello Mapview tutorial

推荐答案

此方法从您的资源中获取一个可绘制对象,在其上绘制一些文本并返回新的可绘制对象.你需要做的就是给它你的气泡的资源 ID,以及你想要的文本.然后你可以在任何你想要的地方传递返回的 drawable.

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

这可以防止出现不需要的调整大小的 drawable.

This prevent an undesired resized drawable.

这篇关于将图像和文本合并为可绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆