android - 以编程方式创建图标以用作 ItemizedOverlay 可绘制对象 - Android

标签 android drawable itemizedoverlay

我正在尝试以编程方式绘制一个 parking 图标,以作为 map 上逐项叠加层的可绘制对象放置。

该图标由一个蓝色正方形组成,其中心有一个白色“P”,我想以编程方式更改正方形的颜色以表示不同的 parking 类型。

我尝试使用 drawRect 和 drawText 通过 Canvas 创建它,但我找不到一种简单的方法将文本居中放置在正方形中,我也找不到一种方法将 Canvas 居中放置在坐标上 - 它一直想从左上角。

我也曾尝试创建一个 XML 布局以转换为可绘制对象,但也无法实现。

对于我想要实现的目标,是否有一个优雅的解决方案?

最佳答案

public class TextDrawable extends Drawable {

    private final static int    TEXT_PADDING           = 3;
    private final static int    ROUNDED_RECT_RADIUS    = 5;

    private final String    text;
    private final Paint     textPaint;
    private final Rect      textBounds;
    private final Paint     bgPaint;
    private final RectF     bgBounds;

    public TextDrawable(String text, String backgroundColor, int textHeight) {

        this.text = text;

        // Text
        this.textPaint = new Paint();
        this.textBounds = new Rect();
        textPaint.setColor(Color.WHITE);
        textPaint.setARGB(255, 255, 255, 255);
        textPaint.setAntiAlias(true);
        textPaint.setSubpixelText(true);
        textPaint.setTextAlign(Paint.Align.CENTER); // Important to centre horizontally in the background RectF
        textPaint.setTextSize(textHeight);
        textPaint.setTypeface(Typeface.DEFAULT_BOLD);
        // Map textPaint to a Rect in order to get its true height
        // ... a bit long-winded I know but unfortunately getTextSize does not seem to give a true height!
        textPaint.getTextBounds(text, 0, text.length(), textBounds);

        // Background
        this.bgPaint = new Paint();
        bgPaint.setAntiAlias(true);
        bgPaint.setColor(Color.parseColor(backgroundColor));
        float rectHeight  = TEXT_PADDING * 2 + textHeight;
        float rectWidth   = TEXT_PADDING * 2 + textPaint.measureText(text);
        //float rectWidth   = TEXT_PADDING * 2 + textHeight;  // Square (alternative)
        // Create the background - use negative start x/y coordinates to centre align the icon
        this.bgBounds = new RectF(rectWidth / -2, rectHeight / -2, rectWidth / 2, rectHeight / 2);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawRoundRect(bgBounds, ROUNDED_RECT_RADIUS, ROUNDED_RECT_RADIUS, bgPaint);
        // Position the text in the horizontal/vertical centre of the background RectF
        canvas.drawText(text, 0, (textBounds.bottom - textBounds.top)/2, textPaint);
    }

    @Override
    public void setAlpha(int alpha) {
        bgPaint.setAlpha(alpha);
        textPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        bgPaint.setColorFilter(cf);
        textPaint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

关于android - 以编程方式创建图标以用作 ItemizedOverlay 可绘制对象 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9832777/

相关文章:

android - 位图可绘制( map 叠加项)在android中旋转后像素化

android - 在自定义 ListView 适配器中按下项目时突出显示效果

Android:麦克风音量级别(可能)、自动增益、FFT、Butterworth、Audacity

android - 正在使用哪个可绘制文件夹?

android - 可以用选择器绘制吗?

android - 重绘/刷新逐项叠加? (安卓/谷歌地图 API)

android Activity 保护免受 Intent

java - 将代码从 Java 转换为 Android Studio(显示值)

android - 如何在 Activity 之间传递drawable

android - osmdroid ItemizedOverlay 错误 : The method boundCenterBottom(Drawable) is undefined for the type