简体   繁体   English

如何将左下角和右上角设置为四舍五入到我的图像视图?

[英]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.

相关问题 如何制作左上圆角和左下圆角的形状? - How to make a shape with left-top round rounded corner and left-bottom rounded corner? 如何在android的左上角和右下角创建圆角半径? - How to create corner radius in top left and bottom right in android? 如何找到左,右,上,下视图 - how to find left,right,top,bottom View 您好,我想将卫星菜单从屏幕的右上角移到屏幕的左下角 - hello,I would like to move my satellite menu from top right corner to the bottom left corner of the screen 如何在Android的自定义圆角图像视图中设置图像 - How to set image in custom rounded corner image view in android 具有令人敬畏的字体和圆角左上角和底部边框的按钮 - Button with font awesome and rounded top left corner and border at bottom Flutter 仅顶部、右侧、左侧边框,右上角和左上角为圆角 - Flutter only top, right, left border with rounded top right and top left corner 我如何将图像设置为活动的左下角和背景 - How can i set an image to be in the bottom left corner of an activity AND in the background 如何在linearlayout中将图像设置在左上角位置? - how to set image in top left corner position in linearlayout? 如何使用圆角顶部和底部矩形创建EditText - How to create a EditText with the top corner rounded and the bottom rectangle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM