iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android实现自定义加载框的代码示例
  • 174
分享到

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

自定义示例Android 2022-06-06 04:06:22 174人浏览 薄情痞子
摘要

App在与服务器进行网络交互的时候,需要有一个提示的加载框,如图: 此时我们可以自定义一个加载中的对话框,代码如下: public class LoadingDialog

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(); 
您可能感兴趣的文章:加载页面遮挡耗时操作任务页面--第三方开源之AndroidProgressLayoutAndroid开发中如何解决Fragment +Viewpager滑动页面重复加载的问题Android中替换WEBView加载网页失败时的页面Android中自定义加载样式图片的具体实现Android自定义加载loading view动画组件Android自定义加载控件实现数据加载动画Android自定义view实现阻尼效果的加载动画Android自定义下拉刷新上拉加载Android自定义View实现loading动画加载效果Android自定义Dialog实现文字动态加载效果Android开发实现自定义新闻加载页面功能实例


--结束END--

本文标题: Android实现自定义加载框的代码示例

本文链接: https://www.lsjlt.com/news/22342.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作