一、概述
Android是单线程模型,耗时的操作必须放在非主线程中执行,对此,我们需要使用多线程/线程池或者AsyncTask等来完成异步加载任务。
二、AsyncTask
AsyncTask
是一个抽象类,通常用于被继承,继承AsyncTask需要指定如下三个泛型参数:
Params:启动任务时输入参数的类型
Progress:后台任务执行中返回进度值的类型
Result:后台执行任务完成后返回结果的类型
AsyncTask子类的回调方法:
doInBackground:必须重写,异步执行后台线程将要完成的任务
onPreExecute:执行后台耗时操作前被调用,通常用户完成一些初始化操作
onPostExecute:当doInBackground()完成后系统会自动调用该方法,并将doInBackground方法返回的值传给该方法
onProgressUpdate:在doInBackground()方法中调用publishProgress()方法更新任务的执行进度后,就会触发该方法。
注意事项:
必须在主线程中创建AsyncTask实例
execute方法必须在主线程中调用
重写的四个方法是系统自动调用的,不应手动调用
ImageAsyncTaskActivity.java
package com.czhappy.asynctaskdemo.activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import com.czhappy.asynctaskdemo.R;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* Description:图片加载演示
* User: chenzheng
* Date: 2016/12/23 0023
* Time: 10:43
*/
public class ImageAsyncTaskActivity extends AppCompatActivity {
private ImageView imageView;
private ProgressBar progressBar;
private static String imageUrl = "http://img.hb.aicdn.com/761f1bce319b745e663fed957606b4b5d167b9bff70a-nfBc9N_fw580";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_asynctask);
imageView = (ImageView) findViewById(R.id.imageView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
//开启异步线程操作
new MyAsyncTask().execute(imageUrl);
}
class MyAsyncTask extends AsyncTask{
/**
* 初始化操作
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected Bitmap doInBackground(String... params) {
String url = params[0];//这里只有一个参数,直接获取
Bitmap bitmap = null;
URLConnection connection ;
InputStream is;
try {
connection = new URL(url).openConnection();
is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
//休眠3秒防止加载太快,看不到加载效果
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
bitmap = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
progressBar.setVisibility(View.GONE);
if(bitmap!=null){
imageView.setImageBitmap(bitmap);
}
}
}
}
四、水平进度条异步加载演示
1.效果图:
2.核心代码:
ProgressBarTestActivity.java
package com.czhappy.asynctaskdemo.activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ProgressBar;
import com.czhappy.asynctaskdemo.R;
/**
* Description:水平进度条调用演示
* User: chenzheng
* Date: 2016/12/23 0023
* Time: 11:38
*/
public class ProgressBarTestActivity extends AppCompatActivity {
private ProgressBar progressBar;
private MyAsyncTask mTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progressbar_test);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
mTask = new MyAsyncTask();
mTask.execute();
}
class MyAsyncTask extends AsyncTask{
@Override
protected Void doInBackground(Void... params) {
for (int i = 0; i < 100; i++) {
//如果已经设置成cancel,则终止掉当前的进程
if(mTask.isCancelled()){
break;
}
publishProgress(i);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if(mTask.isCancelled()){
return;
}
progressBar.setProgress(values[0]);
}
}
@Override
protected void onPause() {
super.onPause();
if(mTask!=null && mTask.getStatus()== AsyncTask.Status.RUNNING){
//cancel方法只是将对应的AsyncTask状态标记为cancel,并没有真正的取消掉
mTask.cancel(true);
}
}
}
六、高效ListView及图片一级缓存
本例使用的是Android提供LruCache类实现一级缓存,而对于高效ListView这块我们是判断ListView滑动状态,决定何时加载图片以提高效率:
ListView滑动停止后才加载可见项
ListView滑动时,取消所有加载项
1.效果图
核心代码:
News.java
package com.czhappy.asynctaskdemo.model;
/**
* Description:
* User: chenzheng
* Date: 2016/12/24 0024
* Time: 11:23
*/
public class News {
public String newsIconUrl;
public String newsTitle;
public String newsContent;
}
NewsListActivity.java
package com.czhappy.asynctaskdemo.activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
import com.czhappy.asynctaskdemo.R;
import com.czhappy.asynctaskdemo.adapter.NewsListAdapter;
import com.czhappy.asynctaskdemo.model.News;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* Description:
* User: chenzheng
* Date: 2016/12/24 0024
* Time: 11:18
*/
public class NewsListActivity extends AppCompatActivity {
private ListView mListView;
private String mUrl = "http://www.imooc.com/api/teacher?type=4&num=30";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_list);
mListView = (ListView) findViewById(R.id.listView);
new NewsAsyncTask().execute(mUrl);
}
/**
* 实现网络的异步访问
*/
class NewsAsyncTask extends AsyncTask>{
@Override
protected List doInBackground(String... params) {
return getJsonData(params[0]);
}
@Override
protected void onPostExecute(List list) {
super.onPostExecute(list);
NewsListAdapter adapter = new NewsListAdapter(NewsListActivity.this, list, mListView);
mListView.setAdapter(adapter);
}
}
private List getJsonData(String url) {
List list = new ArrayList<>();
try {
String jsonString = readStream(new URL(url).openStream());
Log.e("tag", "jsonString:"+jsonString);
JSONObject jsonObject;
News news;
try {
jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
news = new News();
news.newsIconUrl = jsonObject.getString("picSmall");
news.newsTitle = jsonObject.getString("name");
news.newsContent = jsonObject.getString("description");
list.add(news);
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
private String readStream(InputStream is){
InputStreamReader isr;
String result = "";
try {
String line = "";
isr = new InputStreamReader(is, "utf-8");//字节流转化为字符流
BufferedReader br = new BufferedReader(isr);
while((line = br.readLine())!=null){
result += line;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
NewsListAdapter.java
package com.czhappy.asynctaskdemo.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.czhappy.asynctaskdemo.R;
import com.czhappy.asynctaskdemo.model.News;
import com.czhappy.asynctaskdemo.utils.ImageLoader;
import java.util.List;
/**
* Description:
* User: chenzheng
* Date: 2016/12/24 0024
* Time: 13:53
*/
public class NewsListAdapter extends BaseAdapter implements AbsListView.OnScrollListener{
private Context context;
private List list;
private LayoutInflater mInflater;
private ImageLoader mImageLoader;
private int mStart, mEnd;
public static String [] URLS;
private boolean mFirstIn;
public NewsListAdapter(Context context, List list, ListView listView) {
this.context = context;
this.list = list;
mInflater = LayoutInflater.from(context);
mImageLoader = new ImageLoader(listView);
URLS = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
URLS[i] = list.get(i).newsIconUrl; }
listView.setOnScrollListener(this);//绑定监听事件
mFirstIn = true;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder=null;
if(convertView==null){
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.news_list_item, null);
viewHolder.image_iv = (ImageView) convertView.findViewById(R.id.image_iv);
viewHolder.title_tv = (TextView) convertView.findViewById(R.id.title_tv);
viewHolder.content_tv = (TextView) convertView.findViewById(R.id.content_tv);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
//viewHolder.image_iv.setImageResource(R.mipmap.ic_launcher);
String url = list.get(position).newsIconUrl;
viewHolder.image_iv.setTag(url);//设置tag,防止图片错乱
//mImageLoader.showImageByThread(viewHolder.image_iv, url);
mImageLoader.showImageByAsyncTask(viewHolder.image_iv, url);
viewHolder.title_tv.setText(list.get(position).newsTitle);
viewHolder.content_tv.setText(list.get(position).newsContent);
return convertView;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState == SCROLL_STATE_IDLE){//滑动停止状态,加载可见项
mImageLoader.loadImage(mStart, mEnd);
}else{//停止加载可见项
mImageLoader.cancelAllTasks();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
mStart = firstVisibleItem;
mEnd = firstVisibleItem + visibleItemCount;
//第一次显示的时候调用
if(mFirstIn && totalItemCount>0){
mFirstIn = false;
mImageLoader.loadImage(mStart, mEnd);
}
}
class ViewHolder{
TextView title_tv, content_tv;
ImageView image_iv;
}
}
ImageLoader.java
package com.czhappy.asynctaskdemo.utils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.LruCache;
import android.widget.ImageView;
import android.widget.ListView;
import com.czhappy.asynctaskdemo.R;
import com.czhappy.asynctaskdemo.adapter.NewsListAdapter;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
/**
* Description:
* User: chenzheng
* Date: 2016/12/24 0024
* Time: 14:08
*/
public class ImageLoader {
private ImageView mImageView;
private String mUrl;
private LruCache mCaches;//一级缓存保存在内存中
private ListView mListView;
private Set mTasks;
public ImageLoader(ListView listView){
this.mListView = listView;
mTasks = new HashSet<>();
//获取最大可用内存
int maxMemory = (int) Runtime.getRuntime().maxMemory();
int cacheSize = maxMemory/4;//设置实际缓存大小
mCaches = new LruCache(cacheSize){
/**
* 在每次存入缓存的时候调用
* @param key
* @param value
* @return
*/
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
}
/**
* 增加到缓存
* @param url
* @param bitmap
*/
public void addBitmapToCache(String url, Bitmap bitmap){
if(getBitmapFromCache(url)==null){
mCaches.put(url, bitmap);
}
}
/**
* 从缓存中获取数据
* @param url
* @return
*/
public Bitmap getBitmapFromCache(String url){
return mCaches.get(url);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(mImageView.getTag().equals(mUrl)){
mImageView.setImageBitmap((Bitmap) msg.obj);
}
}
};
public void showImageByThread(ImageView imageView, final String imageUrl){
mImageView = imageView;
mUrl = imageUrl;
new Thread(){
@Override
public void run() {
super.run();
Bitmap bmp = getBitmapFromUrl(imageUrl);
Message message = Message.obtain();//使用现有的Messsage,提高Message的效率
message.obj = bmp;
handler.sendMessage(message);
}
}.start();
}
/**
* 从网络下载图片
* @param iurl
* @return
*/
public Bitmap getBitmapFromUrl(String iurl){
Bitmap bitmap = null;
InputStream is = null;
try {
URL url = new URL(iurl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
is = new BufferedInputStream(connection.getInputStream());
bitmap = BitmapFactory.decodeStream(is);
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bitmap;
}
public void showImageByAsyncTask(ImageView imageView, final String imageUrl){
//从缓存中获取图片,判断是否存在
Bitmap bitmap = getBitmapFromCache(imageUrl);
if(bitmap==null){
//从网络下载图片
imageView.setImageResource(R.mipmap.ic_launcher);
//new NewsAsyncTask(imageView, imageUrl).execute(imageUrl);
}else{
//如果缓存中存在,则直接显示图片
imageView.setImageBitmap(bitmap);
}
}
private class NewsAsyncTask extends AsyncTask<String, Void, Bitmap>{
//private ImageView mImageView;
private String mUrl;
public NewsAsyncTask(String url) {
//this.mImageView = mImageView;
this.mUrl = url;
}
@Override
protected Bitmap doInBackground(String... params) {
//从网络获取图片
Bitmap bitmap = getBitmapFromUrl(params[0]);
if(bitmap!=null){
//将Bitmap保存到缓存
addBitmapToCache(params[0], bitmap);
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
ImageView imageView = (ImageView) mListView.findViewWithTag(mUrl);
if(imageView!=null && bitmap!=null){
imageView.setImageBitmap(bitmap);
}
mTasks.remove(this);
}
}
/**
* 用来加载从start到end的数据
* @param start
* @param end
*/
public void loadImage(int start, int end){
for (int i = start; i < end; i++) {
String url = NewsListAdapter.URLS[i];
//从缓存中获取图片,判断是否存在
Bitmap bitmap = getBitmapFromCache(url);
if(bitmap==null){
//从网络下载图片
NewsAsyncTask task = new NewsAsyncTask(url);
task.execute(url);
mTasks.add(task);
}else{
//如果缓存中存在,则直接显示图片
ImageView imageView = (ImageView) mListView.findViewWithTag(url);
imageView.setImageBitmap(bitmap);
}
}
}
public void cancelAllTasks(){
if(mTasks!=null){
for (NewsAsyncTask task : mTasks){
task.cancel(true);
}
}
}
}
七、源码下载
http://download.csdn.net/detail/chenzheng8975/9722102