【问题标题】:Android Drawing text onto a bitmapAndroid 在位图上绘制文本
【发布时间】:2014-09-19 13:47:19
【问题描述】:

我正在尝试在 Android 的位图上写一些文本。我遵循了几个指南并尝试了几种不同的代码变体,但我要么得到一些模糊的东西,要么是空白屏幕,要么是未更改的位图。这是我一直在玩的基本想法。

public Bitmap writeOnDrawable(Context gContext, String path, String text) {

    Bitmap bitmap = BitmapFactory.decodeFile(path);

    Bitmap bmOverlay = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_4444);

    Canvas canvas = new Canvas(bmOverlay);

    Paint paint = new Paint();
    paint.setColor(Color.CYAN);
    paint.setTextSize(20);
    paint.setFlags(Paint.ANTI_ALIAS_FLAG);

    canvas.drawBitmap(bitmap, 0, 0, null);
    canvas.drawPoint(30, 50, paint);
    canvas.drawText("Text", 33, 53, paint);

    return bmOverlay;
}

我将变量传递给这个函数,然后通过将返回的位图转换为 BitmapDrawable 并使其成为布局的背景来使用它。这个特定版本的函数只返回我最初的、未更改的位图,其中没有“文本”文本。

我尝试了一些变化,例如复制原始图像:

Bitmap drawableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

将原始图像绘制到画布上:

canvas.drawBitmap(drawableBitmap, screenHeight, screenWidth, paint);

【问题讨论】:

标签: android android-canvas


【解决方案1】:

我使用 android-maps-util 库解决了这个问题。

compile 'com.google.maps.android:android-maps-utils:0.4'
依赖项添加到您的 gradle 文件中。
使用 IconGenerator 创建带有文本的位图,并在您的 BitmapDescriptor 中使用它来在您的地图上创建标记。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多