Android 自定义Dialog 实例
程序员文章站
2024-03-05 15:52:55
开发中经常需要请求网络获取数据,我们在请求网络到得到数据时当中需要等待一些时间,为了增加用户体验,我们一般会用一个dialog来提示用户我们在加载网络数据。
今天我们来实...
开发中经常需要请求网络获取数据,我们在请求网络到得到数据时当中需要等待一些时间,为了增加用户体验,我们一般会用一个dialog来提示用户我们在加载网络数据。
今天我们来实现如下效果的加载中dialog。
从图中我们可以看到要这个dialog是图片还有文字组成的,(不过我这里使用代码实现的,没有用图片),以下是这个加载图形的代码:
public class lvcircularring extends view { private float mwidth = 0f; private float mpadding = 0f; private float startangle = 0f; private paint mpaint; public lvcircularring(context context) { this(context, null); } public lvcircularring(context context, attributeset attrs) { this(context, attrs, 0); } public lvcircularring(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); initpaint(); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); if (getmeasuredwidth() > getheight()) mwidth = getmeasuredheight(); else mwidth = getmeasuredwidth(); mpadding = 5; } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); mpaint.setcolor(color.argb(100, 255, 255, 255)); canvas.drawcircle(mwidth / 2, mwidth / 2, mwidth / 2 - mpadding, mpaint); mpaint.setcolor(color.white); rectf rectf = new rectf(mpadding, mpadding, mwidth - mpadding, mwidth - mpadding); canvas.drawarc(rectf, startangle, 100 , false, mpaint);//第四个参数是否显示半径 } private void initpaint() { mpaint = new paint(); mpaint.setantialias(true); mpaint.setstyle(paint.style.stroke); mpaint.setcolor(color.white); mpaint.setstrokewidth(8); } public void startanim() { stopanim(); startviewanim(0f, 1f, 1000); } public void stopanim() { if (valueanimator != null) { clearanimation(); valueanimator.setrepeatcount(1); valueanimator.cancel(); valueanimator.end(); } } valueanimator valueanimator; private valueanimator startviewanim(float startf, final float endf, long time) { valueanimator = valueanimator.offloat(startf, endf); valueanimator.setduration(time); valueanimator.setinterpolator(new linearinterpolator()); valueanimator.setrepeatcount(valueanimator.infinite);//无限循环 valueanimator.setrepeatmode(valueanimator.restart);// valueanimator.addupdatelistener(new valueanimator.animatorupdatelistener() { @override public void onanimationupdate(valueanimator valueanimator) { float value = (float) valueanimator.getanimatedvalue(); startangle = 360 * value; invalidate(); } }); valueanimator.addlistener(new animatorlisteneradapter() { @override public void onanimationend(animator animation) { super.onanimationend(animation); } }); if (!valueanimator.isrunning()) { valueanimator.start(); } return valueanimator; } }
dialog 代码:
public class loadingdialog { lvcircularring mloadingview; dialog mloadingdialog; public loadingdialog(context context,string msg) { // 首先得到整个view view view = layoutinflater.from(context).inflate( r.layout.loading_dialog_view, null); // 获取整个布局 linearlayout layout = (linearlayout) view.findviewbyid(r.id.dialog_view); // 页面中的loadingview mloadingview = (lvcircularring) view.findviewbyid(r.id.lv_circularring); // 页面中显示文本 textview loadingtext = (textview) view.findviewbyid(r.id.loading_text); // 显示文本 loadingtext.settext(msg); // 创建自定义样式的dialog mloadingdialog = new dialog(context, r.style.loading_dialog); // 设置返回键无效 mloadingdialog.setcancelable(false); mloadingdialog.setcontentview(layout, new linearlayout.layoutparams( linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent)); } public void show(){ mloadingdialog.show(); mloadingview.startanim(); } public void close(){ if (mloadingdialog!=null) { mloadingview.stopanim(); mloadingdialog.dismiss(); mloadingdialog=null; } } }
布局文件loading_dialog_view 代码:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dialog_view" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="@drawable/dialog_bg" android:padding="20dp" android:orientation="vertical"> <com.ye.daqiapp.ui.widget.loading.lvcircularring android:id="@+id/lv_circularring" android:layout_width="50dp" android:layout_height="50dp"/> <textview android:id="@+id/loading_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#ffffff" android:layout_margintop="5dp" android:textsize="15sp"/> </linearlayout>
dialog中style代码:
<style name="loading_dialog" parent="android:style/theme.dialog"> <item name="android:windowframe">@null</item> <item name="android:windownotitle">true</item> <item name="android:windowbackground">@android:color/transparent</item> <item name="android:windowisfloating">true</item> <item name="android:backgrounddimenabled">false</item> <item name="android:windowcontentoverlay">@null</item> </style>
背景dialog_bg 代码:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 内部颜色 --> <solid android:color="#444444" /> <!-- 圆角的幅度 --> <corners android:bottomleftradius="3dp" android:bottomrightradius="3dp" android:topleftradius="3dp" android:toprightradius="3dp" /> </shape>
如何使用:在需要使用的地方初始化dialog:
loadingdialog dialog=new loadingdialog(context,"玩命加载中..."); //显示dialog dialog.show(); //关闭dialog dialog.close();
以上是对android dialog 重写的小示例,有需要的朋友可以参考下。
上一篇: Android仿新浪微博、QQ空间等帖子显示(1)
下一篇: java中如何反射获取一个类