大佬教程收集整理的这篇文章主要介绍了Android实现自定义加载框的代码示例,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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,Animation.RESTART,0.5f,0.5f); mAnim = new RotateAnimation(0,360,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(); } } @H_874_14@
-------------对应的布局如下------------------
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://scheR_269_11845@as.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> @H_874_14@
比如在Activity中要实现加载对话框调用 :
LoadingDialog loadingDialog ; if (null == loadingDialog) { loadingDialog = new LoadingDialog(aty); loadingDialog.setOnCancelListener(this); } loadingDialog.settitle(“数据加载中”); if (!loadingDialog.isShowing()) loadingDialog.show(); @H_874_14@
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是大佬教程为你收集整理的Android实现自定义加载框的代码示例全部内容,希望文章能够帮你解决Android实现自定义加载框的代码示例所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。