package com.wasu.cs.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.InputStream;

/**
 * 节省资源加载本地图片
 */

public class BitmapUtils {

    /**
     * 以最省内存的方式读取本地资源的图片
     *
     * @param context
     * @param resId
     * @return
     */
    public static Bitmap readBitMapByDecodeStream(Context context,int resId) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inPurgeable = true;
        options.inInputShareable = true;
        //获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, options);
    }

    public static Bitmap readBitMapByDecodeStream(Context context,int resId,Bitmap.Config rgb) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = rgb;
        options.inPurgeable = true;
        options.inInputShareable = true;
        //获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, options);
    }


    public static Bitmap readBitMapByDecodeResourse(Context context,int resId) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inPurgeable = true;
        options.inInputShareable = true;
        //获取资源图片
        return BitmapFactory.decodeResource(context.getResources(), resId, options);
    }


    /**
     * 回收bitm
     * @param bitmap
     */
    public static void recycleBitmap(Bitmap bitmap) {
        if (bitmap != null) {
            bitmap.recycle();
        }
    }


}
