Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the effect of Circular Picture in Android

Shulou Source: shulou.com Published: 2022-05-31 21:45:55 09月18日 Update

This article mainly explains "how to achieve circular picture effect in Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to achieve circular picture effect in Android"!

Create a RoundPicture.java file

Create a new RoundPicture.java under src/main/java/XX package

Write to RoundPicture.java file

Just copy the following code and introduce the class

Public class RoundPicture extends androidx.appcompat.widget.AppCompatImageView {private Paint paint; public RoundPicture (Context context) {this (context, null);} public RoundPicture (Context context, AttributeSet attrs) {this (context, attrs, 0);} public RoundPicture (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); paint = new Paint () } / / draw a circular picture @ Override protected void onDraw (Canvas canvas) {Drawable drawable = getDrawable (); if (null! = drawable) {Bitmap bitmap = ((BitmapDrawable) drawable) .getBitmap (); Bitmap b = getCircleBitmap (bitmap, 14); final Rect rectSrc = new Rect (0,0, b.getWidth (), b.getHeight ()) Final Rect rectDest = new Rect (0,0, getWidth (), getHeight ()); paint.reset (); canvas.drawBitmap (b, rectSrc, rectDest, paint);} else {super.onDraw (canvas) }} / / private Bitmap getCircleBitmap (Bitmap bitmap, int pixels) {Bitmap output = Bitmap.createBitmap (bitmap.getWidth (), bitmap.getHeight (), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas (output); final int color = 0xff424242; final Rect rect = new Rect (0,0, bitmap.getWidth (), bitmap.getHeight ()); paint.setAntiAlias (true) Canvas.drawARGB (0,0,0,0); paint.setColor (color); int x = bitmap.getWidth (); canvas.drawCircle (x / 2, paint); paint.setXfermode (new PorterDuffXfermode (PorterDuff.Mode.SRC_IN)); canvas.drawBitmap (bitmap, rect, rect, paint); return output;}}

Call RoundPicture to create a circular picture

Simply insert the picture in the .xml file and change the control name to

< XX.RoundPicture and introduce the picture.

At this point, I believe you have a deeper understanding of "how to achieve circular picture effect in Android". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Pictures circles effects files content methods learning practical deeper code interests just practical practical controls easy to operate more friends websites channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Huawei Redmi Shulou Tech Info Microsoft