我正在使用 TextView 。它有一个图像作为背景。如何以编程方式将此图像的角圆角?
最佳答案
将您的图像转换为位图,然后将该位图转换为圆角位图。最后将该位图应用于您的 TextView 背景。以下代码用于将位图转换为圆形位图图像。
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,int roundPixelSize) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = roundPixelSize;
paint.setAntiAlias(true);
canvas.drawRoundRect(rectF,roundPx,roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
关于android - 如何以编程方式使图像角变圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7088596/