ImageView subclass to make Rounded Right Corner

2019-08-30 00:28发布

I am trying to take an image like these

enter image description here enter image description here

and turn them into the top right 1/4 of the image. The image below is an example of what I need.

enter image description here

**UPDATE: **

I have it (sort of) working, but scaling the image creates an unacceptable amount of distortion my code seems terribly inefficient. Any ideas on how to improve this code or a possible better approach?

enter image description here

I am subclassing Volley's NetworkImageView.

Here is my code

<com.RoundedNetworkImageView
    android:id="@+id/smallProductImage"
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:layout_centerVertical="true"
    android:layout_margin="5dp"
    android:adjustViewBounds="true"
    android:background="@android:color/white "
    android:scaleType="fitCenter" />

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;

    if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
        float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
        float factor = smallest / radius;
        sbmp = Bitmap.createScaledBitmap(bmp, (int) (bmp.getWidth() / factor), (int) (bmp.getHeight() / factor),
                false);
    } else {
        sbmp = bmp;
    }

    Bitmap output = Bitmap.createBitmap(radius, radius, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, radius, radius);

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(radius / 2 + 0.7f, radius / 2 + 0.7f, radius / 2 + 0.1f, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);

    Bitmap bitmap = Bitmap.createBitmap(output, output.getWidth() / 2, 0, output.getWidth() / 2,
            output.getHeight() / 2);
    Bitmap scaledBmp = Bitmap.createScaledBitmap(bitmap, radius, radius, true);

    return scaledBmp;
}

3条回答
爷的心禁止访问
2楼-- · 2019-08-30 00:55

IMHO this is the simplest way, and you can totally customize the class for achieve your goals. https://github.com/memoryleak/Android-RoundedImageView

查看更多
家丑人穷心不美
3楼-- · 2019-08-30 01:03

Check this link. it has so many answers similar to what you are looking for and might help you. Happy coding. :)

查看更多
成全新的幸福
4楼-- · 2019-08-30 01:12
登录 后发表回答