Android图像办理组件

[文件] ImageManager2.java ~ 13KB    下载(5) package com.example.util;

import java.io.File;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

 http://www.fp1111.info/linked/20130304.do; 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.media.ThumbnailUtils;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.v4.util.LruCache;
import android.widget.ImageView;

import com.example.MyApplication;

/**
 * 图像加载类
 * 
 * @author 月月鸟
 */
public class ImageManager2 {

	private static ImageManager2 imageManager;
	public LruCache mMemoryCache;
	private static final int DISK_CACHE_SIZE = 1024 * 1024 * 20; // 10MB
	private static final String DISK_CACHE_SUBDIR = "thumbnails";
	public DiskLruCache mDiskCache;
	private static MyApplication myapp;

	/** 图像加载行列,后进先出 */
	private Stack mImageQueue = new Stack();

	/** 图像恳求行列,先进先出,用于寄存已发送的恳求。 */
	private Queue mRequestQueue = new LinkedList();

	/** 图像加载线程音讯处理器 */
	private Handler mImageLoaderHandler;

	/** 图像加载线程能否安排妥当 */
	private boolean mImageLoaderIdle = true;

	/** 恳求图像 */
	private static final int MSG_REQUEST = 1;
	/** 图像加载完结 */
	private static final int MSG_REPLY = 2;
	/** 间断图像加载线程 */
	private static final int MSG_STOP = 3;
	/** 若是图像是从网络加载,则运用渐显动画,若是从缓存读出则不运用动画 */
	private boolean isFromNet = true;

	/**
	 * 获取单例,只能在UI线程中运用。
	 * 
	 * @param context
	 * @return
	 */
	public static ImageManager2 from(Context context) {

		// 若是不在ui线程中,则抛出反常
		if (Looper.myLooper() != Looper.getMainLooper()) {
			throw new RuntimeException("Cannot instantiate outside UI thread.");
		}

		if (myapp == null) {
			myapp = (MyApplication) context.getApplicationContext();
		}

		if (imageManager == null) {
			imageManager = new ImageManager2(myapp);
		}

		return imageManager;
	}

	/**
	 * 私有结构函数,确保单例形式
	 * 
	 * @param context
	 */
	private ImageManager2(Context context) {
		int memClass = ((ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
		memClass = memClass > 32 ? 32 : memClass;
		// 运用可用内存的1/8作为图像缓存
		final int cacheSize = 1024 * 1024 * memClass / 8;

		mMemoryCache = new LruCache(cacheSize) {

			protected int sizeOf(String key, Bitmap bitmap) {
				return bitmap.getRowBytes() * bitmap.getHeight();
			}

		};

		File cacheDir = DiskLruCache
				.getDiskCacheDir(context, DISK_CACHE_SUBDIR);
		mDiskCache = DiskLruCache.openCache(context, cacheDir, DISK_CACHE_SIZE);

	}

	/**
	 * 寄存图像信息
	 */
	class ImageRef {

		/** 图像对应ImageView控件 */
		ImageView imageView;
		/** 图像URL地址 */
		String url;
		/** 图像缓存途径 */
		String filePath;
		/** 默许图资源ID */
		int resId;
		int width = 0;
		int height = 0;

		/**
		 * 结构函数
		 * 
		 * @param imageView
		 * @param url
		 * @param resId
		 * @param filePath
		 */
		ImageRef(ImageView imageView, String url, String filePath, int resId) {
			this.imageView = imageView;
			this.url = url;
			this.filePath = filePath;
			this.resId = resId;
		}

		ImageRef(ImageView imageView, String url, String filePath, int resId,
				int width, int height) {
			this.imageView = imageView;
			this.url = url;
			this.filePath = filePath;
			this.resId = resId;
			this.width = width;
			this.height = height;
		}

	}

	/**
	 * 显现图像
	 * 
	 * @param imageView
	 * @param url
	 * @param resId
	 */
	public void displayImage(ImageView imageView, String url, int resId) {
		if (imageView == null) {
			return;
		}
		if (imageView.getTag() != null
				 http://www.1111kp.info/linked/20130304.do; 
posted @ 2013-03-05 02:31  chinadiy197601  阅读(270)  评论(0编辑  收藏  举报