我有图像列表。我使用此功能对图像的边角进行了舍入:
public Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = null;
try {
if(bitmap != null) {
output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
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 = pixels;
paint.setAntiAlias(true);
// canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return output;
}
它成功地围绕图像的角落,但角落模糊,粗糙和像素化。角落不顺畅。示例输出为:
我该如何解决这个问题?
答案 0 :(得分:0)
我的代码中没有任何问题,但我使用此代码:
public static Bitmap roundBitmap(Bitmap bitmap, int radius) {
Paint paintForRound = new Paint();
paintForRound.setAntiAlias(true);
paintForRound.setColor(0xff424242);
paintForRound.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
canvas.drawARGB(0, 0, 0, 0);
paintForRound.setXfermode(null);
canvas.drawRoundRect(rectF, radius, radius, paintForRound);
paintForRound.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paintForRound);
return output;
}
此代码给出了像素完美的结果:
答案 1 :(得分:0)
使用此公式
使用角半径float roundPx = bitmap.getWidth() > bitmap.getHeight() ? ((float) bitmap.getHeight())/6 : ((float) bitmap.getWidth())/6;
roundPx = roundPx < 10 ? 10 : roundPx;