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

/** 
 * @author [email protected] 
 * @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 = new Rect((int) left, (int) top, (int) right,
				(int) bottom);
		final Rect dst = new Rect((int) dst_left, (int) dst_top,
				(int) dst_right, (int) dst_bottom);
		paint.setAntiAlias(true);// 设置画笔无锯齿
	
		canvas.drawARGB(0, 0, 0, 0); // 填充整个Canvas
		paint.setColor(color);
	
		// 以下有两种方法画圆,drawRounRect和drawCircle
		// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);//
		// 画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。
		canvas.drawCircle(roundPx, roundPx, roundPx, paint);
	
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 设置两张图片相交时的模式,参考http://trylovecatch.iteye.com/blog/1189452
		canvas.drawBitmap(bitmap, src, dst, paint); // 以Mode.SRC_IN模式合并bitmap和已经draw了的Circle
		return output;
	}

	/**
	 * 计算所需大小的图片 的压缩比例
	 * @param options
	 * @param reqWidth 所需宽度
	 * @param reqHeight 所需高度
	 * @return 压缩比例
	 */
	public  int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
	    final int height = options.outHeight;
	    final int width = options.outWidth;
	    int inSampleSize = 1;
	    if (height > reqHeight || width > reqWidth) {
	        final int halfHeight = height / 2;
	        final int halfWidth = width / 2;
	    	while ((halfHeight / inSampleSize) > reqHeight
	            && (halfWidth / inSampleSize) > reqWidth) {
	    		inSampleSize *= 2;
	    	}
		}
		return inSampleSize;
	}

	/**
	 * 检查启动的任务
	 * @param fileUrl 文件路径
	 * @param imageView 目标控件
	 * @return 是否开始加载任务
	 */
	private  boolean cancelPotentialWork(String fileUrl, ImageView imageView) {
	
	    final BitmapMultiWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
	    
	    if (bitmapWorkerTask != null) {
	    	
	        String bitmapUrl = bitmapWorkerTask.fileUrl;
	        if (bitmapUrl!=null&&bitmapUrl != fileUrl) {
	        	
	            bitmapWorkerTask.cancel(true);
	            return true;
	        }
	        
	        else {
	            return false;
	        }
	    }
	    return true;
	}

	/**
	 * 获取该图片控件中的加载任务
	 * @param imageView
	 * @return 返回该任务
	 */
	private  BitmapMultiWorkerTask getBitmapWorkerTask(ImageView imageView) {
		   if (imageView != null) {
		       final Drawable drawable = imageView.getDrawable();
		       if (drawable instanceof AsyncDrawable) {
		           final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
		           return asyncDrawable.getBitmapWorkerTask();
		       }
		    }
		    return null;
	}
	private class AsyncDrawable extends BitmapDrawable {
	    private final WeakReference<BitmapMultiWorkerTask> bitmapWorkerTaskReference;
	    @SuppressWarnings("deprecation")
		public AsyncDrawable(Bitmap bitmap,BitmapMultiWorkerTask bitmapWorkerTask) {
	        super(bitmap);
	        bitmapWorkerTaskReference =new WeakReference<BitmapMultiWorkerTask>(bitmapWorkerTask);
	    }
	    public BitmapMultiWorkerTask getBitmapWorkerTask() {
	        return bitmapWorkerTaskReference.get();
	    }
	}
	private class BitmapMultiWorkerTask extends AsyncTask<String, Void, Bitmap> {
		private WeakReference<ImageView> imageViewReference;
		private String fileUrl;
		public BitmapMultiWorkerTask(ImageView imageView){
			imageViewReference=new WeakReference<ImageView>(imageView);
		}
		@Override
		protected Bitmap doInBackground(String... params) {
			fileUrl=params[0];
			int reqWidth=Integer.valueOf(params[1]);
			int reqHeight=Integer.valueOf(params[2]);
			Bitmap bitmap=getBitmapFormWeb(fileUrl,reqWidth,reqHeight);
			return bitmap;
		}
		
		@Override
	    protected void onPostExecute(Bitmap bitmap) {
	      
	        if (isCancelled()) {
	            bitmap = null;
	        }
	        
	        if (imageViewReference != null) {
	            final ImageView imageView = imageViewReference.get();
	            
	            final BitmapMultiWorkerTask bitmapWorkerTask =getBitmapWorkerTask(imageView);
	           
	            if (this == bitmapWorkerTask && imageView != null) {
	            	
	            	if(bitmap!=null){
	            		imageView.setImageBitmap(bitmap);
	            		cacheManager.addBitmapToCache(fileUrl, bitmap);
	            	}
	            
	            	else{
	            		imageView.setImageBitmap(bmpNoImg);
	            	}
	            }
	        }
	    }
	}
	/**
	 * 异步加载一张图片
	 * @author [email protected]
	 *
	 */
	private class BitmapWorkerSingleTask extends AsyncTask<String, Void, Bitmap>{
		private WeakReference<ImageView> imageViewReference;
		private String fileUrl="";
		public BitmapWorkerSingleTask(ImageView imageView){
			imageViewReference=new WeakReference<ImageView>(imageView);
		}
		@Override
		protected Bitmap doInBackground(String... params) {
			fileUrl=params[0];
			int reqWidth=Integer.valueOf(params[1]);
			int reqHeight=Integer.valueOf(params[2]);
			Bitmap bitmap=getBitmapFormWeb(fileUrl,reqWidth,reqHeight);
			return bitmap;
		}
		@Override
		protected void onPostExecute(Bitmap bitmap) {
			 if (imageViewReference != null) {
		            final ImageView imageView = imageViewReference.get();
		            if (imageView != null) {
		            	if(bitmap!=null){
		            		imageView.setImageBitmap(bitmap);
		            	}
	            	   else{
			            	  imageView.setImageBitmap(bmpNoImg);
			            }
		            }
		    }
		}
	}
}

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。