【发布时间】: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;