首页 > 代码库 > android自带的处理Bitmap out Memory 的处理,第三方开源的那个更方便,自己练习的话还是很好的

android自带的处理Bitmap out Memory 的处理,第三方开源的那个更方便,自己练习的话还是很好的

/** 
 * @author intbird@163.com 
 * @time 20140606 
 */ 
package com.intbird.utils;

import java.lang.ref.WeakReference;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.os.AsyncTask;
import android.widget.ImageView;

public class BitmapHelper {
	
	private CacheManager cacheManager;
	
	public static final int IMG_TYPE_RES=0;
	public static final int IMG_TYPE_WEB=1;
	public static final int IMG_TYPE_FILE=3;
	
	private Bitmap bmpHolder=null;
	private Bitmap bmpNoImg=null;
	
	public BitmapHelper(Context context,int loadingId,int loadNoId){
		cacheManager=CacheManager.getInstance();
		bmpHolder=BitmapFactory.decodeResource(context.getResources(),loadingId);
		bmpNoImg=BitmapFactory.decodeResource(context.getResources(),loadNoId);
	}
	
	/**
	 * 加载图片
	 * @param type 网络文件,本地文件,还是资源文件
	 * @param fileUrl url
	 * @param imageView 控件
	 * @param width 要获取的指定高度
	 * @param height 要获取的指定宽度
	 */
	public void commonLoadBitmap(int type,String fileUrl,ImageView imageView,int width,int height){
		//内存和文件中没有图片,重新获取;
		Bitmap bmp=cacheManager.getBitmapFromCache(fileUrl);
		if(bmp!=null){
			imageView.setImageBitmap(bmp);
		}
		else{
			switch(type){
			case IMG_TYPE_WEB:
				loadMultiBitmapFromWeb(fileUrl, imageView,width,height);
				break;
			case IMG_TYPE_FILE:
				imageView.setImageBitmap(getBitmapFormFile(	fileUrl, width, height, true));
				break;
			case IMG_TYPE_RES:
				imageView.setImageResource(Integer.parseInt(fileUrl));
				break;
			}
		}
	}
	
	/**
	 * 设置ImageView为获取指定文件地址的指定高度指定宽度的图片; 
	 * @param fileUrl 文件地址
	 * @param reqWidth 目标宽度
	 * @param reqHeight 目标高度
	 * @return bitmap
	 */
	public Bitmap loadSingleBitmapFromFile(String fileUrl,ImageView iv,int reqWidth,int reqHeight){
		BitmapFactory.Options options=new BitmapFactory.Options();
		options.inJustDecodeBounds=true;
		options.inSampleSize=calculateInSampleSize(options, reqWidth, reqHeight);
		BitmapFactory.decodeFile(fileUrl,options);
		options.inJustDecodeBounds=false;
		Bitmap bmp=BitmapFactory.decodeFile(fileUrl,options);
		if(iv!=null) iv.setImageBitmap(bmp);
		return 	bmp;
	}
	/**
	 * 设置ImageView为需要加载的一张网络图片;
	 * @param webUrl
	 * @param imageView
	 * @param width
	 * @param heigth
	 */
	public void loadSingleBitmapFromWeb(String webUrl,ImageView imageView,int width,int heigth){
		BitmapHelper.BitmapWorkerSingleTask task=new BitmapWorkerSingleTask(imageView);
		task.execute(webUrl,width+"",heigth+"");
	}

	/**
	 * adapter中加载图片
	 * @param fileUrl 图片地址
	 * @param imageView 图片控件
	 * @param width 要得到的宽度
	 * @param height 要的到的高度
	 */
	public void loadMultiBitmapFromWeb(String fileUrl, ImageView imageView,int width,int height) {
		
	    if (cancelPotentialWork(fileUrl, imageView)) {
	    	
	        final BitmapMultiWorkerTask task = new BitmapMultiWorkerTask(imageView);
	        
	        final AsyncDrawable asyncDrawable =new AsyncDrawable(bmpHolder, task);
	        
	        imageView.setImageDrawable(asyncDrawable);
	        
	        task.execute(fileUrl,width+"",height+"");
	    }
	}
	
	/**
	 * 从网络中加载一个需要的Bitmap;
	 * @param webUrl
	 * @param reqWidth
	 * @param reqHeight
	 * @return
	 */
	public Bitmap getBitmapFormWeb(String webUrl,int reqWidth,int reqHeight){
		BitmapFactory.Options options=new BitmapFactory.Options();
		options.inJustDecodeBounds=true;
		options.inSampleSize=calculateInSampleSize(options, reqWidth, reqHeight);
		return ConnInternet.loadBitmapFromNet(webUrl, options);
	}

	/**
	 * 将图片文件转换成bitmap
	 * 
	 * @param fileUrl
	 *            图片文件路径
	 * @param width
	 *            宽度
	 * @param height
	 *            高度
	 * @param isThumbnail
	 *            是否根据高宽生成缩略图
	 * @return
	 */
	public Bitmap getBitmapFormFile(String fileUrl, int width, int height,
			boolean isThumbnail) {
		Bitmap bitmap=loadSingleBitmapFromFile(fileUrl,null, width, height);
		// 生成固定尺寸的缩略图
		if (isThumbnail) {
			bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
					ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
		}
		cacheManager.addBitmapToCache(fileUrl, bitmap);
		return bitmap;
	}

	/**
	 * 转换图片成圆形
	 * 
	 * @param bitmap 
	 * @return
	 */
	public static Bitmap changeBitmapToRound(Bitmap bitmap) {
		if(bitmap==null)
			return null;
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		float roundPx;
		float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
		if (width <= height) {
			roundPx = width / 2;
			left = 0;
			top = 0;
			right = width;
			bottom = width;
			height = width;
			dst_left = 0;
			dst_top = 0;
			dst_right = width;
			dst_bottom = width;
		} else {
			roundPx = height / 2;
			float clip = (width - height) / 2;
			left = clip;
			right = width - clip;
			top = 0;
			bottom = height;
			width = height;
			dst_left = 0;
			dst_top = 0;
			dst_right = height;
			dst_bottom = height;
		}
	
		Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
	
		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect src = http://www.mamicode.com/new Rect((int) left, (int) top, (int) right,>