Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the effect of Circular Picture in Android

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report