码迷,mamicode.com
首页 > 其他好文 > 详细

异步任务及其Listview优化的种种

时间:2015-08-03 01:16:33      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.asynctask.MainActivity" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</RelativeLayout>

item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="4dp" >

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:gravity="center"
        android:paddingLeft="4dp"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TextView
            android:id="@+id/tv_title"
            android:layout_gravity="center"
            android:text="title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        
        
        <TextView
            android:id="@+id/tv_content"
            android:layout_gravity="center"
            android:text="content"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

NewsBean

package com.example.asynctask;

import android.widget.ImageView;

public class NewsBean {
	public String title;
	public String content;
	public String imageViewUrl;
	public NewsBean() {
		super();
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getImageViewUrl() {
		return imageViewUrl;
	}
	public void setImageViewUrl(String imageViewUrl) {
		this.imageViewUrl = imageViewUrl;
	}
	
	
	
	
}

MainActivity

package com.example.asynctask;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {
	private ListView listView;
	private static String httpUrl = "网址";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (ListView) findViewById(R.id.listview);
		//开始异步任务进行异步加载
		new NewAsyncTask().execute(httpUrl);
	}

	/**
	 * 异步任务--参数为url地址、没有进度、返回对象NewsBean集合
	 */
	class NewAsyncTask extends AsyncTask<String, Void, List<NewsBean>> {

		private List<NewsBean> newBeanlist;

		@Override
		protected List<NewsBean> doInBackground(String... params) {
			//通过http网址,进行获取json字符串获取的操作
			return getJsonData(httpUrl);
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected void onPostExecute(List<NewsBean> result) {
			super.onPostExecute(result);
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			super.onProgressUpdate(values);
			//获取完毕后,进行listview的数据适配操作,进行填充数据操作
			NewAdapter adapter = new NewAdapter(newBeanlist, MainActivity.this,listView);
			listView.setAdapter(adapter);
		}

		/**************************************************************
		 * 通过url地址信息,进行json字符串的获取
		 */
		private List<NewsBean> getJsonData(String url) {
			//最终返回的json数据,解析,分装一个对象集合
			newBeanlist = new ArrayList<NewsBean>();

			/**
			 * 跟这个一样new URL(url).openConnection().getInputStream()
			 */
			try {
				// 返回一个json字符串
				String jsonString = readStream(new URL(url).openStream());
				JSONObject jsonObject = null;
				NewsBean newsBean;
				try {
					// 整体是一个jsonObject
					jsonObject = new JSONObject(jsonString);
					// 键是jsonArray数组
					JSONArray jsonArray = jsonObject.getJSONArray("data");
					// 从jsonArray中循环获取jsonObject
					for (int i = 0; i < jsonArray.length(); i++) {
						jsonObject = jsonArray.getJSONObject(i);
						//创建本地的newsBean对象
						newsBean = new NewsBean();
						//为该对象进行属性值的设置操作
						newsBean.imageViewUrl = jsonObject
								.getString("picSmall");
						newsBean.title = jsonObject.getString("name");
						newsBean.content = jsonObject.getString("description");
						//添加对象,组建集合
						newBeanlist.add(newsBean);

					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return newBeanlist;

		}

		/**
		 * 通过流的方式,读取的网络json字符串
		 */
		private String readStream(InputStream inStream) {
			InputStreamReader isr;
			String result = "";
			try {
				String line = "";
				isr = new InputStreamReader(inStream, "utf-8");
				BufferedReader br = new BufferedReader(isr);
				while ((line = br.readLine()) != null) {
					result += line;

				}
			} catch (Exception e) {
			}
			return result;
		}

	}
}

NewAdapter

package com.example.asynctask;
import java.util.List;

import android.R.bool;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class NewAdapter extends BaseAdapter implements OnScrollListener{
	/**
	 * list是数据集合进行设置数据的操作
	 * layoutInflater进行加载itme布局
	 * 
	 */
	private List<NewsBean> mList;
	private LayoutInflater layoutInflater;

	private ImageLoader mImageLoader;
	//代表可见的条目的起始位置
	private int mStart;
	private int mEnd;
	//将所有数据的url放到一个数组中,进行管理
	public static String[] urls;
	//代表是不是第一次启动程序--需要进行预加载图片
	private boolean mFirst;
	public NewAdapter(List<NewsBean> mList, Context context,ListView listView) {
		super();
		this.mList = mList;
		this.layoutInflater = layoutInflater.from(context);
		//将数据集合的url单独放到数组中
		urls = new String[mList.size()];
		mImageLoader = new ImageLoader(listView);
		for (int i = 0; i < mList.size(); i++) {
			//遍历获取每个条目的图片url地址信息
			urls[i] = mList.get(i).imageViewUrl;
		}
		//默认是第一次加载
		mFirst = true;
		//注册监听
		listView.setOnScrollListener(this);
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.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 = layoutInflater.inflate(R.layout.item, null);
			viewHolder.title = (TextView) convertView
					.findViewById(R.id.tv_title);
			viewHolder.content = (TextView) convertView
					.findViewById(R.id.tv_content);
			viewHolder.imageView = (ImageView) convertView
					.findViewById(R.id.imageview);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		String imageViewUrl = mList.get(position).imageViewUrl;
		//进行绑定--不会出现图片错位现象
		viewHolder.imageView.setTag(imageViewUrl);
		/**
		 * 进行图片加载的2种方式
		 */
		new ImageLoader().showImageByThread(viewHolder.imageView, mList.get(position).imageViewUrl);
		mImageLoader.showImageByAsyncTask(viewHolder.imageView, imageViewUrl);
		viewHolder.title.setText(mList.get(position).title);
		viewHolder.content.setText(mList.get(position).content);

		return convertView;
	}

	class ViewHolder {
		public TextView title;
		public TextView content;
		public ImageView imageView;
	}

	/**
	 * 状态改变,进行调用,初始化不会被调用
	 */
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if (scrollState == SCROLL_STATE_IDLE) {
			//停止状态,进行加载图片
			mImageLoader.loadImages(mStart, mEnd);
		}else {
			//停止所有的下载任务
			mImageLoader.cancelAllTasks();
		}
	}

	/**
	 * 滑动过程中调用---第一次加载就可以调用
	 */
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		//获取listview可见的起始项目
		mStart = firstVisibleItem;
		mEnd = firstVisibleItem + visibleItemCount;
		if (mFirst && visibleItemCount>0) {
			//手动加载第一屏
			mImageLoader.loadImages(mStart, mEnd);
			mFirst = false;
		}
	}

}

ImageLoader

package com.example.asynctask;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;

import android.R.integer;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v4.util.LruCache;
import android.widget.ImageView;
import android.widget.ListView;

public class ImageLoader {
	private ImageView mImageView;
	private String mUrl;
	// 图片缓存算法
	private LruCache<String, Bitmap> mCache;
	private ListView mListView;
	private Set<NewsAsyncTask> mTasks;// ??

