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

 更新时间:2017年02月04日 14:29:49   投稿:mrr  
这篇文章主要介绍了Android实现网络加载时的对话框功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下

效果预览

这里写图片描述

简要说明

现在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 相机相册权限设置方法

    今天小编就为大家分享一篇Android 相机相册权限设置,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • Android开发中Flutter组件实用技巧

    Android开发中Flutter组件实用技巧

    这篇文章主要为大家介绍了Android开发中Flutter组件实用技巧,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Android 中Lambda表达式的使用实例详解

    Android 中Lambda表达式的使用实例详解

    这篇文章主要介绍了 Android 中Lambda表达式的使用实例详解的相关资料,需要的朋友可以参考下
    2017-05-05
  • Android App开发中创建Fragment组件的教程

    Android App开发中创建Fragment组件的教程

    这篇文章主要介绍了Android App开发中创建Fragment的教程,Fragment是用以更灵活地构建多屏幕界面的可UI组件,需要的朋友可以参考下
    2016-05-05
  • android读写中文如何避免乱码详解

    android读写中文如何避免乱码详解

    这篇文章主要介绍了android读写中文如何避免乱码的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-04-04
  • Android Studio如何为Activity添加自定义注解信息

    Android Studio如何为Activity添加自定义注解信息

    好久没用写文章了,今天给大家分享Android Studio如何为Activity添加自定义注解信息,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2021-06-06
  • Win10下Android App安装配置开发环境

    Win10下Android App安装配置开发环境

    这篇文章主要为大家详细介绍了Win10下Android App安装配置开发环境,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Android ViewModel的作用深入讲解

    Android ViewModel的作用深入讲解

    这篇文章主要介绍了Android ViewModel的作用,ViewModel类旨在以注重生命周期的方式存储和管理界面相关数据,ViewModel类让数据可在发生屏幕旋转等配置更改后继续留存,需要详细了解可以参考下文
    2023-05-05
  • Android使用API实现图像扭曲效果示例

    Android使用API实现图像扭曲效果示例

    这篇文章主要介绍了Android使用API实现图像扭曲效果,涉及Android坐标运算与图形绘制相关操作技巧,需要的朋友可以参考下
    2017-08-08
  • Android实现类似网易新闻选项卡动态滑动效果

    Android实现类似网易新闻选项卡动态滑动效果

    这篇文章主要介绍了Android实现类似网易新闻选项卡动态滑动效果的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-11-11

最新评论