Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android trying to round the bitmap corners

I have an imageView and and I'm trying to create rounded corners, tried all of the solutions from this post: How to make an ImageView with rounded corners? But nothing worked.. Here's my XML

    <RelativeLayout
    android:id="@+id/RL_ImageHolder"
    android:layout_width="150dp"
    android:layout_height="180dp"
    android:layout_alignBottom="@+id/relativeLayout2"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/relativeLayout2"
    android:layout_marginLeft="10dp" >

    <ImageView
        android:id="@+id/imgPreview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:contentDescription="Preview"

         />
</RelativeLayout>

and that's how I set the ImageView using the method mentioned in the post I've linked above:

  byteArray = extras.getByteArray("picture");
    if (byteArray != null) {
        bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
        preview.setScaleType(ScaleType.CENTER_CROP);
        preview.setImageBitmap(getRoundedCornerBitmap(bmp));

The image is set correctly, but it keeps a rectangle.. Any ideas why it doesn't work?

EDIT: just found out that is doesn't work only when the layout holding the image has a fixed width/height.. Gotta think how to manage that. Thanks guys

like image 378
Droidman Avatar asked Dec 19 '12 09:12

Droidman


People also ask

How do you handle bitmaps in Android?

For most cases, we recommend that you use the Glide library to fetch, decode, and display bitmaps in your app. Glide abstracts out most of the complexity in handling these and other tasks related to working with bitmaps and other images on Android.


1 Answers

try with Below Code Snippet:

public static Bitmap GetCurveImage(Bitmap bitmap) {
        // Bitmap myCoolBitmap = ... ; // <-- Your bitmap you
        // want rounded
        int w = bitmap.getWidth(), h = bitmap.getHeight();

        // We have to make sure our rounded corners have an
        // alpha channel in most cases
        Bitmap rounder = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(rounder);

        // We're going to apply this paint eventually using a
        // porter-duff xfer mode.
        // This will allow us to only overwrite certain pixels.
        // RED is arbitrary. This
        // could be any color that was fully opaque (alpha =
        // 255)
        Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        xferPaint.setColor(Color.RED);

        // We're just reusing xferPaint to paint a normal
        // looking rounded box, the 20.f
        // is the amount we're rounding by.
        canvas.drawRoundRect(new RectF(0, 0, w, h), 5.0f, 5.0f, xferPaint);

        // Now we apply the 'magic sauce' to the paint
        xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

        Bitmap result = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas resultCanvas = new Canvas(result);
        resultCanvas.drawBitmap(bitmap, 0, 0, null);
        resultCanvas.drawBitmap(rounder, 0, 0, xferPaint);

        return result;
    } 

Hope it will Help you.

like image 147
Bhavesh Patadiya Avatar answered Oct 22 '22 18:10

Bhavesh Patadiya