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

Android实现网络加载时的对话框功能,第1张

概述效果预览简要说明现在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.classpackage 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实现网络加载时的对话框功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1147080.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存