【问题标题】:Android circle bitmap center label not properly centerAndroid圆形位图中心标签未正确居中
【发布时间】:2015-09-17 10:36:56
【问题描述】:

我有一个方形位图 (200*200)。这个位图有一个背景颜色(黄色)和一个字母在它的中心(想象一个 A)。 从该位图中,我想创建一个带有黑色边框的圆形位图。这个边框是 4 个像素,所以我假装的最终图像是一个 220*220 的位图,其中心有一个半径为 100 的圆,边框为 4。如果我查看这个位图,我会看到一个圆形中心有黑色边框,两侧有一些透明像素 (16)。

我正在这样做:

float scaleWidth = ((float) destWidth) / width;
float scaleHeight = ((float) destHeight) / height;

float scale = Math.max(scaleWidth, scaleHeight);

Matrix matrix = new Matrix();
matrix.postScale(scale, scale);

Bitmap roundBitmap = Bitmap.createBitmap(destWidth + 20, destHeight + 20, Bitmap.Config.ARGB_8888) // This creates a square bitmap of 220*220

BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP); // The original size of bitmap is 200*200
shader.setLocalMatrix(matrix);

Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);

Canvas canvas = new Canvas(roundBitmap);
canvas.drawCircle((destWidth + 20) / 2, (destHeight + 20) / 2, destWidth / 2, paint)

图像是这样创建的:

如何让字母 A 居中?

在左边我得到了原始图像(位图),在左边(roundBitmap)我得到了我想要的,除了标签居中:

【问题讨论】:

  • 有人在问题中发现了两个“if if...”吗?
  • 感谢您指出这一点
  • 对某些人来说可能是一个很好的练习。但无论如何,您的字母是否在方形位图中居中?
  • 原图是
  • 尝试将您的比例更改为float scaleWidth = (((float) destWidth)+20) / width; float scaleHeight = (((float) destHeight)+20) / height;

标签: android bitmap


【解决方案1】:

由于您的图像大小始终相同,因此您应该如下修改您的代码

scaleWidth = (((float) destWidth)+20) / width; float scaleHeight = (((float) destHeight)+20) / height;

【讨论】:

    【解决方案2】:

    Chris Banes 写了一篇关于这个确切问题的帖子。 Measuring Text.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多