阅读更多 增加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如何通过代码将res里的图片转换成drawable.
  5. Android通过图片名字获得ID
  6. android图片上传springMvc
  7. android中图片的拖拉和缩放
  8. Android图片放大修改代码

随机推荐

  1. android studio线性布局,相对布局,TestView
  2. Android Bitmap和Canvas学习笔记
  3. [android] 隐式意图的配置
  4. Android(安卓)Drawable系列——Animation
  5. android sdk2.0分辨率的bug
  6. Android AsyncTask的优缺点详解
  7. 【android】应用架构一一一一一Activity
  8. Android基础教程——横竖切换监听、禁止
  9. AndroidUI自动化测试工具-UIautomator
  10. Android Layout XML属性 及 控件属性