AsyncTaskActivity
package org.wp.activity;
import java.io.InputStream;
import java.text.DecimalFormat;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.Config;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AsyncTaskActivity extends Activity {
/** Context对象 **/
private Context mContext;
/** 开始加载图片 **/
private Button myButton;
/** 显示加载进度 **/
private TextView myTextView;
/** 加载图片开始时间 **/
private Long mLoadStartTime = 0L;
/** 加载图片结束时间 **/
private Long mLoadEndTime = 0L;
/** 格式化对象 **/
private DecimalFormat decf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.asynctask);
mContext = this;
decf = new DecimalFormat("0.00");
myTextView = (TextView) this.findViewById(R.id.myTextView);
myButton = (Button) this.findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadImage();
}
});
}
private void loadImage() {
/**
* AsyncTask 定义了三种泛型类型 Params,Progress和Result。
* Params 启动任务执行的输入参数,比如HTTP请求的URL。
* Progress 后台任务执行的百分比。
* Result 后台执行任务最终返回的结果,比如String。
*/
new AsyncTask<Void, Double, Long>() {
@Override
protected void onPreExecute() {
myTextView.setText("开始加载图片");
}
@Override
protected Long doInBackground(Void... params) {
mLoadStartTime = System.currentTimeMillis();
for (int i = 0; i < 2000; i++) {
readBitmap(mContext, R.drawable.one_piece);
double percent = (double) (i + 1) * 100 / 2000;
publishProgress(percent); // 更新百分比
}
mLoadEndTime = System.currentTimeMillis();
return mLoadEndTime - mLoadStartTime;
}
@Override
protected void onProgressUpdate(Double... values) {
myTextView.setText("当前加载进度" + decf.format(values[0]) + "%");
}
@Override
protected void onPostExecute(Long result) {
myTextView.setText("读取2000张图片一共耗时" + result + "毫秒");
}
}.execute();
}
/**
* 读取图片
*
* @param context
* @param resId
* @return Bitmap
*/
public Bitmap readBitmap(Context context, int resId) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Config.RGB_565;
opts.inPurgeable = true;
opts.inInputShareable = true;
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opts);
}
}
asynctask.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="AsyncTask循环加载2000张图片"
android:id="@+id/myButton" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="点击按钮开始加载图片"
android:id="@+id/myTextView" />
</LinearLayout>
分享到:
相关推荐
Android AsyncTask的简单Demo,对应Blog文章:http://blog.csdn.net/dolacmeng/article/details/50215519
Android AsyncTask用法和异步加载图片通过AsyncTask 从网络上异步加载一张图片
Android AsyncTask Demo 自己参考网络写的学习 demo
Android AsyncTask异步处理下载网页
代码为博客的示例代码,有问题请博客中留言:http://blog.csdn.net/lmj623565791/article/details/38614699
AsyncTask 断点下载
Android AsyncTask 文章集合
android AsyncTask中更新progressBar 。可用于http请求、下载文件等
Android Asynctask 先小试牛刀,了解一下各个方法执行过程,关注博客http://himici.com/
android asyncTask的使用,通过简单的实例来说明Task异步类的使用
Android AsyncTask 同步下载图片,先是显示默认的图标,下载完成了立刻显示下载的图标,可以左右切换
android asynctask的fragment更新UI(附线程管理) 博客地址:http://blog.csdn.net/qq_16064871
Android AsyncTask Demo
简单的android AsyncTask 例子
android AsyncTask介绍,多线程。
主要为大家详细解析了Android AsyncTask使用以及源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
自己写了一个基于httpurlconnection和Asynctask的例子
里面是Android AsyncTask 的两种使用方法,一种是加载网络图片,一种是模拟耗时加载进度条
如果用现在做秒表,建议用asyncTask...