Android实现加载对话框
作者:楠之枫雪 发布时间:2023-10-24 14:48:36
标签: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);
}
}
来源:https://blog.csdn.net/u014614038/article/details/47809393


猜你喜欢
- 1 配置多数据源时,application.yml 的有关mybatis的配置是失效的,因为他不知道配置哪一个数据源2 applicatio
- 本文实例讲述了C#遍历指定目录下所有文件的方法。分享给大家供大家参考。具体分析如下:先通过DirectoryInfo打开指定的目录,然后通过
- 本Demo为练手小项目,主要是熟悉目前主流APP的架构模式.此项目中采用MVC设计模式,纯代码和少许XIB方式实现.主要实现了朋友圈功能和摇
- Java原生API并不支持为应用程序设置全局热键。要实现全局热键,需要用JNI方式实现,这就涉及到编写C/C++代码,这对于大多数不熟悉C/
- 本文为大家分析了Java中字符流与字节流的区别,供大家参考,具体内容如下1. 什么是流 Java中的流是
- 最近在鼓捣spring -boot ,真好用,学习到jpa.通过生成Entity 文件,能够快速的生成数据库,并且使用JpaReposito
- springboot jpa 延迟加载问题在springboot中,在application.properties的配置文件中新增sprin
- 代码中已经加入了注释,需要的朋友可以直接参考代码中的注释。下面直接上功能实现的主要代码:import java.io.File;import
- Java 8 中 Function 接口的介绍Java 8 中提供了一个函数式接口 Function,这个接口表示对一个参数做一些
- 废话不多说了,直接给大家贴代码了,具体代码如下<?xml version="1.0" encoding="
- 有时候会碰到一些需要设置开始日期和结束日期的需求,比如有很多商品,每件商品都有开始出售日期和结束出售日期的字段。如何使用DatePicker
- 1.Spring bean组件 ”默认为单例模式scope=“singleton, 运行JavaApplication容器启动时自动创建对象
- 前言在Android开发过程中,我发现很多安卓源代码里应用了设计模式,比较常用的有适配器模式(各种adapter),建造者模式(Alert
- 1. 描述线程与进程的区别? 什么是Windows服务,它的生命周期与标准的EXE程序有什么不同 Windows上的单个进程所能访问的最大内
- 本文实例为大家分享了Java实现Flappy Bird游戏的具体代码,供大家参考,具体内容如下1.首先在mainActivity.xml中放
- 我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等。你可能知道在WinForm应用程序中可以利用Propertie
- 本文实例讲述了java读取properties配置文件的方法。分享给大家供大家参考。具体分析如下:这两天做java项目,用到属性文件,到网上
- 把最近听的写的一些题目做下笔记!1.下列程序的执行,说法错误的是 ( ABC )public class MultiCatch
- Android Studio 3.6正式版已经发布https://android-developers.googleblog.com/202
- 题目描述原题链接 :88. 合并两个有序数组给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m