首页 > 代码库 > Drawable与Bitmap(转)

Drawable与Bitmap(转)

Drawable                                                                                

以下这个是测试加载1000个Drawable对象的代码:

public class Main extends Activity{    int number = 1000;    Drawable[] array;     @Override    public void onCreate(Bundle savedInstanceState)    {       super.onCreate(savedInstanceState);       setContentView(R.layout.main);        array = new BitmapDrawable[number];                             for(int i = 0; i < number; i++)        {            Log.e("", "测试第" + (i+1) + "张图片");            array[i] = getResources().getDrawable(R.drawable.img);        }    }}

输出结果:

04-07 21:49:25.248: D/szipinf(7828): Initializing inflate state04-07 21:49:25.398: E/(7828): 测试第1张图片04-07 21:49:25.658: D/dalvikvm(7828): GC_EXTERNAL_ALLOC freed 48K, 50% free 2692K/5379K, external 0K/0K, paused 24ms04-07 21:49:25.748: E/(7828): 测试第2张图片04-07 21:49:25.748: E/(7828): 测试第3张图片………………………………04-07 21:49:26.089: E/(7828): 测试第998张图片04-07 21:49:26.089: E/(7828): 测试第999张图片04-07 21:49:26.089: E/(7828): 测试第1000张图片

程序没有报错,正常运行,加载1000个Drawable对象没问题。

Bitmap                                                                                    

下面再来看一下加载1000个Bitmap对象的代码:

public class Main extends Activity{    int number = 1000;    Bitmap bitmap[];    @Override    public void onCreate(Bundle savedInstanceState)    {       super.onCreate(savedInstanceState);       setContentView(R.layout.main);       bitmap = new Bitmap[number];       for (int i = 0; i < number; i++)       {           Log.e("", "测试第" + (i+1) + "张图片");           bitmap[i] = BitmapFactory.decodeResource(getResources(), R.drawable.img);       }    }}

输出结果:

04-07 22:06:05.344: D/szipinf(7937): Initializing inflate state04-07 22:06:05.374: E/(7937): 测试第1张图片04-07 22:06:05.544: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed 51K, 50% free 2692K/5379K, external 0K/0K, paused 40ms04-07 22:06:05.664: E/(7937): 测试第2张图片04-07 22:06:05.774: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed 1K, 50% free 2691K/5379K, external 6026K/7525K, paused 31ms04-07 22:06:05.834: E/(7937): 测试第3张图片04-07 22:06:05.934: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external 12052K/14100K, paused 24ms04-07 22:06:06.004: E/(7937): 测试第4张图片04-07 22:06:06.124: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external 18078K/20126K, paused 27ms04-07 22:06:06.204: E/(7937): 测试第5张图片04-07 22:06:06.315: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external 24104K/26152K, paused 26ms04-07 22:06:06.395: E/(7937): 测试第6张图片04-07 22:06:06.495: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external 30130K/32178K, paused 22ms04-07 22:06:06.565: E/(7937): 测试第7张图片04-07 22:06:06.665: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external 36156K/38204K, paused 22ms04-07 22:06:06.745: E/(7937): 测试第8张图片04-07 22:06:06.845: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed 2K, 51% free 2689K/5379K, external 42182K/44230K, paused 23ms04-07 22:06:06.845: E/dalvikvm-heap(7937): 6170724-byte external allocation too large for this process.04-07 22:06:06.885: I/dalvikvm-heap(7937): Clamp target GC heap from 48.239MB to 48.000MB04-07 22:06:06.885: E/GraphicsJNI(7937): VM won‘t let us allocate 6170724 bytes04-07 22:06:06.885: D/dalvikvm(7937): GC_FOR_MALLOC freed <1K, 51% free 2689K/5379K, external 42182K/44230K, paused 25ms04-07 22:06:06.885: D/AndroidRuntime(7937): Shutting down VM04-07 22:06:06.885: W/dalvikvm(7937): threadid=1: thread exiting with uncaught exception (group=0x40015560)04-07 22:06:06.885: E/AndroidRuntime(7937): FATAL EXCEPTION: main04-07 22:06:06.885: E/AndroidRuntime(7937): java.lang.OutOfMemoryError: bitmap size exceeds VM budget04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.graphics.Bitmap.nativeCreate(Native Method)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.graphics.Bitmap.createBitmap(Bitmap.java:477)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.graphics.Bitmap.createBitmap(Bitmap.java:444)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:359)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:385)04-07 22:06:06.885: E/AndroidRuntime(7937):   at bassy.test.drawable.Main.onCreate(Main.java:37)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.app.ActivityThread.access$1500(ActivityThread.java:123)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.os.Handler.dispatchMessage(Handler.java:99)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.os.Looper.loop(Looper.java:130)04-07 22:06:06.885: E/AndroidRuntime(7937):   at android.app.ActivityThread.main(ActivityThread.java:3835)04-07 22:06:06.885: E/AndroidRuntime(7937):   at java.lang.reflect.Method.invokeNative(Native Method)04-07 22:06:06.885: E/AndroidRuntime(7937):   at java.lang.reflect.Method.invoke(Method.java:507)04-07 22:06:06.885: E/AndroidRuntime(7937):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)04-07 22:06:06.885: E/AndroidRuntime(7937):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)04-07 22:06:06.885: E/AndroidRuntime(7937):   at dalvik.system.NativeStart.main(Native Method)

