[英]how to set bottom left and top right corner as rounded to my image view?
see the image corners.Please help请参阅图像角落。请帮助
i want to round bottom left and top left corners of my image rounded while setting it to image view我想将图像的左下角和左上角四舍五入,同时将其设置为图像视图
My code:我的代码:
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;
}
This xml will give you left top and bottom corner rounder.这个 xml 会给你左上角和下角的圆角。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="-20dp">
<shape android:shape="rectangle" >
<solid android:color="@android:color/holo_green_dark" />
<corners android:radius="20dp" />
</shape>
</item>
<item android:right="-20dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="3dp"
android:color="@android:color/holo_red_dark" />
<corners android:radius="20dp"/>
</shape>
</item>
</layer-list>
Modify and create your drawable as your requirement根据您的要求修改和创建您的可绘制对象
i have a used these two functions together :我一起使用了这两个功能:
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;
}
and then used this from another post : https://stackoverflow.com/a/28655800/4336375然后从另一篇文章中使用它: 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;
}
modify the code to crop your image into round rectangle as per requirement.修改代码以根据需要将图像裁剪为圆角矩形。
Can make use of drawable
for setting background for image.可以利用
drawable
来设置图像的背景。
drawable for top-left and bottom-left rounded corners.可绘制左上角和左下角的圆角。
<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" />
drawable for top-right and bottom-right rounded corners.可绘制右上角和右下角的圆角。
<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" />
Can change the background for image programatically within recyclerView viewHolder.可以在 recyclerView viewHolder 中以编程方式更改图像的背景。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.