图片读取工具类


package com.ldw.dreamblinkfeed.util;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

/**
 * 图片工具类
 * @author ldw
 */
public class ImageUtil
{
	private static BitmapFactory.Options opt = null;
	
	/**
	 * 从Res加载图片为Bitmap
	 * @param context	
	 * @param resId	图片的id
	 * @return	565格式的图片
	 */
	@SuppressWarnings("deprecation")
	public static Bitmap getBitmap(Context context, int resId){
		if(opt == null){
			opt = new BitmapFactory.Options();
			opt.inPreferredConfig = Bitmap.Config.RGB_565;
			opt.inPurgeable = true;
		}
		InputStream is = context.getResources().openRawResource(resId);
		return BitmapFactory.decodeStream(is, null, opt);
	}
	
	/**
	 * 从Res加载图片为Drawable
	 * @param context	
	 * @param resId	图片的id
	 * @return	565格式的图片
	 */
	public static BitmapDrawable getDrawable(Context context, int resId) {
		Bitmap bmp = getBitmap(context, resId);
		if (bmp != null){
			return new BitmapDrawable(context.getResources(), bmp);
		}
		return null;
	}
	
	/**
	 * 从Assets加载图片为Bitmap
	 * @param context
	 * @param filePath
	 * @return 565格式的图片
	 */
	@SuppressWarnings("deprecation")
	public static Bitmap getBitmapFromAssets(Context context, String filePath){
		InputStream input = null;
		Bitmap bitmap = null;
		try {
			input = context.getResources().getAssets().open(filePath);
			if(opt == null){
				opt = new BitmapFactory.Options();
				opt.inPreferredConfig = Bitmap.Config.RGB_565;
				opt.inPurgeable = true;
			}
		    bitmap = BitmapFactory.decodeStream(input, null, opt);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
	    	try {
	    		if(input != null){
	    			input.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	    return bitmap;
	}
	
	/**
	 * 从本地加载图片为Bitmap
	 * @param context
	 * @param filePath 本地存储路径
	 * @return 565格式的图片
	 */
	@SuppressWarnings("deprecation")
	public static Bitmap getBitmapFromLocal(Context context, String filePath) {
		try {
			if(opt == null){
				opt = new BitmapFactory.Options();
				opt.inPreferredConfig = Bitmap.Config.RGB_565;
				opt.inPurgeable = true;
			}
			return BitmapFactory.decodeFile(filePath, opt);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 从本地加载图片为Drawable
	 * @param context
	 * @param filePath 本地存储路径
	 * @return 565格式的图片
	 */
	public static Drawable getDrawableFromLocal(Context context, String filePath) {
		Bitmap bmp = getBitmapFromLocal(context, filePath);
		if (bmp != null){
			return new BitmapDrawable(context.getResources(), bmp);
		}
		return null;
	}
	
	/**
	 * 将图片截取为圆角图片
	 * @param bitmap 原图片
	 * @param ratio 截取比例,如果是8,则圆角半径是宽高的1/8,如果是2,则是圆形图片
	 * @param isRecyleSrcBm 释放传入的bitmap,true则释放,否则用户自己释放
	 * @return 圆角矩形图片,用户记得释放资源
	 */
	public static Bitmap getRoundCornerBitmap(Bitmap bitmap, float ratio, boolean isRecyleSrcBm) {
	    //出错处理
		if (bitmap == null || bitmap.isRecycled()) {
			return null;
		}
		//图片宽高出错
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		if (width <= 0 || height <= 0) {
			return null;
		}
		//半径处理
		if (ratio <= 0.0f) {
			ratio = 1;
		}
		//边角处理
        Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, width, height);
        final RectF rectF = new RectF(rect);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        float rx = width / ratio;
        float ry = height / ratio;
        canvas.drawRoundRect(rectF, rx, ry, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        //释放源bitmap
        if (isRecyleSrcBm) {
        	bitmap.recycle();
        }
        return output;
	}
	
	//////////////////////////////以下为一些辅助功能//////////////////////////////
	
	/**
	 * Bitmap转换为Drawable
	 * @param context
	 * @param bitmap
	 * @return
	 */
	public static Drawable getDrawableFromBitmap(Context context, Bitmap bitmap){
		if(bitmap != null && !bitmap.isRecycled()){
			return new BitmapDrawable(context.getResources(), bitmap);
		}else{
			return null;
		}
	}
	
	/**
	 * Drawable转换为Bitmap
	 * @param drawable
	 * @return
	 */
	public static Bitmap getBitmapFromDrawable(Drawable drawable){
		BitmapDrawable bd = (BitmapDrawable) drawable;
		return bd.getBitmap();
	}
	
	/**
	 * 获取assets下文件夹里的图片名称
	 * @param context
	 * @param FolderPath	文件夹路径
	 * @return
	 */
	public static String[] getAssetsFileList(Context context, String FolderPath){
		String [] fileLists = null;
		try {
			fileLists = context.getAssets().list(FolderPath);
			for(int i = 0; i < fileLists.length; i++){
				fileLists[i] = FolderPath + "/" + fileLists[i];
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fileLists;
	}
	
	/**
	 * 释放Bitmap资源
	 * @param bitmap	释放的对象
	 */
	public static void recycleBitmap(Bitmap bitmap){
		if(bitmap != null){
			if(!bitmap.isRecycled()){
				bitmap.recycle();
			}
			bitmap = null;
		}
	}
	/**
	 * 释放Bitmap数组资源
	 * @param bitmaps
	 */
	public static void releaseBitmaps(Bitmap[] bitmaps){
		for(int i=0; i<bitmaps.length; i++){
			recycleBitmap(bitmaps[i]);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值