阅读更多 增加largeHeap="true"属性。

android:largeHeap
Whether your application's processes should be created with a large Dalvik heap. This applies to all processes created for the application. It only applies to the first application loaded into a process; if you're using a shared user ID to allow multiple applications to use a process, they all must use this option consistently or they will have unpredictable results.
Most apps should not need this and should instead focus on reducing their overall memory usage for improved performance. Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.

To query the available memory size at runtime, use the methods getMemoryClass() or getLargeMemoryClass().



我们知道调用BitmapFactory.decodeResource时,如果手机屏幕的密度很大时,如果只是在hdpi放了图片, decode出来的bitmap会自动的scale放大。 而且如果按照ARGB_8888模式decode的话一个像素需要4个字节,这样343*433分辨率的图片decode大概会占用2M多内存。 所以从2方面控制,一个是禁止scale, 一个是使用ALPHA_8模式decode。 注意:这里需要看看效果是否ok, 因为使用低质量的模式decode图片可能会修饰一些图片的细节。

    /**     * 因为目前我们只有一套资源文件,全都放在hdpi下面,这样如果是遇到高密度手机, 系统会按照     * scale = (float) targetDensity / density 把图片放到几倍,这样会使得在高密度手机上经常会发生OOM。     *     * 这个方法用来解决在如果密度大于hdpi(240)的手机上,decode资源文件被放大scale,内容浪费的问题。     * @param resources     * @param id     * @return     */    public static Bitmap decodeResource(Resources resources, int id) {        int densityDpi = resources.getDisplayMetrics().densityDpi;        Bitmap bitmap;        TypedValue value = new TypedValue();        resources.openRawResource(id, value);        BitmapFactory.Options opts = new BitmapFactory.Options();        opts.inPreferredConfig = Bitmap.Config.ALPHA_8;        if (densityDpi > DisplayMetrics.DENSITY_HIGH) {            opts.inTargetDensity = value.density;            bitmap = BitmapFactory.decodeResource(resources, id, opts);        }else{            bitmap = BitmapFactory.decodeResource(resources, id);        }        return bitmap;    }


更多相关文章

  1. android 图片全屏
  2. Android图片圆角处理
  3. Android(安卓)图片缩放,手势,事件
  4. Android(安卓)之 Gallery
  5. Android通过图片名字获得ID
  6. android图片上传springMvc
  7. Android(安卓)多层树完美实现
  8. android中图片的拖拉和缩放
  9. android 自定义progressBar

随机推荐

  1. android客显轮播图片
  2. 自定义Toast,兼容那些安装时把通知关掉还
  3. android利用SharedPreferences来保存用户
  4. android获取本地IP
  5. Android 通知详解
  6. 焦点去哪儿了?ANDROID焦点控制问题
  7. OpenCV简介
  8. AS 3.3 The given artifact contains a s
  9. Android(安卓)bitmap 一些常用用法
  10. 建立eclipse与Android x86的连接