Android 实现自定义圆形listview功能的实例代码
程序员文章站
2022-03-20 19:13:21
最近遇到一个需求需要圆形listview作为悬浮窗,费了九牛二虎之力终于开发出来了,特别有成就感,下面分享下案例,项目原因,只能分享一部分供大家参考1.有图有真相下面就来讲解下代码:1.自定义list...
最近遇到一个需求需要圆形listview作为悬浮窗,费了九牛二虎之力终于开发出来了,特别有成就感,下面分享下案例,项目原因,只能分享一部分供大家参考
1.有图有真相
下面就来讲解下代码:
1.自定义listview
import android.content.context; import android.content.res.typedarray; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.paint; import android.graphics.rect; import android.util.attributeset; import android.view.view; import android.view.viewgroup; public class circlelistview extends viewgroup { private static final double intervalangel = 60;//子view之间的间隔角 int circler;//圆的半径 int ccx;//圆心的x轴坐标 int ccy;//圆心的y轴坐标 double angel = 0;//偏移角度 private float oldtouchy;//上一次触摸的y轴位置 private boolean isscrolling = false;//是否在滑动状态 private bitmap circlebitmap = null; private rect src; private rect dst; private paint paint; private adapter adapter = new adapter(this) { @override public view getview(int position) { return new view(getcontext()); } }; public circlelistview(context context) { super(context); setwillnotdraw(false); paint = new paint(); } public circlelistview(context context, attributeset attrs) { super(context, attrs); setwillnotdraw(false); paint = new paint(); // typedarray ta = context.obtainstyledattributes(attrs, r.styleable.circlelistview); // setcirclebitmap(ta.getresourceid(r.styleable.circlelistview_circledrawable, 0)); // ta.recycle(); } private void setcirclebitmap(int drawableid) { if (drawableid != 0) { circlebitmap = bitmapfactory.decoderesource(getresources(), drawableid); } else { circlebitmap = null; } } public void setadapter(adapter adapter) { this.adapter = adapter; refreshlist(); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { measurechildren(widthmeasurespec, heightmeasurespec); super.onmeasure(widthmeasurespec, heightmeasurespec); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); if (circlebitmap != null) { if (src == null) { src = new rect(); } src.left = 0; src.top = 0; src.right = circlebitmap.getwidth(); src.bottom = circlebitmap.getheight(); if (dst == null) { dst = new rect(); } dst.left = ccx - circlebitmap.getwidth()/4; dst.top = ccy - circlebitmap.getheight()/4; dst.right = ccx + circlebitmap.getwidth()/4; dst.bottom = ccy + circlebitmap.getheight()/4; canvas.drawbitmap(circlebitmap, src, dst, paint); } } @override protected void onlayout(boolean changed, int l, int t, int r, int b) { circler = (getright() - getleft()) / 10 * 5; ccy = (int) (getheight() * 0.5); ccx = getwidth() / 2; for (int i = 0; i < adapter.getcount(); i++) { view childview = getchildat(i); double childviewangel = i * intervalangel + angel; int x = ccx + (int) (math.sin(math.toradians(childviewangel)) * circler*0.6); int y = ccy - (int) (math.cos(math.toradians(childviewangel)) * circler*0.6); int vl = x - childview.getmeasuredwidth() / 2; int vt = y - childview.getmeasuredheight() / 2; int vr = x + childview.getmeasuredwidth() / 2; int vb = y + childview.getmeasuredheight() / 2; childview.layout(vl, vt, vr, vb ); } } // @override // public boolean dispatchtouchevent(motionevent ev) { // switch (ev.getaction()) { // case motionevent.action_down: // oldtouchy = ev.gety(); // super.dispatchtouchevent(ev); // return true; // case motionevent.action_move: // if (!isscrolling && math.abs(oldtouchy - ev.gety()) > 50) { // isscrolling = true; // float offsety = 0; // oldtouchy = ev.gety(); // angel += offsety / 20; // requestlayout(); // return true; // } else if (isscrolling) { // float offsety = ev.gety() - oldtouchy; // oldtouchy = ev.gety(); // if ((angel + offsety / 20) < ((adapter.getcount() - 1) * -intervalangel)) { // angel = (adapter.getcount() - 1) * -intervalangel; // } else if ((angel + offsety / 20) > 0) { // angel = 0; // } else { // angel += offsety / 20; // } // requestlayout(); // return true; // } // return super.dispatchtouchevent(ev); // case motionevent.action_up: // boolean notdispatch = isscrolling; // isscrolling = false; // if (notdispatch) { // return false; // } else { // performclick(); // return super.dispatchtouchevent(ev); // } // default: // isscrolling = false; // return super.dispatchtouchevent(ev); // } // } protected void refreshlist() { removeallviews(); for (int i = 0; i < adapter.getcount(); i++) { if (i == 0 && angel < -intervalangel * (adapter.getcount() - 1)) { angel = -intervalangel * (adapter.getcount() - 1); } addview(adapter.getview(i)); if (adapter.getcount() == 1) { setposition(0); } } invalidate(); } protected void setposition(int position) { angel = -position * intervalangel; } }
2.实体类
import android.graphics.drawable.drawable; public class files { private int id; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getapp_name() { return app_name; } public void setapp_name(string app_name) { this.app_name = app_name; } public string getpackagename() { return packagename; } public void setpackagename(string packagename) { this.packagename = packagename; } public int getimg_bg_id() { return img_bg_id; } public void setimg_bg_id(int img_bg_id) { this.img_bg_id = img_bg_id; } public boolean isshow() { return isshow; } public void setshow(boolean show) { isshow = show; } private drawable imgdrawable; public drawable getimgdrawable() { return imgdrawable; } public void setimgdrawable(drawable imgdrawable) { this.imgdrawable = imgdrawable; } private boolean isshow; private string app_name; private string packagename; private int img_bg_id; }
3.适配器
import android.view.view; import java.util.arraylist; public abstract class adapter { private circlelistview circlelistview; private arraylist<files> allapplist; public adapter(circlelistview circlelistview) { this.circlelistview = circlelistview; } public int getcount() { return allapplist.size(); } public abstract view getview(int position); public void notifydatachanged() { circlelistview.refreshlist(); } public void setposition(int position) { if (position > getcount() - 1) { position = getcount() - 1; } else if (position < 0) { position = 0; } circlelistview.setposition(position); } }
4.float.xml 引用:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <imageview android:id="@+id/float_btn" android:layout_width="79px" android:layout_height="79px" android:scaletype="fitcenter" android:layout_centerinparent="true" android:background="@drawable/main_selector_floatbtn" android:clickable="true" /> <relativelayout android:id="@+id/float_area" android:layout_width="300px" android:layout_height="300px" android:layout_centerinparent="true" android:background="@drawable/float_display_bg" android:visibility="gone"> <com.xinrui.headsettest.circlelistview.circlelistview android:id="@+id/circle_list_view" android:layout_width="300px" android:layout_height="300px" android:layout_centerhorizontal="true"/> <imageview android:id="@+id/float_main_img" android:layout_width="70px" android:layout_height="70px" android:scaletype="fitcenter" android:layout_centerinparent="true" android:background="@drawable/float_main_icon" android:clickable="true"/> </relativelayout> </relativelayout>
5.activity中应用:
import android.app.activity; import android.os.bundle; import android.provider.contactscontract; import android.view.layoutinflater; import android.view.view; import android.widget.imageview; import android.widget.relativelayout; import android.widget.textview; import android.widget.toast; import com.xinrui.headsettest.circlelistview.adapter; import com.xinrui.headsettest.circlelistview.circlelistview; import com.xinrui.headsettest.circlelistview.files; import java.util.arraylist; public class floatactivity extends activity implements view.onclicklistener{ private imageview floatimg, float_main_img; private relativelayout float_area; private circlelistview circle_list_view; private adapter adapter; private arraylist<files> appdatalist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.float_layout); getdata(); init(); } private void getdata(){ appdatalist=new arraylist<>(); files files = new files(); files.setapp_name(this.getresources().getstring(r.string.calculator_txt)); files.setimg_bg_id(r.drawable.main_selector_calculator); appdatalist.add(files); files files0 = new files(); files0.setapp_name(this.getresources().getstring(r.string.calendar_txt)); files0.setimg_bg_id(r.drawable.main_selector_calendar); appdatalist.add(files0); files files1 = new files(); files1.setapp_name(this.getresources().getstring(r.string.timer_txt)); files1.setimg_bg_id(r.drawable.main_selector_clock); appdatalist.add(files1); files files2 = new files(); files2.setapp_name(this.getresources().getstring(r.string.vip_txt)); files2.setimg_bg_id(r.drawable.main_selector_vip); appdatalist.add(files2); files files3 = new files(); files3.setapp_name(this.getresources().getstring(r.string.screen_shots_txt)); files3.setimg_bg_id(r.drawable.main_selector_screenshot); appdatalist.add(files3); files files4 = new files(); files4.setapp_name(this.getresources().getstring(r.string.recording_screen_txt)); files4.setimg_bg_id(r.drawable.main_selector_recordingscreen); appdatalist.add(files4); } private void init() { float_area = (relativelayout) findviewbyid(r.id.float_area); floatimg = (imageview) findviewbyid(r.id.float_btn); float_main_img = (imageview) findviewbyid(r.id.float_main_img); float_main_img.bringtofront(); floatimg.setonclicklistener(this); float_main_img.setonclicklistener(this); circle_list_view = findviewbyid(r.id.circle_list_view); adapter = new adapter(circle_list_view) { @override public view getview(final int position) { view view = layoutinflater.from(floatactivity.this).inflate(r.layout.item_app,null); textview app_name = view.findviewbyid(r.id.app_name); imageview app_img = view.findviewbyid(r.id.app_img); imageview delete_img = view.findviewbyid(r.id.delete_icon); delete_img.bringtofront(); final files files = appdatalist.get(position); app_name.settext(files.getapp_name()); app_img.setimageresource(files.getimg_bg_id()); if(files.isshow()){ delete_img.setvisibility(view.visible); }else{ delete_img.setvisibility(view.invisible); } delete_img.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { appdatalist.remove(position); files file = new files(); file.setshow(false); file.setimg_bg_id(r.drawable.main_selector_additem); file.setapp_name(floatactivity.this.getresources().getstring(r.string.custom_txt)); appdatalist.add(position,file); adapter.notifydatachanged(); } }); view.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(floatactivity.this,"my is "+files.getapp_name(),toast.length_short).show(); } }); view.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view v) { if(!files.getapp_name().equals(floatactivity.this.getresources().getstring(r.string.custom_txt))) { for (files file : appdatalist) { if (file.getapp_name().equals(files.getapp_name())) { file.setshow(true); } else { file.setshow(false); } } adapter.notifydatachanged(); } return true; } }); return view; } @override public int getcount() { return appdatalist.size(); } }; circle_list_view.setadapter(adapter); adapter.setposition(0); } @override public void onclick(view v) { switch (v.getid()) { case r.id.float_main_img: float_area.setvisibility(view.gone); floatimg.setvisibility(view.visible); break; case r.id.float_btn: float_area.setvisibility(view.visible); floatimg.setvisibility(view.gone); break; } } }
大功告成就这样实现了圆形listview…
到此这篇关于android 实现自定义圆形listview功能的实例代码的文章就介绍到这了,更多相关android 自定义圆形listview内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 通过法律手段起诉恶意营销号
下一篇: 安装CentOS--设置网络_2