Android实现加载对话框

本文实例为大家分享了Android实现加载对话框的具体代码,供大家参考,具体内容如下

这里简单说一下两种实现加载对话框的方式:1.使用动画让一个图片旋转 2.使用progressbar。

感觉简单来说,dialog就是一个弹出的window,把自己定义的布局放置到window里面就可以了,加载对话框就是有个加载的动画,核心的地方就是实现这个动画,所所以方法  可以有,对图片添加动画,或者使用progressbar。

第一种方式:使用动画让一个图片旋转

先看一下布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/dialog_bg_while"
 android:orientation="vertical" >

 <ImageView
  android:layout_width="54dp"
  android:id="@+id/loading_dialog_pic"
  android:layout_height="54dp"
  android:layout_gravity="center_horizontal"
  android:layout_marginTop="15dp"
  android:background="@drawable/loading" />

 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:layout_marginTop="10dp"
  android:text="正在加载..." />

</LinearLayout>

然后自定义Alertdialog,并对图片添加旋转动画:

public class LoadingDialog extends AlertDialog {
 private final String DEFAULT_TEXT="正在加载";
 private ImageView mImageview;
 private TextView mTextView;
 private LinearLayout mLayout;
 private String mText;

 protected LoadingDialog(Context context) {
 super(context);
 // TODO Auto-generated constructor stub
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 mLayout=(LinearLayout) LinearLayout.inflate(getContext(), R.layout.loading_dialog, null);
 mImageview=(ImageView) mLayout.findViewById(R.id.loading_dialog_pic);
 mTextView=(TextView) mLayout.findViewById(R.id.loading_dialog_text);
 loadanimation();
 getWindow().setContentView(mLayout);

 }

 private void loadanimation() {//对图片添加旋转动画
 // TODO Auto-generated method stub
 Animation anim=AnimationUtils.loadAnimation(getContext(), R.anim.loading_dialog_anim);
 LinearInterpolator lin = new LinearInterpolator();
 anim.setInterpolator(lin);
 mImageview.setAnimation(anim);

 }

}

看一下xml的动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

 <rotate
  android:duration="1500"
  android:pivotX="50%"
  android:pivotY="50%"
  android:fromDegrees="0.0"
  android:repeatCount="infinite"
  android:toDegrees="-358" />

</set>

第二种方式:使用progressbar

首先是一个animation-list:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >

 <item
  android:drawable="@drawable/loading1"
  android:duration="100"/>
 <item
  android:drawable="@drawable/loading2"
  android:duration="100"/>
 <item
  android:drawable="@drawable/loading3"
  android:duration="100"/>
 <item
  android:drawable="@drawable/loading4"
  android:duration="100"/>
 <item
  android:drawable="@drawable/loading5"
  android:duration="100"/>
 <item
  android:drawable="@drawable/loading6"
  android:duration="100"/>
 <item
  android:drawable="@drawable/loading7"
  android:duration="100"/>
 <item
  android:drawable="@drawable/loading8"
  android:duration="100"/>

</animation-list>

看一下布局的实现:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@drawable/dialog_bg_while"
 android:orientation="vertical" >

 <ProgressBar
  style="@android:style/Widget.ProgressBar.Large"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:layout_marginTop="10dp"
  android:indeterminateDrawable="@drawable/loading_animation_list"
  android:indeterminateDuration="1500" />

 <View
  android:layout_width="match_parent"
  android:layout_height="1dp"
  android:background="#00BCD4" />

 <TextView
  android:id="@+id/loading_dialog_text"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:layout_marginTop="10dp"
  android:text="正在加载..." />

</LinearLayout>

然后自定义一个alertdialog:

public class LoadingDialog extends AlertDialog {
 private final String DEFAULT_TEXT="正在加载";
 private TextView mTextView;
 private LinearLayout mLayout;
 private String mText;

