简体   繁体   中英

set image inside circle shaped layout and hide overlapping part

I'm using frame layout and I've added imageview into it. frame layout is circled shape. the imageview inside frame layout moves outside of that circle when it's size gets bigger than layout size. I want to hide that extra imageview. any help would be much appreciated.

Thanks :)

Try this function...

You need to provide your square bitmap file and output size of the circle image you want.

public static Bitmap getCroppedBitmap(Bitmap bitmap, int size) {
    bitmap = Bitmap.createScaledBitmap(bitmap, size, size, false);
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
           bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    int color = 0xff424242;
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
            bitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

Basically you should use canvas for this purpose.

Draw a circular canvas and add you image bitmap in it.

For your refs https://github.com/lopspower/CircularImageView

You can use this lib in your project

just add this in xml

<com.mikhaellopez.circularimageview.CircularImageView
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:src="@drawable/image"
        app:border_color="#EEEEEE"
        app:border_width="4dp"
        app:shadow="true" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM