首页 技术 正文
技术 2022年11月20日
0 收藏 578 点赞 3,477 浏览 1667 个字

增加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图片可能会修饰一些图片的细节。

    1. /**
    2. * 因为目前我们只有一套资源文件,全都放在hdpi下面,这样如果是遇到高密度手机, 系统会按照
    3. * scale = (float) targetDensity / density 把图片放到几倍,这样会使得在高密度手机上经常会发生OOM。
    4. *
    5. * 这个方法用来解决在如果密度大于hdpi(240)的手机上,decode资源文件被放大scale,内容浪费的问题。
    6. * @param resources
    7. * @param id
    8. * @return
    9. */
    10. public static Bitmap decodeResource(Resources resources, int id) {
    11. int densityDpi = resources.getDisplayMetrics().densityDpi;
    12. Bitmap bitmap;
    13. TypedValue value = new TypedValue();
    14. resources.openRawResource(id, value);
    15. BitmapFactory.Options opts = new BitmapFactory.Options();
    16. opts.inPreferredConfig = Bitmap.Config.ALPHA_8;
    17. if (densityDpi > DisplayMetrics.DENSITY_HIGH) {
    18. opts.inTargetDensity = value.density;
    19. bitmap = BitmapFactory.decodeResource(resources, id, opts);
    20. }else{
    21. bitmap = BitmapFactory.decodeResource(resources, id);
    22. }
    23. return bitmap;
    24. }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,912
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,436
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,251
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,063
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,694
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,732