结合图片和文字来绘制对象 [英] Combine image and text to drawable

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

问题描述

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

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.

这篇关于结合图片和文字来绘制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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