How to create a circle of a clip from an image?

Imagine that I have a rectangular image. How to create a style like the following?

enter image description hereenter image description here

I mean, by cropping the image in a circle, add a border, shadow and gross / glitter effect. So far, I only tried this snippet code to crop the image: Cropping a circular area from a bitmap in Android , but only that. I do not know how to make other components in Android.

+6
source share
1 answer

An easy way to achieve this effect is to use Canvas.drawCircle() and BitmapShader :

 BitmapShader s = new BitmapShader(myPhoto, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); Paint p = new Paint(); p.setShader(s); myCanvas.drawCircle(centerX, centerY, radius, p); 

To make a shadow, simply draw Paint.setShadowLayer() on the paint (this will work only if you draw the effect on an off-screen Bitmap or if your View uses a program level - set when calling View.setLayerType() -).

You can draw a border by drawing another circle on top using the Paint.Style.STROKE style (which you can set by calling Paint.setStyle() ).

Finally, you can draw a sparkle by drawing a circle, oval, or Path on top of your very first circle. You will need the LinearGradient shader on your paint, and you will also need to fix the shine. You can do this in two ways:

  • If you paint the whole effect in Bitmap , which I would recommend, just set the Xfermode paint to new PorterDuffXfermode(PorterDuff.Mode.SRC_IN) .
  • If you draw an effect directly on the screen, you can simply use Canvas.clipPath() to set the circular clip. Please note that this will only work with hardware acceleration with Android 4.3.
+16
source

Source: https://habr.com/ru/post/953257/


All Articles