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

所属分类: 软件编程 / Android 阅读数: 21
收藏 0 赞 0 分享

效果预览

这里写图片描述

简要说明

现在android程序网络请求操作是必不可少的,然而拥有好的交互体验的程序对网络耗时操作的处理尤为重要。

代码说明:

dialog_loading.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/dialog_view" 
 android:orientation="vertical"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"
 android:gravity="center">
 <ImageView 
 android:id="@+id/img"
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"
 android:background="@android:color/transparent"
 android:src="@drawable/progress"
 />
 <TextView 
 android:id="@+id/tipTextView" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_marginLeft="10dp" 
 android:text="数据加载中……" /> 
</LinearLayout>

这个布局就是我们自定义的显示布局,比较简单明了,最外层一个垂直排列的线性布局,里面依次是一个imageview和textview。

loading_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">
 <rotate 
 android:interpolator="@android:anim/linear_interpolator"
 android:pivotX="50%"
 android:pivotY="50%"
 android:fromDegrees="0"
 android:toDegrees="+360"
 android:duration="1500"
 android:startOffset="-1"
 android:repeatMode="restart"
 android:repeatCount="-1"/>
</set>

这个就是我们设置的旋转的属性动画的基本属性操作,这个xml存在于res下的anim文件夹下(手动创建文件夹)

CustomProgressDialog.class
package com.cc.customprogressdialog.util;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.cc.customprogressdialog.R;
/**
 * Created by CC on 2017/2/4.
 */
public class CustomProgressDialog extends Dialog {
 Context context;
 private ImageView spaceshipImage;
 private Animation hyperspaceJumpAnimation;
 public CustomProgressDialog(Context context) {
 super(context);
 this.context = context;
 }
 public CustomProgressDialog(Context context, int theme) {
 super(context, theme);
 this.context = context;
 }
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 LayoutInflater inflater = LayoutInflater.from(context);
 View v = inflater.inflate(R.layout.dialog_loading, null);// 得到加载view
 LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
 // main.xml中的ImageView
 spaceshipImage = (ImageView) v.findViewById(R.id.img);
 // 加载动画
 hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.loading_animation);
 // 使用ImageView显示动画
 spaceshipImage.startAnimation(hyperspaceJumpAnimation);
 setCancelable(false);// 不可以用“返回键”取消
 setContentView(layout, new LinearLayout.LayoutParams(
  LinearLayout.LayoutParams.MATCH_PARENT,
  LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
 }
}

这个类就是自定义的ProgressDialog,代码的关键步骤我都写了注释。

使用

package com.cc.customprogressdialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.cc.customprogressdialog.util.CustomProgressDialog;
public class MainActivity extends AppCompatActivity {
 private Button btn;
 private CustomProgressDialog mProgressDialog;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 btn = (Button) findViewById(R.id.btn);
 btn.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
  new AsyncTask<Void, Void, Void>() {
   @Override
   protected void onPreExecute() {
   super.onPreExecute();
   mProgressDialog = new CustomProgressDialog(MainActivity.this, R.style.loading_dialog);
   mProgressDialog.show();
   }
   @Override
   protected Void doInBackground(Void... voids) {
   SystemClock.sleep(2000);
   return null;
   }
   @Override
   protected void onPostExecute(Void aVoid) {
   super.onPostExecute(aVoid);
   mProgressDialog.dismiss();
   }
  }.execute();
  }
 });
 }
}

上述代码我们看到我在主activity里面添加一个按钮,实现其点击事件,在点击事件中我创建了一个异步操作,模拟网络耗时。
注意一点我在创建CustomProgressDialog的时候传入了一个style,系统默认的不给力,所以只能自己写了一个。

 <!-- 自定义loading dialog -->
 <style name="loading_dialog" parent="android:style/Theme.Dialog">
 <item name="android:windowFrame">@null</item>
 <item name="android:windowNoTitle">true</item>
 <item name="android:background">#00000000</item>
 <item name="android:windowBackground">@android:color/transparent</item>
 <item name="android:windowIsFloating">true</item>
 <item name="android:windowContentOverlay">@null</item>
 </style>

属性的参数意思有兴趣的自行百度,在这里不一一介绍了。

实现的代码就这么简单但很实用,希望对各位读者有所帮助。最后附上完整的代码:

http://xiazai.jb51.net/201702/yuanma/CustomProgressDialog

以上所述是小编给大家介绍的Android实现网络加载时的对话框功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Android实现悬浮窗体效果

这篇文章主要为大家详细介绍了Android实现悬浮窗体效果,显示悬浮窗口,窗口可以拖动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Andriod studio 打包aar 的方法

这篇文章主要介绍了Andriod studio 打包aar的方法,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

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

这篇文章主要介绍了Android加载loading对话框的功能及实例代码,不退出沉浸式效果,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中LayoutInflater.inflater()的正确打开方式

这篇文章主要给大家介绍了关于Android中LayoutInflater.inflater()的正确打开方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Delphi在Android下使用Java库的方法

这篇文章主要介绍了Delphi在Android下使用Java库的方法,本文以Android的USB串口通讯库为例,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多