Imagine that I have a rectangle image. How could I create a style like the next one?
I mean, cropping the image into a circle, add the border, the shadow and the gross /shine effect. Until now, I only have tried this snippet code to crop the image: Cropping circular area from bitmap in Android but just that. I have no idea how to do the remaining components in Android.
An easy way to achieve this effect is to use Canvas.drawCircle()
and a 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 do the shadow, simply call Paint.setShadowLayer()
on the paint (this will only work if you draw the effect into an offscreen Bitmap
or if your View
uses a software layer – set by calling View.setLayerType()
–).
The border can be drawn by drawing another circle on top, using the Paint.Style.STROKE
style (that you can set by calling Paint.setStyle()
).
Finally you can draw the gloss by drawing a circle, oval or Path
on top of your very first circle. You'll need to use a LinearGradient
shader on your paint and you'll also need to clip the gloss. You can do this in two ways:
Bitmap
, which is what I would recommend, simply set the paint's Xfermode
to a new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
.Canvas.clipPath()
to set a circular clip. Note that this will work with hardware acceleration only as of Android 4.3.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With