服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现加载对话框

Android实现加载对话框

2022-11-15 15:47楠之枫雪 Android

这篇文章主要为大家详细介绍了Android实现加载对话框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

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

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

先看一下布局:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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,并对图片添加旋转动画:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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的动画:

?
1
2
3
4
5
6
7
8
9
10
11
12
<?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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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>

看一下布局的实现:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

延伸 · 阅读

精彩推荐