博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android:AysncTask异步加载
阅读量:4880 次
发布时间:2019-06-11

本文共 3928 字,大约阅读时间需要 13 分钟。

以下是链接:

 

转:

 

1 package com.wyl.myasyctask;  2   3 import java.io.InputStream;  4   5 import android.app.Activity;  6 import android.app.ProgressDialog;  7 import android.content.Context;  8 import android.graphics.Bitmap;  9 import android.graphics.BitmapFactory; 10 import android.os.AsyncTask; 11 import android.os.Bundle; 12 import android.util.Log; 13 import android.view.View; 14 import android.widget.Button; 15 import android.widget.ImageView; 16  17 public class MainActivity extends Activity { 18     private static final String sImageUrl = "http://fashion.qqread.com/ArtImage/20110225/0083_13.jpg"; 19     private Button mLoadButton; 20     private ImageView mImageView; 21  22     @Override 23     protected void onCreate(Bundle savedInstanceState) { 24         super.onCreate(savedInstanceState); 25         setContentView(R.layout.main); 26  27         Log.i("UI thread", " >> onCreate()"); 28  29         mImageView = (ImageView) this.findViewById(R.id.ImageVivew); 30  31         mLoadButton = (Button) this.findViewById(R.id.LoadButton); 32         mLoadButton.setOnClickListener(new View.OnClickListener() { 33             @Override 34             public void onClick(View v) { 35                 LoadImageTask task = new LoadImageTask(v.getContext()); 36                 task.execute(sImageUrl);// 源码中得知,会调用onPreExecute()...方法 37             } 38         }); 39  40     } 41  42     class LoadImageTask extends AsyncTask
{ 43 private ProgressDialog mProgressBar; 44 45 LoadImageTask(Context context) { 46 mProgressBar = new ProgressDialog(context); 47 mProgressBar.setCancelable(true); 48 mProgressBar.setMessage("setMessage()..."); 49 mProgressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER); 50 mProgressBar.setMax(100); 51 } 52 53 @Override 54 protected Bitmap doInBackground(String... params) { 55 System.out.println("doInBackground()..."); 56 57 Bitmap bitmap = null; 58 59 try { 60 publishProgress(10); 61 Thread.sleep(1000); 62 63 InputStream in = new java.net.URL(sImageUrl).openStream(); 64 publishProgress(60); 65 Thread.sleep(1000); 66 67 bitmap = BitmapFactory.decodeStream(in); 68 in.close(); 69 } catch (Exception e) { 70 e.printStackTrace(); 71 } 72 73 publishProgress(100); 74 return bitmap; 75 } 76 77 @Override 78 protected void onCancelled() { 79 super.onCancelled(); 80 } 81 82 @Override 83 protected void onPreExecute() { 84 85 mProgressBar.setProgress(0); 86 mProgressBar.setMessage("Image downloading ... 0%"); 87 mProgressBar.show(); 88 System.out.println("onPreExecute()..."); 89 } 90 91 @Override 92 protected void onPostExecute(Bitmap result) { 93 System.out.println("onPostExecute()..."); 94 if (result != null) { 95 mProgressBar.setMessage("Image downloading success!"); 96 mImageView.setImageBitmap(result); 97 } else { 98 mProgressBar.setMessage("Image downloading failure!"); 99 }100 101 mProgressBar.dismiss();102 }103 104 @Override105 protected void onProgressUpdate(Integer... values) {106 System.out.println("onProgressUpdate()...");107 mProgressBar.setMessage("Image downloading ... %" + values[0]);108 mProgressBar.setProgress(values[0]);109 }110 }111 112 }

 

 

main.xml:

1 
2
7 8
14 15
21 22 23

转载于:https://www.cnblogs.com/Sunnor/p/4856517.html

你可能感兴趣的文章
2345网址导航源码 v3.3
查看>>
JS重要知识点
查看>>
java解析数据
查看>>
改变 C/C++ 控制台程序的输出颜色和样式
查看>>
ADO constants include file for VBScript
查看>>
ExtJs4.2 RadioGroup CheckboxGroup
查看>>
InnoDB Undo Log
查看>>
在Application中集成Microsoft Translator服务之使用http获取服务
查看>>
flask页面中Head标签内容为空问题
查看>>
Centos7 Putty SSH密钥登录
查看>>
HDU 6330--Visual Cube(构造,计算)
查看>>
小说Symbian的签名
查看>>
Objective-C中ORM的运用:实体对象和字典的相互自动转换
查看>>
高级java面试宝典
查看>>
声明,本博客文章均为转载,只为学习,不为其他用途。感谢技术大牛的技术分享,让我少走弯路。...
查看>>
centos7.1下 Docker环境搭建
查看>>
c# 导出Excel
查看>>
Status: Checked in and viewable by authorized users 出现在sharepoint 2013 home 页面
查看>>
python数据预处理
查看>>
Python之路,Day21 - 常用算法学习
查看>>