Android圆形位图中心标签未正确居中
我有一个正方形位图(200*200)。此位图有一个背景色(黄色)和一个字母在其中心(想象一个a)。 从该位图我想创建一个黑色边框的圆形位图。这个边框是4个像素,所以我假装的最终图像是一个220*220的位图,中间有一个半径为100的圆,边框是4。如果我看这个位图,我会看到一个圆圆,圆心有一个黑色边框,边上有一些透明像素(16) 我正在这样做:Android圆形位图中心标签未正确居中,android,bitmap,Android,Bitmap,我有一个正方形位图(200*200)。此位图有一个背景色(黄色)和一个字母在其中心(想象一个a)。 从该位图我想创建一个黑色边框的圆形位图。这个边框是4个像素,所以我假装的最终图像是一个220*220的位图,中间有一个半径为100的圆,边框是4。如果我看这个位图,我会看到一个圆圆,圆心有一个黑色边框,边上有一些透明像素(16) 我正在这样做: float scaleWidth = ((float) destWidth) / width; float scaleHeight = ((float)
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居中
在左边,我得到了原始图像(位图),在左边(圆形位图),我得到了我想要的,除了居中的标签:
克里斯·贝恩斯(Chris Banes)就这个问题写了一篇文章。
.由于您的图像大小始终相同,因此您应该按如下所示修改代码
scaleWidth = (((float) destWidth)+20) / width; float scaleHeight = (((float) destHeight)+20) / height;
有人注意到问题中的两个“如果…”吗?谢谢你指出Outoull对一些人来说是一个很好的练习。但不管怎样,你的字母是否在正方形位图中居中?在原始图像中,它是尝试将你的比例更改为
float scaleWidth=((float)destWidth)+20)/width;浮点数高度=((浮点数)高度)+20)/高度代码>