Android Programming-the implementation of circular images

Source: Internet
Author: User

In Android, Google only provides a circular operation of the picture, but does not implement a circular operation on the picture, so we do not do this, we will use the framework for the design (the following framework for the as written):

Https://github.com/monsterLin/RoundedImageView

Https://github.com/pungrue26/SelectableRoundedImageView
Https://github.com/hdodenhof/CircleImageView
Https://github.com/MostafaGazar/CustomShapeImageView
Https://github.com/siyamed/android-shape-imageview

Here we do this by using Roundedimageview:

First we import the open source framework into the project:

After the import is successful-

The first way: Write the XML file directly:

<Com.makeramen.roundedimageview.RoundedImageViewXmlns:app= "Http://schemas.android.com/apk/res-auto"Android:id= "@+id/imageview1"Android:layout_width= "40DP"Android:layout_height= "40DP"android:src= "@drawable/photo"App:riv_corner_radius= "30dip"App:riv_border_width= "2dip"App:riv_border_color= "#333333"App:riv_mutate_background= "true"App:riv_oval= "true" />

The second way: implemented by code:

 Public Static classRoundeddrawableextendsdrawable {protected Final floatCornerradius; protected Final intmargin; protected FinalRECTF Mrect =NewRECTF (), Mbitmaprect; protected FinalBitmapshader Bitmapshader; protected FinalPaint paint;  PublicRoundeddrawable (Bitmap Bitmap,intCornerradius,intmargin) {             This. Cornerradius =Cornerradius;  This. Margin =margin; Bitmapshader=NewBitmapshader (Bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); Mbitmaprect=NewRECTF (margin, margin, bitmap.getwidth ()-Margin, bitmap.getheight ()-margin); Paint=NewPaint (); Paint.setantialias (true);        Paint.setshader (Bitmapshader); } @Overrideprotected voidOnboundschange (Rect bounds) {Super. Onboundschange (bounds); Mrect.set (margin, margin, bounds.width ()-Margin, bounds.height ()-margin); //Resize the original bitmap to fit the new boundMatrix Shadermatrix =NewMatrix ();            Shadermatrix.setrecttorect (Mbitmaprect, Mrect, Matrix.ScaleToFit.FILL);                    Bitmapshader.setlocalmatrix (Shadermatrix); } @Override Public voidDraw (canvas canvas) {canvas.drawroundrect (Mrect, Cornerradius, Cornerradius, paint); } @Override Public intgetopacity () {returnpixelformat.translucent; } @Override Public voidSetalpha (intAlpha)        {Paint.setalpha (alpha); } @Override Public voidSetcolorfilter (Colorfilter CF) {paint.setcolorfilter (CF); }}imageaware.setimagedrawable (NewRoundeddrawable (bitmap, Cornerradius, margin));

Appendix:

Open source framework for Eclipse: Http://files.cnblogs.com/files/boy1025/roundedimageview.zip

Android Programming-the implementation of circular images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.