	/**
	 * @param listView
	 */
	public ImageLoader(ListView listView) {
		this.mListView = listView;
		mTasks = new HashSet<ImageLoader.NewsAsyncTask>();

		// 获取最大可用内存
		int maxMemory = (int) Runtime.getRuntime().maxMemory();
		int cacheSize = maxMemory / 4;
		// 创建缓存
		mCache = new LruCache<String, Bitmap>(cacheSize) {
			@Override
			protected int sizeOf(String key, Bitmap value) {
				// 在每次存入缓存的时候,调用
				return value.getByteCount();
			}
		};
	}

	/**
	 * 
	 * @param imageView
	 * @param url
	 */
	public void showImageByAsyncTask(ImageView imageView, String url) {
		// 获取图片时候,先从缓存取出对应的图片
		Bitmap bitmap = getBitmapFromCache(url);
		// 如果缓存中没有,就显示一个默认图片即可
		if (bitmap == null) {
			imageView.setImageResource(R.drawable.ic_launcher);
		} else {
			// 如果有的话,直接设置图片就可以了
			imageView.setImageBitmap(bitmap);
		}
	}

	/**
	 * 添加到缓存中
	 */
	public void addBitmapToCache(String url, Bitmap bitmap) {
		// 如果某个url对应的图片不存在,为null,就直接存放即可
		if (getBitmapFromCache(url) == null) {
			mCache.put(url, bitmap);
		}
	}

	/**
	 * 从缓存中取出图片--通过url获取图片
	 */
	public Bitmap getBitmapFromCache(String url) {
		return mCache.get(url);
	}

	/**
	 * 加载了从start到end的图片的url
	 */
	public void loadImages(int start, int end) {
		for (int i = start; i < end; i++) {
			String url = NewAdapter.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);
			}
		}
	}

	private class NewsAsyncTask extends AsyncTask<String, Void, Bitmap> {

		private String url;

		public NewsAsyncTask(String url) {
			this.url = url;
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			// 获取图片的url地址
			String url = params[0];
			// 从网络上获取图片
			Bitmap bitmap = getBitmapFromUrl(url);
			// 如果图片不为null
			if (bitmap != null) {
				// 加入到缓存中
				addBitmapToCache(url, bitmap);

			}
			return bitmap;
			// return getBitmapFromUrl(params[0]);
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			super.onPostExecute(result);
			ImageView imageView = (ImageView) mListView.findViewWithTag(url);
			if (imageView != null && result != null) {
				imageView.setImageBitmap(result);

			}
		}
	}

	/**
	 * 取消运行的任务
	 */
	public void cancelAllTasks() {
		if (mTasks != null) {
			for (NewsAsyncTask task : mTasks) {
				task.cancel(false);

			}

		}
	}

	/****************************************************************
	 * 处理handler发送过来的消息--携带bitmap
	 */
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (mImageView.getTag().equals(mUrl)) {
				// 处理消息
				mImageView.setImageBitmap((Bitmap) msg.obj);

			}
		};
	};

	public ImageLoader() {
		super();
	}

	/**
	 * @param imageView
	 *            图片视图
	 * @param url
	 *            图片的url地址
	 */
	public void showImageByThread(ImageView imageView, final String url) {
		this.mImageView = imageView;
		this.mUrl = url;
		// 另开线程进行服务器的图片下载
		new Thread() {
			public void run() {
				// 通过url获取图片
				Bitmap bitmap = getBitmapFromUrl(url);
				Message message = Message.obtain();
				message.obj = bitmap;
				// 通过handler发送消息
				handler.sendMessage(message);
			};
		}.start();
	}

	/**
	 * 通过url获取网络图片
	 * 
	 * @param urlString
	 * @return
	 */
	public Bitmap getBitmapFromUrl(String urlString) {
		Bitmap bitmap = null;
		InputStream inputStream = null;
		try {
			URL httpUrl = new URL(urlString);
			HttpURLConnection connection = (HttpURLConnection) httpUrl
					.openConnection();
			inputStream = new BufferedInputStream(connection.getInputStream());
			bitmap = BitmapFactory.decodeStream(inputStream);
			connection.disconnect();
			return bitmap;

		} catch (Exception e) {
		} finally {
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return bitmap;

	}

	/**********************************************************
	 * 
	 * @param imageView
	 * @param url
	 */

}


版权声明:本文为博主原创文章,未经博主允许不得转载。

异步任务及其Listview优化的种种

标签:

原文地址:http://blog.csdn.net/u013210620/article/details/47218621

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!