Bitmap以最省内存的方式获取本地资源,转换drawable到bitmap

来源:互联网 发布:js设置input不允许输入 编辑:程序博客网 时间:2024/05/16 10:59
public final class BitmapUtils {    /**     * 清空ImageView中的图片的内存     */    public static void clearImageMemory(View view) {        if (view != null && view instanceof ImageView) {            Drawable d = ((ImageView)view).getDrawable();            if (d != null && d instanceof BitmapDrawable) {                Bitmap bmp = ((BitmapDrawable)d).getBitmap();                bmp.recycle();                System.gc();            }            ((ImageView)view).setImageDrawable(null);            if (d != null) {                d.setCallback(null);            }        }    }    /**     * 以最省内存的方式读取本地资源的图片     *     * @param context Context     * @param resId   图片资源ID     * @return Bitmap     */    @SuppressWarnings("deprecation")    public static Bitmap readBitmap(Context context, int resId) {        BitmapFactory.Options opt = new BitmapFactory.Options();        opt.inPreferredConfig = Bitmap.Config.RGB_565;        opt.inPurgeable = true;        opt.inInputShareable = true;        // 获取资源图片        InputStream is = context.getResources().openRawResource(resId);        return BitmapFactory.decodeStream(is, null, opt);    }    /**     * 转换DrawableBitmap     *     * @param drawable Drawable     * @return 转换DrawableBitmap     */    public static Bitmap drawableToBitmap(Drawable drawable) {        if (drawable != null && drawable instanceof BitmapDrawable) {            BitmapDrawable bd = (BitmapDrawable)drawable;            return bd.getBitmap();        }        return null;    }}
0 0