Android -- 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 state
04-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 24ms
04-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 state
04-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 40ms
04-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 31ms
04-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 24ms
04-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 27ms
04-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 26ms
04-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 22ms
04-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 22ms
04-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 23ms
04-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.000MB
04-07 22:06:06.885: E/GraphicsJNI(7937): VM won‘t let us allocate 6170724 bytes
04-07 22:06:06.885: D/dalvikvm(7937): GC_FOR_MALLOC freed <1K, 51% free 2689K/5379K, external 42182K/44230K, paused 25ms
04-07 22:06:06.885: D/AndroidRuntime(7937): Shutting down VM
04-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: main
04-07 22:06:06.885: E/AndroidRuntime(7937): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
04-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=12
04-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=12
04-07 22:16:12.906: E/(8091): 测试第566张图片
04-07 22:16:12.916: E/filemap(8091): mmap(0,416798) failed: Out of memory
04-07 22:16:12.916: D/filemap(8091): munmap(0x0, 0) failed
04-07 22:16:12.916: W/asset(8091): create map from entry failed
04-07 22:16:12.916: D/AndroidRuntime(8091): Shutting down VM
04-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: main
04-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 #0x7f020001
04-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 #0x7f020001
04-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 more
04-07 22:16:12.936: E/AndroidRuntime(8091): Caused by: java.io.FileNotFoundException: res/drawable-mdpi/img.png
04-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根据自己的情况获取drawable
BitmapDrawable bd = (BitmapDrawable) d;
Bitmap bm = bd.getBitmap();

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

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

Android -- Drawable与Bitmap,布布扣,bubuko.com

时间: 2024-10-20 09:54:12

Android -- Drawable与Bitmap的相关文章

Android Drawable、Bitmap、byte[]之间的转换

转自http://blog.csdn.net/june5253/article/details/7826597 1.Bitmap-->Drawable Bitmap drawable2Bitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof NinePatch

android Drawable转Bitmap| Bitmap转byte[]

// private byte[] Bitmap2Bytes(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } // public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap

Android开发实用技巧:Drawable和Bitmap之间不得不说的秘密

Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565.RGB888.作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低.我们理解为一种存储对象比较好. Drawable - 作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF.PNG.JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变.图形等. 一. Bitmap转Drawable Bitmap bm = xxx; //xxx根据你的情况获取 Bitmap

android 获取资源文件 R.drawable中的图片转换为drawable、bitmap(转载)

转载自:android 获取资源文件 R.drawable中的图片转换为drawable.bitmap 1. Resources resources = mContext.getResources();Drawable drawable = resources.getDrawable(R.drawable.a);imageview.setBackground(drawable); 2. Resources r = this.getContext().getResources();Inputstr

Android Drawable 转化成 Bitmap

/*Java代码 将Drawable转化为Bitmap */ Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height,drawable.getOpacity() != PixelFormat.O

Android -- Drawable &amp;&amp; Bitmap

Bitmap转Drawable Bitmap bm=xxx; BitmapDrawable bd=new BitmapDrawable(bm); 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可. Drawable转Bitmap Drawable d=xxx; BitmapDrawable bd = (BitmapDrawable) d; Bitmap bm = bd.getBitmap(); 最终bm就是我们需要的Bitmap对象了. 从资源中获取Bitmap

android 获取资源文件 r.drawable中的图片转换为drawable、bitmap

1. Resources resources = mContext.getResources(); Drawable drawable = resources.getDrawable(R.drawable.a); imageview.setBackground(drawable); 2. Resources r = this.getContext().getResources(); Inputstream is = r.openRawResource(R.drawable.my_backgrou

Android Drawable 那些不为人知的高效用法

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43752383,本文出自:[张鸿洋的博客] 1.概述 Drawable在我们平时的开发中,基本都会用到,而且给大家非常的有用.那么什么是Drawable呢?能够在canvas上绘制的一个玩意,而且相比于View,并不需要去考虑measure.layout,仅仅只要去考虑如何draw(canavs).当然了,对于Drawable传统的用法,大家肯定不陌生 ,今天主要给大家带来以下几

Android Drawable Mipmap Vector使用及Vector兼容

原文地址:http://blog.csdn.net/eclipsexys/article/details/51838119 http://blog.csdn.net/qq_15545283/article/details/51472458 一.谷歌在app中图标的适配的历史 在安卓的发展历程中,由于设备碎片化的原故,谷歌在app中图标的适配上做出一步又一步的改进,大体有这么几个阶段: 首先有了drawable-(m|h|xh|xxh|xxxh)dpi 自android studio后,又有了mi