How to apply half circle mask to an ImageView

妖精的绣舞 提交于 2019-12-30 07:15:14

问题


I have an image, a half circle frame image and I need to put that image inside this frame. But I need to apply mask to my image so it is only displayed inside frame.

For example this is my image:

And my desired result should be like that:

The red frame also an image view which inside is transparent.

How can I achieve this in Android?


回答1:


There's a great tutorial on Styling Android blog in four parts that explains how you can achieve this.

Edit:

I've edited the code in part two of the tutorial and created the effect:

private Bitmap processImage(Bitmap bitmap) {
    Bitmap bmp;

    bmp = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    BitmapShader shader = new BitmapShader(bitmap,
            BitmapShader.TileMode.CLAMP,
            BitmapShader.TileMode.CLAMP);

    float radius = bitmap.getWidth() / 2f;
    Canvas canvas = new Canvas(bmp);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);

    RectF rect = new RectF(-bitmap.getWidth() / 2f, 0,
            bitmap.getWidth() / 2f, bitmap.getHeight());
    canvas.drawOval(rect, paint);

    return bmp;
}

I just replaced the drawRoundRect at the end of the code with drawOval and it essentially draws a circle that half of it is out of canvas.



来源:https://stackoverflow.com/questions/32433691/how-to-apply-half-circle-mask-to-an-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!