Android picture fillet conversion Roundedimageview Open source Project Little notes

Source: Internet
Author: User

How Android quickly transforms pictures into rounded corners

Using Open source Projects Roundedimageview

The official address of the open source project above GitHub is: Https://github.com/vinc3m1/RoundedImageView


The effect is as follows:

The following quick integration comes in

Steps are divided into 3

1: Go to GitHub to download the project

Https://github.com/vinc3m1/RoundedImageView


2: Import Project



3 Use it in layouts

    <com.makeramen.rounded.roundedimageview        android:id= "@+id/imageview1"        android:layout_width= "Wrap_ Content "        android:layout_height=" wrap_content "        android:layout_alignparenttop=" true "        android:layout_ Alignparentbottom= "true"        android:layout_alignparentright= "true"        android:layout_alignparentleft= "true"        android:padding= "10dip"        android:src= "@drawable/photo1"        android:scaletype= "center"        makeramen : corner_radius= "30dip"        makeramen:border_width= "2dip"        makeramen:border_color= "#333333"/>





The meaning of the attribute:

makeramen:border_width= "2dip" indicates a picture with a border width of 2 DP

Makeramen:corner_radius represents the radian of the picture's rounded corners

Modify makeramen:corner_radius= "100dip"

When Corner_radius is set to 100DP, it is rendered as a circle.

(Note: The width and height of the Com.makeramen.rounded.RoundedImageView control need to be equal)


Modified the Rounded_item.xml in the example


<?xml version= "1.0" encoding= "Utf-8"?><!--Copyright (C) to make Ramen, Llc--><relativelayout Xmlns:an Droid= "Http://schemas.android.com/apk/res/android" xmlns:makeramen= "http://schemas.android.com/apk/res/ Com.makeramen.rounded.example "android:layout_width=" match_parent "android:layout_height=" 200dip "> <com . Makeramen.rounded.RoundedImageView android:id= "@+id/imageview1" android:layout_width= "200DP" Android : layout_height= "200DP" android:padding= "10dip" android:src= "@drawable/photo1" android:scaletype= "cent Er "makeramen:corner_radius=" 100DP "makeramen:border_width=" 2dip "makeramen:border_color=" #333333 "/& Gt;<textview android:id= "@+id/textview3" android:layout_width= "wrap_content" android:layout_height= "Wrap_c Ontent "android:layout_marginbottom=" 36DP "android:layout_marginleft=" 36DP "Android:layout_alignbotto M= "@+id/imageview1" android:layout_alignleft= "@+id/imageview1" android:background= "#7f000000" android:paddingleft= "8DP" Android:paddin gright= "8DP" android:textappearance= "Android:attr/textappearancesmallinverse"/> <textview Android : id= "@+id/textview2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android        : layout_above= "@+id/textview3" android:layout_alignleft= "@+id/textview3" android:layout_marginbottom= "4DP" Android:background= "#7f000000" android:paddingleft= "8DP" android:paddingright= "8DP" Android:tex Tappearance= "? android:attr/textappearancemediuminverse"/> <textview android:id= "@+id/textView1" an         Droid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_above= "@+id/textView2" android:layout_alignleft= "@+id/textview2" android:layout_marginbottom= "4DP" android:background= "#7f0 00000 "Android:paddingleft= "8DP" android:paddingright= "8DP" android:textappearance= "Android:attr/textappearancelargeinverse" /></relativelayout>

Such as:



Create with code:

Roundedimageview IV = new Roundedimageview (context); Iv.setscaletype (Scaletype.center_crop); Iv.setcornerradius (10); Iv.setborderwidth (2); Iv.setbordercolor (Color.dkgray); Iv.setroundedbackground (true); Iv.setimagedrawable ( drawable); Iv.setbackground (backgrounddrawable); Iv.setoval (true);


Paste part of the source code:

Package Com.makeramen;import Android.content.context;import Android.content.res.colorstatelist;import Android.content.res.resources;import Android.content.res.typedarray;import Android.graphics.bitmap;import Android.graphics.drawable.drawable;import Android.graphics.drawable.layerdrawable;import Android.net.Uri;import Android.util.attributeset;import android.util.log;import Android.widget.ImageView; @SuppressWarnings ("   Unuseddeclaration ") public class Roundedimageview extends ImageView {public static final String TAG =" Roundedimageview ";  public static final Float Default_radius = 0f;  public static final Float default_border_width = 0f;      private static final scaletype[] Scale_types = {Scaletype.matrix, scaletype.fit_xy, Scaletype.fit_start, Scaletype.fit_center, Scaletype.fit_end, Scaletype.center, Scaletype.center_crop, Scaletype.center  _inside};  private float Cornerradius = Default_radius; private float borderWidth = default_border_width; Private Colorstatelist bordercolor = colorstatelist.valueof (Roundeddrawable.default_border_color);  Private Boolean isoval = false;  Private Boolean mutatebackground = false;  private int mresource;  Private drawable mdrawable;  Private drawable mbackgrounddrawable;  Private ScaleType Mscaletype;  Public Roundedimageview (Context context) {super (context);  } public Roundedimageview (context context, AttributeSet Attrs) {This (context, attrs, 0);    } public Roundedimageview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);    TypedArray a = Context.obtainstyledattributes (Attrs, R.styleable.roundedimageview, Defstyle, 0);    int index = A.getint (R.styleable.roundedimageview_android_scaletype,-1);    if (index >= 0) {setscaletype (Scale_types[index]);    } else {//default ScaleType to Fit_center Setscaletype (scaletype.fit_center); } Cornerradius = A.getdimensionpixelsize (R.styleable.roundedimageview_corner_radiuS,-1);    BorderWidth = A.getdimensionpixelsize (R.styleable.roundedimageview_border_width,-1);    Don ' t allow negative values for radius and border if (Cornerradius < 0) {Cornerradius = Default_radius;    } if (BorderWidth < 0) {borderWidth = Default_border_width;    } bordercolor = A.getcolorstatelist (R.styleable.roundedimageview_border_color);    if (bordercolor = = null) {bordercolor = colorstatelist.valueof (Roundeddrawable.default_border_color);    } Mutatebackground = A.getboolean (R.styleable.roundedimageview_mutate_background, false);    Isoval = A.getboolean (R.styleable.roundedimageview_oval, false);    Updatedrawableattrs ();    Updatebackgrounddrawableattrs (TRUE);  A.recycle ();    } @Override protected void drawablestatechanged () {super.drawablestatechanged ();  Invalidate ();   }/** * Return the current scale type ' in ' by ' this ImageView. * * @attr ref Android. R.styleable#imageview_scaletype * @see Android.widget.ImageVieW.scaletype */@Override public scaletype Getscaletype () {return mscaletype;   }/** * Controls How the image should is resized or moved to match the size * of this ImageView.   * * @param scaletype the desired scaling mode. * @attr ref Android. R.styleable#imageview_scaletype */@Override public void Setscaletype (ScaleType scaletype) {assert scaletype! = Nu    ll      if (mscaletype! = scaletype) {mscaletype = ScaleType;        Switch (scaletype) {case Center:case center_crop:case center_inside:case fit_center:          Case Fit_start:case fit_end:case FIT_XY:super.setScaleType (SCALETYPE.FIT_XY);        Break          Default:super.setScaleType (ScaleType);      Break      } updatedrawableattrs ();      Updatebackgrounddrawableattrs (FALSE);    Invalidate ();    }} @Override public void setimagedrawable (drawable drawable) {mresource = 0; Mdrawable = Roundeddrawable.fromdrawable (drawable);    Updatedrawableattrs ();  Super.setimagedrawable (mdrawable);    } @Override public void Setimagebitmap (Bitmap bm) {mresource = 0;    Mdrawable = Roundeddrawable.frombitmap (BM);    Updatedrawableattrs ();  Super.setimagedrawable (mdrawable);      } @Override public void Setimageresource (int resId) {if (Mresource! = resId) {mresource = ResId;      mdrawable = ResolveResource ();      Updatedrawableattrs ();    Super.setimagedrawable (mdrawable);    }} @Override public void Setimageuri (Uri uri) {Super.setimageuri (URI);  Setimagedrawable (Getdrawable ());    } private drawable ResolveResource () {Resources rsrc = getresources ();    if (rsrc = = null) {return null;}    Drawable d = null;      if (Mresource! = 0) {try {d = rsrc.getdrawable (Mresource);        } catch (Exception e) {LOG.W (TAG, "Unable to find resource:" + Mresource, E);        Don ' t try again.      Mresource = 0;  }} return roundeddrawable.fromdrawable (d);} @Override public void SetBackground (drawable background) {setbackgrounddrawable (background);  } private void Updatedrawableattrs () {updateattrs (mdrawable); } private void Updatebackgrounddrawableattrs (Boolean convert) {if (Mutatebackground) {if (convert) {mBa      ckgrounddrawable = roundeddrawable.fromdrawable (mbackgrounddrawable);    } updateattrs (mbackgrounddrawable);    }} private void Updateattrs (drawable drawable) {if (drawable = = null) {return;} if (drawable instanceof roundeddrawable) {((roundeddrawable) drawable). Setscaletype (Mscaletype). Setcornerradius (Cornerradius). Setborderwidth (BorderWidth). setBorderColor (bordercolor). Setova    L (Isoval); } else if (drawable instanceof layerdrawable) {//Loop through layers to and set drawable attrs layerdrawable l      D = ((layerdrawable) drawable); for (int i = 0, layers = ld.getnumberoflayers (); i < layers; i++) {UpDateattrs (Ld.getdrawable (i)); }}} @Override @Deprecated public void setbackgrounddrawable (drawable background) {mbackgrounddrawable = backg    Round    Updatebackgrounddrawableattrs (TRUE);  Super.setbackgrounddrawable (mbackgrounddrawable);  } public float Getcornerradius () {return cornerradius;  } public void Setcornerradius (int resId) {Setcornerradius (Getresources (). Getdimension (resId));    public void Setcornerradius (float radius) {if (Cornerradius = = radius) {return;}    Cornerradius = radius;    Updatedrawableattrs ();  Updatebackgrounddrawableattrs (FALSE);  } public float Getborderwidth () {return borderWidth;  } public void Setborderwidth (int resId) {setborderwidth (Getresources (). Getdimension (resId));    } public void Setborderwidth (float width) {if (borderWidth = = width) {return;}    BorderWidth = width;    Updatedrawableattrs ();    Updatebackgrounddrawableattrs (FALSE);  Invalidate (); } public int Getbordercolor () {returnBordercolor.getdefaultcolor ();  public void setBorderColor (int color) {setBorderColor (colorstatelist.valueof (color));  } public colorstatelist Getbordercolors () {return bordercolor;    public void setBorderColor (colorstatelist colors) {if (Bordercolor.equals (colors)) {return;} BorderColor = (colors! = NULL)?    Colors:ColorStateList.valueOf (Roundeddrawable.default_border_color);    Updatedrawableattrs ();    Updatebackgrounddrawableattrs (FALSE);    if (BorderWidth > 0) {invalidate ();  }} public Boolean isoval () {return isoval;    } public void Setoval (Boolean oval) {isoval = oval;    Updatedrawableattrs ();    Updatebackgrounddrawableattrs (FALSE);  Invalidate ();  } public boolean Ismutatebackground () {return mutatebackground;    public void Setmutatebackground (Boolean mutate) {if (Mutatebackground = = mutate) {return;}    Mutatebackground = mutate;    Updatebackgrounddrawableattrs (TRUE);  Invalidate (); }}

Xiao Kee: too convenient to really thank the open source

Daily diligence a little bit thank you



Android picture fillet conversion Roundedimageview Open source Project Little notes

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.