看看上面的输出,才加载到第8张图片,程序就报错了“java.lang.OutOfMemoryError: bitmap size exceeds VM budget”。通过上面的例子,可以看清楚地看出来,使用Drawable保存图片对象,占用更小的内存空间。而使用Biamtp对象,则会占用很大内存空间,很容易就出现OOM了!

下面我们再来看一个例子,这个也是加载Bitmap对象。只不过,之次不是使用BitmapFactory的decodeResource方法,而是使用decodeStream方法,看代码。

public class Main extends Activity{    int number = 1000;    Bitmap bitmap[];     @Override    public void onCreate(Bundle savedInstanceState)    {       super.onCreate(savedInstanceState);       setContentView(R.layout.main);        bitmap = new Bitmap[number];        for (int i = 0; i < number; i++)       {           Log.e("", "测试第" + (i+1) + "张图片");           bitmap[i] =BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.img));//这里换了方法       }    }}

输出结果:

04-07 22:16:12.676: E/(8091): 测试第561张图片04-07 22:16:12.756: E/(8091): 测试第562张图片04-07 22:16:12.826: E/(8091): 测试第563张图片04-07 22:16:12.906: E/(8091): 测试第564张图片04-07 22:16:12.906: D/skia(8091): ---------- mmap failed for imageref_ashmem size=2744320 err=1204-07 22:16:12.906: E/(8091): 测试第565张图片04-07 22:16:12.906: D/skia(8091): ---------- mmap failed for imageref_ashmem size=2744320 err=1204-07 22:16:12.906: E/(8091): 测试第566张图片04-07 22:16:12.916: E/filemap(8091): mmap(0,416798) failed: Out of memory04-07 22:16:12.916: D/filemap(8091): munmap(0x0, 0) failed04-07 22:16:12.916: W/asset(8091): create map from entry failed04-07 22:16:12.916: D/AndroidRuntime(8091): Shutting down VM04-07 22:16:12.916: W/dalvikvm(8091): threadid=1: thread exiting with uncaught exception (group=0x40015560)04-07 22:16:12.936: E/AndroidRuntime(8091): FATAL EXCEPTION: main04-07 22:16:12.936: E/AndroidRuntime(8091): java.lang.RuntimeException: Unable to start activity ComponentInfo{bassy.test.drawable/bassy.test.drawable.Main}: android.content.res.Resources$NotFoundException: File res/drawable-mdpi/img.png from drawable resource ID #0x7f02000104-07 22:16:12.936: E/AndroidRuntime(8091):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)04-07 22:16:12.936: E/AndroidRuntime(8091):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)04-07 22:16:12.936: E/AndroidRuntime(8091):   at android.app.ActivityThread.access$1500(ActivityThread.java:123)04-07 22:16:12.936: E/AndroidRuntime(8091):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)04-07 22:16:12.936: E/AndroidRuntime(8091):   at android.os.Handler.dispatchMessage(Handler.java:99)04-07 22:16:12.936: E/AndroidRuntime(8091):   at android.os.Looper.loop(Looper.java:130)04-07 22:16:12.936: E/AndroidRuntime(8091):   at android.app.ActivityThread.main(ActivityThread.java:3835)04-07 22:16:12.936: E/AndroidRuntime(8091):   at java.lang.reflect.Method.invokeNative(Native Method)04-07 22:16:12.936: E/AndroidRuntime(8091):   at java.lang.reflect.Method.invoke(Method.java:507)04-07 22:16:12.936: E/AndroidRuntime(8091):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)04-07 22:16:12.936: E/AndroidRuntime(8091):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)04-07 22:16:12.936: E/AndroidRuntime(8091):   at dalvik.system.NativeStart.main(Native Method)04-07 22:16:12.936: E/AndroidRuntime(8091): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-mdpi/img.png from drawable resource ID #0x7f02000104-07 22:16:12.936: E/AndroidRuntime(8091):   at android.content.res.Resources.openRawResource(Resources.java:860)04-07 22:16:12.936: E/AndroidRuntime(8091):   at android.content.res.Resources.openRawResource(Resources.java:836)04-07 22:16:12.936: E/AndroidRuntime(8091):   at bassy.test.drawable.Main.onCreate(Main.java:43)04-07 22:16:12.936: E/AndroidRuntime(8091):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)04-07 22:16:12.936: E/AndroidRuntime(8091):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)04-07 22:16:12.936: E/AndroidRuntime(8091):   ... 11 more04-07 22:16:12.936: E/AndroidRuntime(8091): Caused by: java.io.FileNotFoundException: res/drawable-mdpi/img.png04-07 22:16:12.936: E/AndroidRuntime(8091):   at android.content.res.AssetManager.openNonAssetNative(Native Method)04-07 22:16:12.936: E/AndroidRuntime(8091):   at android.content.res.AssetManager.openNonAsset(AssetManager.java:429)04-07 22:16:12.936: E/AndroidRuntime(8091):   at android.content.res.Resources.openRawResource(Resources.java:857)04-07 22:16:12.936: E/AndroidRuntime(8091):   ... 15 more

从上面可以看出,程序在加载到第566张的时候,就出现了OOM错误。不过,跟第2个例子比起来,你会发现,程序可以加载更多的图片。这说明了使用BitmapFactory的decodeResource方法会占据大量内存,而使用使用decodeStream方法,则占据更小的内存。

我是天王盖地虎的分割线                                                                

从上面三个例子,可以看出,用第一种方法(即用Drawable加载图片)可以加载更加的图片,加载32张图片的时间约为0.01秒!我试着把Drawable的数量调至1000000,程序在运行时,停在了153761张图片里,手机提示,“应用程序无响应…”

个人猜测,Drawable应该不属于常驻内存的对象,不然的话,不可能不会出现OOM的~~

刚才又做了个测试,把第一个例子中的

array[i] = getResources().getDrawable(R.drawable.img);

方法换成了

array[i] = Drawable.createFromStream(getResources().openRawResource(R.drawable.img), null);

结果和第三个例子一样,在第566张图片中,出现了OOM错误!而且,加载的时间都是一样~~

我是天王盖地虎的分割线                                                                 

  • Bitmap转Drawable
Bitmap bm=xxx; //xxx根据你的情况获取BitmapDrawable bd=BitmapDrawable(bm);

  Android开发网提示因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。

  • Drawable转Bitmap

  转成Bitmap对象后,可以将Drawable对象通过Android的SK库存成一个字节输出流,最终还可以保存成为jpg和png的文件。

Drawable d=xxx; //xxx根据自己的情况获取drawableBitmapDrawable bd = (BitmapDrawable) d;Bitmap bm = bd.getBitmap();

最终bm就是我们需要的Bitmap对象了。

 

 

 

 

 

参考:http://blog.csdn.net/ygc973797893/article/details/7620352