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 set the fillet form of the picture with Android

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

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "Android how to achieve picture fillet form", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Android how to achieve picture fillet form" bar!

1. Custom picture fillet form CircleImageView class

Public class CircleImageView extends ImageView {private static final Xfermode MASK_XFERMODE; private Bitmap mask; private Paint paint; private int mBorderWidth = 10; private int mBorderColor = Color.parseColor ("# f2f2f2"); private boolean useDefaultStyle = false; static {PorterDuff.Mode localMode = PorterDuff.Mode.DST_IN; MASK_XFERMODE = new PorterDuffXfermode (localMode);} public CircleImageView (Context context) {super (context) } public CircleImageView (Context context, AttributeSet attrs) {this (context, attrs, 0);} public CircleImageView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes (attrs, R.styleable.CircularImage); mBorderColor = a.getColor (R.styleable.CircularImage_border_color, mBorderColor) Final int def = (int) (2 * context.getResources (). GetDisplayMetrics (). Density + 0.5f); mBorderWidth = a.getDimensionPixelOffset (R.styleable.CircularImage_border_width, def); a.recycle ();} private void useDefaultStyle (boolean useDefaultStyle) {this.useDefaultStyle = useDefaultStyle;} @ Override protected void onDraw (Canvas canvas) {if (useDefaultStyle) {super.onDraw (canvas) Return;} final Drawable localDraw = getDrawable (); if (localDraw = = null) {return;} if (localDraw instanceof NinePatchDrawable) {return;} if (this.paint = = null) {final Paint localPaint = new Paint (); localPaint.setFilterBitmap (false); localPaint.setAntiAlias (true) LocalPaint.setXfermode (MASK_XFERMODE); this.paint = localPaint;} final int width = getWidth (); final int height = getHeight (); / * * Save layer * / int layer = canvas.saveLayer (0.0F, 0.0F, width, height, null, 31); / * * set drawable size * / localDraw.setBounds (0,0, width, height) / * * bind drawable to bitmap (this.mask) (drawable can only be displayed through bitmap) * / localDraw.draw (canvas); if ((this.mask = = null) | | (this.mask.isRecycled () {this.mask = createOvalBitmap (width, height) } / * * draw bitmap onto canvas * / canvas.drawBitmap (this.mask, 0.0F, 0.0F, this.paint); / * * copy canvas to layer * / canvas.restoreToCount (layer); drawBorder (canvas, width, height) Draw a circular border * / private void drawBorder (Canvas canvas, final int width, final int height) {if (mBorderWidth = = 0) {return;} final Paint mBorderPaint = new Paint (); mBorderPaint.setStyle (Paint.Style.STROKE); mBorderPaint.setAntiAlias (true); mBorderPaint.setColor (mBorderColor); mBorderPaint.setStrokeWidth (mBorderWidth) Canvas.drawCircle (width / 2, height / 2, (width-mBorderWidth) / 2, mBorderPaint); canvas = null;} public Bitmap createOvalBitmap (final int width, final int height) {Bitmap.Config localConfig = Bitmap.Config.ARGB_8888; Bitmap localBitmap = Bitmap.createBitmap (width, height, localConfig); Canvas localCanvas = new Canvas (localBitmap); Paint localPaint = new Paint (); final int padding = (mBorderWidth-3) > 0? MBorderWidth-3: 1; / * set the size of the ellipse (because the outermost edge of the ellipse coincides with the outermost edge of the border, if the outermost edge of the image is very dark, it has the effect of seeing edges, so to make the experience better, * indent it to padding px) * / RectF localRectF = new RectF (padding, padding, width-padding, height-padding) LocalCanvas.drawOval (localRectF, localPaint); return localBitmap;}}

1.1 create a circle_attr.xml under the values directory with the contents of the file:

As shown below:

2.activity_main.xml

3.MainActivity.java

Public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main);}}

The result after running:

Thank you for your reading, the above is "Android how to achieve picture fillet form" of the content, after the study of this article, I believe you on Android how to achieve picture fillet form of this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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