ImageView subclass to make Rounded Right Corner
Asked Answered
S

1

0

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;
}
Staysail answered 11/2, 2014 at 17:53 Comment(2)
Why subclass? You can achieve same thing via XMLConfucius
how would I be able to crop and round an image with xml? I am not looking to simply make a circle.Staysail
D
0

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

Danyelledanyette answered 11/2, 2014 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.