 protected LoadingDialog(Context context) {
 super(context);
 // TODO Auto-generated constructor stub
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 mLayout=(LinearLayout) LinearLayout.inflate(getContext(), R.layout.loading_dialog, null);
 mTextView=(TextView) mLayout.findViewById(R.id.loading_dialog_text);
 WindowManager m=(WindowManager) getContext().getSystemService(getContext().WINDOW_SERVICE);
 int windowwith=m.getDefaultDisplay().getWidth();
 int w=windowwith*3/5;
 int h=300;
 getWindow().setLayout(w, h);//设置对话框窗体大小
 getWindow().setContentView(mLayout);

 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android实现网络加载时的对话框功能

    效果预览 简要说明 现在android程序网络请求操作是必不可少的,然而拥有好的交互体验的程序对网络耗时操作的处理尤为重要. 代码说明: dialog_loading.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="

  • 实例详解Android自定义ProgressDialog进度条对话框的实现

    Android SDK已经提供有进度条组件ProgressDialog组件,但用的时候我们会发现可能风格与我们应用的整体风格不太搭配,而且ProgressDialog的可定制行也不太强,这时就需要我们自定义实现一个ProgressDialog. 通过看源码我们发现,ProgressDialog继承自Alertdialog,有一个ProgressBar和两个TextView组成的,通过对ProgressDialog的源码进行改进就可以实现一个自定义的ProgressDialog. 1.效果: 首先

  • Android加载loading对话框的功能及实例代码(不退出沉浸式效果)

    一.自定义Dialog 在沉浸式效果下,当界面弹出对话框时,对话框将获取到焦点,这将导致界面退出沉浸式效果,那么是不是能通过屏蔽对话框获取焦点来达到不退出沉浸式的目的呢.说干就干,我们先来看一下改善后的效果图. 普通对话框弹出效果 LoadingDialog弹出效果 自定义LoadingDialog public class LoadingDialog extends Dialog { public LoadingDialog(Context context) { super(context);

  • Android中自定义对话框(Dialog)的实例代码

    1.修改系统默认的Dialog样式(风格.主题) 2.自定义Dialog布局文件 3.可以自己封装一个类,继承自Dialog或者直接使用Dialog类来实现,为了方便以后重复使用,建议自己封装一个Dialog类 第一步: 我们知道Android定义个控件或View的样式都是通过定义其style来实现的,查看Android框架中的主题文件,在源码中的路径:/frameworks/base/core/res/res/values/themes.xml,我们可以看到,Android为Dialog定义了

  • Android自定义Dialog实现加载对话框效果

    前言 最近开发中用到许多对话框,之前都是在外面的代码中创建AlertDialog并设置自定义布局实现常见的对话框,诸如更新提示等含有取消和删除两个按钮的对话框我们可以通过代码创建一个AlertDialog并通过它暴露的一系列方法设置我们自定义的布局和style,但有时候系统的AlertDialog并不能实现更好的定制,这时,我们就想到了自定义Dialog.通过查看AlertDialog的类结构发现它也是继承于Dialog,于是我们也可以通过继承Dialog实现我们自定义的Dialog.这篇文章将

  • 详解Android 全局弹出对话框SYSTEM_ALERT_WINDOW权限

    项目中为了实现账号多设备登录的监听 一个账号在别的设备登录时在该设备上需要弹出对话框提示 故而用到全局对话框 方案一. 1.在开发中有时会用到全局弹出对话框但必须在manifest中申请权限: <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 2.创建Dialog AlertDialog.Builder builder=new AlertDialog.Builder(this)

  • Android加载对话框同时异步执行实现方法

    Android中通过子线程连接网络获取资料,同时显示加载进度对话框给用户的操作,需要Thread和Handler来完成,在Thread中执行比较耗时的代码,完成后再通过Handler发送消息给主线程,由主线程刷新UI. 在实现上比较的烦琐,为简化此方法,花了点时间封装了Thread和Handler,现在通过简单的代码就可以实现相同的功能,而把更多精力放到业务逻辑处理上! 效果如图:   复制代码 代码如下: LoadingDialog loadingDialog = new LoadingDia

  • Android实现点击AlertDialog上按钮时不关闭对话框的方法

    本文实例讲述了Android实现点击AlertDialog上按钮时不关闭对话框的方法.分享给大家供大家参考.具体如下: 开发过程中,有时候会有这样的需求: 点击某个按钮之后显示一个对话框,对话框上面有一个输入框,并且有"确认"和"取消"两个按钮.当用户点击确认按钮时,需要对输入框的内容进行判断.如果内容为空则不关闭对话框,并toast提示. 使用AlertDialog.Builder创建对话框时,可以使用builder.setNegativeButton和build

  • Android实现底部对话框BottomDialog弹出实例代码

    最近项目上需要实现一个底部对话框,要实现这样的功能其实很简单,先看代码: private void show1() { Dialog bottomDialog = new Dialog(this, R.style.BottomDialog); View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_content_normal, null); bottomDialog.setContentView(contentV

  • Android中AlertDialog各种对话框的用法实例详解

    目标效果: 程序运行,显示图一的几个按钮,点击按钮分别显示图二到图六的对话框,点击对话框的某一项或者按钮,也会显示相应的吐司输出. 1.activity_main.xml页面存放五个按钮. activity_main.xml页面: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools&

随机推荐