Android实现自定义加载框的代码示例
程序员文章站
2023-12-22 14:11:04
app在与服务器进行网络交互的时候,需要有一个提示的加载框,如图:
此时我们可以自定义一个加载中的对话框,代码如下:
public class load...
app在与服务器进行网络交互的时候,需要有一个提示的加载框,如图:
此时我们可以自定义一个加载中的对话框,代码如下:
public class loadingdialog extends dialog { private static final int change_title_what = 1; private static final int chnage_title_delaymillis = 300; private static final int max_suffix_number = 3; private static final char suffix = '.'; private imageview iv_route; private textview detail_tv; private textview tv_point; private rotateanimation manim; private handler handler = new handler() { private int num = 0; public void handlemessage(android.os.message msg) { if (msg.what == change_title_what) { stringbuilder builder = new stringbuilder(); if (num >= max_suffix_number) { num = 0; } num++; for (int i = 0; i < num; i++) { builder.append(suffix); } tv_point.settext(builder.tostring()); if (isshowing()) { handler.sendemptymessagedelayed(change_title_what, chnage_title_delaymillis); } else { num = 0; } } }; }; public loadingdialog(context context) { super(context, r.style.dialog_bocop); init(); } public loadingdialog(context context, boolean istrans) { super(context, istrans ? r.style.loading_dialog_trans : r.style.dialog_bocop); init(); } private void init() { setcontentview(r.layout.common_dialog_loading_layout); iv_route = (imageview) findviewbyid(r.id.iv_route); detail_tv = (textview) findviewbyid(r.id.detail_tv); tv_point = (textview) findviewbyid(r.id.tv_point); initanim(); getwindow().setwindowanimations(r.anim.alpha_in); } private void initanim() { // manim = new rotateanimation(360, 0, animation.restart, 0.5f, animation.restart, 0.5f); manim = new rotateanimation(0, 360, animation.restart, 0.5f, animation.restart, 0.5f); manim.setduration(2000); manim.setrepeatcount(animation.infinite); manim.setrepeatmode(animation.restart); manim.setstarttime(animation.start_on_first_frame); } @override public void show() {//在要用到的地方调用这个方法 iv_route.startanimation(manim); handler.sendemptymessage(change_title_what); super.show(); } @override public void dismiss() { manim.cancel(); super.dismiss(); } @override public void settitle(charsequence title) { if (textutils.isempty(title)) { detail_tv.settext("正在加载"); } else { detail_tv.settext(title); } } @override public void settitle(int titleid) { settitle(getcontext().getstring(titleid)); } public static void dismissdialog(loadingdialog loadingdialog) { if (null == loadingdialog) { return; } loadingdialog.dismiss(); } }
-------------对应的布局如下------------------
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="160dp" android:layout_height="160dp" android:layout_gravity="center" android:background="@drawable/common_show_dialog" android:orientation="vertical" > <relativelayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="3" android:paddingtop="22dp" android:gravity="center" > <imageview android:id="@+id/iv_route" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:background="@drawable/dialog_bocop_loading_rotate_anim_img" /> </relativelayout> <relativelayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_marginbottom="15dp" android:layout_marginleft="10dp" android:layout_marginright="10dp" android:layout_weight="1" android:gravity="center_horizontal" > <textview android:id="@+id/detail_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toleftof="@+id/tv_point" android:ellipsize="marquee" android:gravity="center" android:singleline="true" android:text="正在加载..." android:textcolor="#ffffff" android:textsize="20sp" /> <textview android:id="@+id/tv_point" android:layout_width="20dp" android:layout_height="wrap_content" android:layout_alignparentright="true" android:text="..." android:textcolor="#ffffff" android:textsize="20sp" /> </relativelayout> </linearlayout>
比如在activity中要实现加载对话框调用 :
loadingdialog loadingdialog ; if (null == loadingdialog) { loadingdialog = new loadingdialog(aty); loadingdialog.setoncancellistener(this); } loadingdialog.settitle(“数据加载中”); if (!loadingdialog.isshowing()) loadingdialog.show();
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。