【问题标题】:How to draw a text at the corner of a bitmap android如何在位图android的角落绘制文本
【发布时间】:2018-08-27 09:31:29
【问题描述】:

我有一个位图,我想在右上角的位图上绘制一个文本。但首先我无法在其上绘制任何文本。我正在将布局转换为位图,然后尝试在其上绘制文本。但它不工作。这是我的代码:

private Bitmap viewToBitmap(LinearLayout layout) {
        Bitmap bitmap = Bitmap.createBitmap(layout.getWidth(), layout.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        layout.draw(canvas);
        canvas=new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
        paint.setTextSize(14.f);
        paint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText("Hello Android!", 0, 0, paint);
        return bitmap;
    }

【问题讨论】:

  • 您是动态获取位图还是在布局中静态获取位图?
  • 静态布局。 @VivekMishra
  • 0, 0 将在左上角显示您的文字
  • 然后使用框架布局
  • 我无法绘制任何文字。 @AbdulKawee

标签: android android-canvas android-bitmap


【解决方案1】:

请注意文字位置和对齐方式。

private Bitmap viewToBitmap(View view)
{
    Bitmap result = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    view.draw(canvas);

    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setAntiAlias(true);
    paint.setTextSize(14);
    paint.setTextAlign(Paint.Align.RIGHT);
    canvas.drawText("Hello Android!", view.getWidth(), 14, paint); // draw watermark at top right corner
    return result;
}

【讨论】:

  • 一个赞成 paint.setTextAlign(Paint.Align.RIGHT);我想念我的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多