Intro:
Photos taken, software cut-off figures are generally presented in rectangular form, but many times Android developers need to make a circular image as a user's avatar.
There are two ways to choose,
1) Use the Roundedbitmapdrawable.java class provided by the ANDROIDSDK to set the fillet angle to the drawable, and you can create a circular effect.
2) Circleimageview is a third-party open source project on GitHub that provides a professional approach to circular images.
The difference between the two is as follows:
1) The roundedbitmapdrawable is rounded by setting the fillet angle, so it produces a rounded effect in addition to a positive circle effect. This, circleimageview do not.
2) Roundedbitmapdrawable cannot directly define the border effect (thickness, color, etc.), but Circleimageview can.
Example:roundedbitmapdrawable:
In Layout.xml:
1 <ImageView2 android:id= "@+id/imageview3"3 android:layout_width = "Wrap_content"4 android:layout_height= "Wrap_content"5 android:padding= "5DP"/>
Java code: Please focus on the Red Note section;
1 PackageCom.example.myroundimageview;2 3 ImportAndroid.graphics.Bitmap;4 Importandroid.graphics.BitmapFactory;5 ImportAndroid.os.Bundle;6 Importandroid.support.v4.graphics.drawable.RoundedBitmapDrawable;7 Importandroid.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;8 Importandroid.support.v7.app.AppCompatActivity;9 ImportAndroid.widget.ImageView;Ten One Public classMainactivityextendsappcompatactivity { A - @Override - protected voidonCreate (Bundle savedinstancestate) { the Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.layout); - -Bitmap src = bitmapfactory.decoderesource (getresources (), r.drawable.me);///1, Bitmap object for obtaining picture resources +Bitmap DST;//2. Cut the rectangle picture into a square picture - if(Src.getwidth () >= src.getheight ()) {//the so-called clipping, is to use the bitmap create method, specify the width of the high and the source bitmap +DST = Bitmap.createbitmap (src, src.getwidth ()/2-src.getheight ()/2, 0, Src.getheight (), Src.getheight ()); A}Else { atDST = bitmap.createbitmap (src, 0, src.getheight ()/2-src.getwidth ()/2, Src.getwidth (), Src.getwidth ()); - } - //In this way, a square figure is obtained. -Roundedbitmapdrawable roundedbitmapdrawable = Roundedbitmapdrawablefactory.create (GetResources (), DST);//3. Enable roundedbitmapdrawable to set fillets and antialiasing -Roundedbitmapdrawable.setcornerradius (Dst.getwidth ()/2);//set the fillet radius to half the length of the square side -Roundedbitmapdrawable.setantialias (true);//the clipping of a picture is usually caused by aliasing, where antialiasing is set inImageView Image3 = (ImageView) Findviewbyid (R.ID.IMAGEVIEW3);//4. Set the Drawable object after processing to ImageView - image3.setimagedrawable (roundedbitmapdrawable); to } +}
Circleimageview:
The library that references it in Androidstudio:
Dependencies { ... . ' de.hdodenhof:circleimageview:2.2.0 '}
In the Layout.xml:
<de.hdodenhof.circleimageview.circleimageview xmlns:app= "Http://schemas.android.com/apk/res-auto"
android:id= "@+id/profile_image"
android:layout_width= "96DP"
android:layout_height= "96DP"
android:src= "@drawable/itsme"
app:civ_border_color= "@android: Color/holo_green_dark"
app:civ_border_overlay= "true"
app:civ_border_width= "2DP"
app:civ_circle_background_color= "@color/colorprimarydark"
app:civ_fill_color= "@color/coloraccent"/>
As for the internal code analysis of this circleimageview, leave a position here and fill in the back.
······
Android-Round Head