Android Circular ListView is implemented and comes with rounded corners ImageView

Source: Internet
Author: User

Reprint please indicate the source: Wang 亟亟 's way of Daniel

Normally a vertical downward listview is commonplace, and a rounded corner with a certain angle to the ListView should give the user a different experience.

Project directory:

2 Custom View one main activity
Mainactivity

 Public  class mainactivity extends Activity {    PrivateListView LV;//Local image simulation data source    Private int[] Images =New int[] {r.drawable.p1, r.drawable.p2, r.drawable.p3};@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        LV = (ListView) Findviewbyid (r.id.lv); Lv.setadapter (NewMyadapter (Makedata ()));Try{changegroupflag (LV); }Catch(Exception e)        {E.printstacktrace (); }//Tumbling process is constantly drawnLv.setonscrolllistener (NewOnscrolllistener () {@Override             Public void onscrollstatechanged(Abslistview view,intscrollstate) {}@Override             Public void onscroll(Abslistview view,intFirstvisibleitem,intVisibleItemCount,intTotalitemcount) { for(inti =0; I < Lv.getchildcount ();                i++) {Lv.getchildat (i). Invalidate (); }            }        });//Listen for events and test if item shift occursLv.setonitemclicklistener (NewOnitemclicklistener () {@Override             Public void Onitemclick(adapterview<?> parent, view view,intPositionLongID) {Toast.maketext (mainactivity. This,"section"+position+"The one that was pressed.", Toast.length_short). Show ();    }        }); }//Custom AdapterClass Myadapter extends Baseadapter { Public Final  class viewholder{         PublicImageView ImageView; PublicTextView text; }//Incoming data source        PrivateList<string> data; Public Myadapter(list<string> data) { This. Data=data; }@Override         Public int GetCount() {returnData.size (); }@Override         PublicObjectGetItem(intPosition) {return NULL; }@Override         Public Long Getitemid(intPosition) {return 0; }@Override         PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {Viewholder Holder =NULL;if(Convertview = =NULL) {holder=NewViewholder (); MatrixView m = (matrixview) layoutinflater.from (mainactivity. This). Inflate (R.layout.view_list_item,NULL);                M.setparentheight (Lv.getheight ());                Convertview = m;                holder.text= (TextView) Convertview.findviewbyid (R.id.text);                Holder.imageview = (ImageView) Convertview.findviewbyid (r.id.image);              Convertview.settag (holder); }Else{holder = (Viewholder) convertview.gettag (); } holder.text.setText (Data.get (position) +""); Holder.imageView.setImageResource (images[position% images.length]);returnConvertview; }    } Public void Changegroupflag(Object obj)throwsexception//reflection substitution replaces all strings with {field[] f = obj.getclass (). Getsuperclass (). Getsuperclass (). Getsuperclass (). getde Claredfields ();//Get member Map array         for(Field tem:f)//Iteration for Loop{if(Tem.getname (). Equals ("Mgroupflags") {tem.setaccessible (true); Integer mgroupflags = (integer) tem.get (obj);//return content                intNewgroupflags = mgroupflags &0xfffff8; Tem.set (obj, newgroupflags);//Replace member values}        }    }PrivateList<string>Makedata() {list<string> data=NewArraylist<string> (); for(intk=0;k< -; k++) {Data.add ("Hello, Mai-mai."+K); }returnData }}

Custom rounded picture

 Public  class roundimageview extends ImageView {    Private floatXradius =Ten;Private floatYradius =Ten;PrivatePaint paint =NewPaint (); Public Roundimageview(Context context) {Super(context); } Public Roundimageview(context context, AttributeSet attrs) {Super(context, attrs); } Public float Getxradius() {returnXradius; } Public void Setxradius(floatXradius) { This. Xradius = Xradius; } Public float Getyradius() {returnYradius; } Public void Setyradius(floatYradius) { This. Yradius = Yradius; }@SuppressWarnings("Deprecation")@Override    protected void OnDraw(Canvas canvas) {//java.lang.ClassCastException:android.graphics.drawable.TransitionDrawable cannot be cast        //To Android.graphics.drawable.BitmapDrawableBitmapshader shader;if(Getdrawable ()instanceofbitmapdrawable) {bitmapdrawable bitmapdrawable = (bitmapdrawable) getdrawable ();//ClipShader =NewBitmapshader (Bitmapdrawable.getbitmap (), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); RECTF rect =NewRECTF (0.0F0.0F, GetWidth (), getheight ());intwidth = Bitmapdrawable.getbitmap (). GetWidth ();intHeight = Bitmapdrawable.getbitmap (). GetHeight (); RECTF src =NULL;if(((float) width)/height >1) {src =NewRECTF (0.0F0.0F, height, height); }Else{src =NewRECTF (0.0F0.0F, width, width);            } Matrix Matrix = Canvas.getmatrix ();            Matrix.setrecttorect (SRC, rect, Matrix.ScaleToFit.CENTER); Shader.setlocalmatrix (matrix);//anti-aliasingPaint.setantialias (true); Paint.setshader (shader);//Draw round circle for Headimage or otherCanvas.drawroundrect (Rect, This. GetWidth ()/2, This. GetHeight ()/2, paint);//Canvas.drawroundrect (rect, Xradius, YRADIUS/2, paint);}    }}

Custom Layouts

 Public  class matrixview extends linearlayout {    Private inth =0;Private floatFullangelfactor =TenFPrivate floatFullscalefactor=1; Public MatrixView(context context, AttributeSet attrs) {Super(context, attrs); } Public void Setparentheight(intHeight) {h = height; }@Override    protected void Dispatchdraw(Canvas canvas) {Canvas.save ();inttop = GetTop ();floatRotate = Calculateangel (top, h);floatScale = Calcuylatescale (top, h);        Matrix m = Canvas.getmatrix (); M.pretranslate (-2/GetWidth (),-2/GetHeight ());        M.postscale (scale, scale); M.posttranslate (2/GetWidth (),2/GetHeight ());        M.postrotate (rotate); Canvas.concat (m);Super. Dispatchdraw (canvas);    Canvas.restore (); }@Override    protected void onmeasure(intWidthmeasurespec,intHEIGHTMEASURESPEC) {Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); }Private float Calculateangel(intTopinth) {floatresult =0Fif(Top < H/2f) {result = (top-(H/2f))/(H/2f) * Fullangelfactor; }Else if(Top > H/2f) {result = (top-(H/2f))/(H/2f) * Fullangelfactor; }returnResult }Private float Calcuylatescale(intTopinth) {floatresult =0F result = (1F-1f/2F*math.abs ((Top-h/2f))/(H/2f)) * fullscalefactor;returnResult }}

Private float Fullangelfactor for the angle of rotation
Private float Fullscalefactor Scale to try to zoom out

When private float fullangelfactor=40f:

The main layout is subsidized, it's a ListView.

Each of the item

<?xml version= "1.0" encoding= "Utf-8"?><com.wjj.test.MatrixView xmlns:android="http://schemas.android.com/apk/res/android "android:layout_width="match_parent "android:layout_height="wrap_content "  android:orientation="Horizontal" >                <com.wjj.test.roundimageview  android:id  =  Android:layout_width  = "80DP"  android:layout_ Height  = "80DP"  android:layout_marginleft  = "30DP"  android:scaletype  = "Centercrop" />     <TextViewandroid:id= "@+id/text"android:layout_width="Match_ Parent "android:layout_height=" 80DP "android:gravity=" Left|center_vertical "  android:paddingleft="20DP"android:textsize="18sp" />                                                </com.wjj.test.MatrixView>

Concrete implementation can see Code, attached Source: Http://yunpan.cn/cmy9fi3fMDY7U Access password 0982

Some of the code is from the Internet, please forgive me if there is repetition

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android round ListView Implementation with rounded corners ImageView

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.