资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android应用中怎么实现一个网络加载时功能

这篇文章将为大家详细讲解有关Android应用中怎么实现一个网络加载时功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

创新互联技术团队十多年来致力于为客户提供网站建设、成都网站建设、成都品牌网站建设成都全网营销推广、搜索引擎SEO优化等服务。经过多年发展,公司拥有经验丰富的技术团队,先后服务、推广了千余家网站,包括各类中小企业、企事单位、高校等机构单位。

效果预览

Android应用中怎么实现一个网络加载时功能

简要说明

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

代码说明:

dialog_loading.xml

<?xml version="1.0" encoding="utf-8"?>

 
  

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

loading_animation.xml

<?xml version="1.0" encoding="utf-8"?>

 

这个就是我们设置的旋转的属性动画的基本属性操作,这个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() {
   @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,系统默认的不给力,所以只能自己写了一个。

 
 

关于Android应用中怎么实现一个网络加载时功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


当前文章:Android应用中怎么实现一个网络加载时功能
文章链接:http://cdkjz.cn/article/iijige.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220