查看图片角落。请帮忙
我希望将图像的左下角和左上角四舍五入,同时将其设置为图像视图
我的代码:
private static Bitmap createRoundedRectBitmap(@NonNull Bitmap bitmap, float topLeftCorner, float topRightCorner, float bottomRightCorner, float bottomLeftCorner) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output);
final int color = Color.WHITE;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
Path path = new Path();
float[] radii = new float[]{
topLeftCorner, bottomLeftCorner,
topRightCorner, topRightCorner,
bottomRightCorner, bottomRightCorner,
bottomLeftCorner, bottomLeftCorner
};
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
path.addRoundRect(rectF, radii, Path.Direction.CW);
canvas.drawPath(path, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
答案 0 :(得分:1)
答案 1 :(得分:0)
我一起使用了这两个功能:
public Bitmap getRoundedSquareBitmap(Bitmap bitmap) {
Bitmap output;
output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Bitmap.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());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.WHITE);
canvas.drawPath(RoundedRect(0, 0, bitmap.getWidth(), bitmap.getHeight(), 5, 5, true), paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
if (output.getWidth() > AppSharedPref.instance.getScreenWidth() * 2)
output = Bitmap.createScaledBitmap(output, output.getWidth() / 3, output.getWidth() / 3, false);
else
output = Bitmap.createScaledBitmap(output, output.getWidth() / 2, output.getWidth() / 2, false);
return output;
}
然后从另一篇文章中使用它: https://stackoverflow.com/a/28655800/4336375
public Path RoundedRect(float left, float top, float right, float bottom, float rx, float ry, boolean conformToOriginalPost) {
Path path = new Path();
if (rx < 0) rx = 0;
if (ry < 0) ry = 0;
float width = right - left;
float height = bottom - top;
if (rx > width / 2) rx = width / 2;
if (ry > height / 2) ry = height / 2;
float widthMinusCorners = (width - (2 * rx));
float heightMinusCorners = (height - (2 * ry));
path.moveTo(right, top + ry);
path.rQuadTo(0, -ry, -rx, -ry);//top-right corner
path.rLineTo(-widthMinusCorners, 0);
path.rQuadTo(-rx, 0, -rx, ry); //top-left corner
path.rLineTo(0, heightMinusCorners);
if (conformToOriginalPost) {
path.rLineTo(0, ry);
path.rLineTo(width, 0);
path.rLineTo(0, -ry);
} else {
path.rQuadTo(0, ry, rx, ry);//bottom-left corner
path.rLineTo(widthMinusCorners, 0);
path.rQuadTo(rx, 0, rx, -ry); //bottom-right corner
}
path.rLineTo(0, -heightMinusCorners);
path.close();//Given close, last lineto can be removed.
return path;
}
修改代码,根据要求将图像裁剪为圆角矩形。
答案 2 :(得分:0)
可以使用drawable
来设置图像背景。
可绘制左上角和左下角的圆角。
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/darker_gray" />
<corners
android:bottomLeftRadius="6dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="6dp"
android:topRightRadius="0dp" />
可绘制右上角和右下角的圆角。
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/darker_gray" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="6dp"
android:topLeftRadius="0dp"
android:topRightRadius="6dp" />
可以在recyclerView viewHolder中以编程方式更改图像的背景。