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); } /** * 转换Drawable到Bitmap * * @param drawable Drawable * @return 转换Drawable到Bitmap */ public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable != null && drawable instanceof BitmapDrawable) { BitmapDrawable bd = (BitmapDrawable)drawable; return bd.getBitmap(); } return null; } }
Bitmap以最省内存的方式获取本地资源,转换drawable到bitmap
最新推荐文章于 2021-02-16 11:22:49 发布