Android实现自定义加载框的代码示例-创新互联

App在与服务器进行网络交互的时候,需要有一个提示的加载框,如图:

我们提供的服务有:网站设计制作、成都网站建设、微信公众号开发、网站优化、网站认证、大田ssl等。为上1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的大田网站制作公司

Android实现自定义加载框的代码示例

此时我们可以自定义一个加载中的对话框,代码如下:


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(); 
} 
} 

网站标题:Android实现自定义加载框的代码示例-创新互联
文章来源:http://scyanting.com/article/dicggj.html