`
dengyin2000
  • 浏览: 1208030 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

android避免decodeResource图片时占用太大的内存。

阅读更多
增加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;
    }


分享到:
评论

相关推荐

    GLRippleView-Android 水波涟漪图片特效 绝对震撼.zip

    国外大牛就是厉害。 下面贴出使用方法: 第一步:添加gradledependencies {  compile 'com.github.r21nomi:glrippleview:1.0.0' }第二步:xml添加布局文件  android:layout_width="match_parent"  android...

    Android 中对于图片的内存优化方法

    尽量不要使用 setImageBitmap、setImageResource、 BitmapFactory.decodeResource 来设置一张大图,因为这些方法在完成 decode 后,最终都是通过 Java 层的 createBitmap 来完成的,需要消耗更多内存。因此,改用先...

    android内存优化之图片优化

    尽量不要使用setImageBitmap、setImageResource、BitmapFactory.decodeResource来设置一张大图,因为这些方法在完成decode后,最终都是通过java层的createBitmap来完成的,需要消耗更多内存。因此,改用先通过...

    android Bitmap用法总结

    //将图片设为原来宽高的1/2,防止内存溢出 Bitmap bm = BitmapFactory.decodeFile("",option);//文件流 URL url = new URL(""); InputStream is = url.openStream(); Bitmap bm = BitmapFactory.decodeStream(is); ...

    Android图像数字识别

    Android可以识别和扫描二维码,但是识别字符串呢? google提供了以下解决方案用的是原来HP的相关资料。 可以吧,这个迁移到Android上。 工程导入成功是可以正常运行的,我是专门换了个电脑重新验证了下。 如果有不能...

    ViewPager或ImgeView加载图片出现内存溢出(OOM)

    使用setImageResource或BitmapFactory.decodeResource来设置一张大图,因为这些函数在完成decode后,最终都是通过java层的createBitmap来完成的,需要消耗更多内存。解决方案:改用先通过BitmapFactory.decodeStream...

    opencv_android开发库

    新建android项目 在AndroidManifest.xml中增加写sd卡的权限: <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WRITE_...

    Android Bitmap的加载与缓存

    通常情况下图片的体积都比较大,单个应用允许使用的内存又是有限的,所以我们需要采取一些手段减少内存占用并提高加载速度。 1、图片加载 SDK提供了BitmapFactory类供我们加载图片,常用的方法有这么几个: ...

    Android实现图片叠加效果的两种方法

    本文实例讲述了Android实现图片叠加效果的两种方法。分享给大家供大家参考,具体如下: 效果图: 第一种: 第二种: 第一种是通过canvas画出来的效果: public void first(View v) { // 防止出现Immutable bitmap...

    Android编程实现图片平铺的方法分析

    本文实例讲述了Android编程实现图片平铺的方法。分享给大家供大家参考,具体如下: 1)第一种利用系统提供的api实现 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic); //bitmap = ...

    Android代码-NextQRCode

    基于ZXing Android实现的二维码扫描支持库。 包括生成二维码图片、解析二维码图片和相机扫描即时解码三部分功能。 与原ZXingMini项目对比 NextQRCode做了重大架构修改,原ZXingMini项目与当前NextQRCode不兼容 依赖 ...

    Android代码-一个酷炫的分享控件

    What's FlipShare ? It's a cool way to show share widget. Demo Usage step 1. Confirm your parentView to ... .addItem(new ShareItem("Twitter", Color.WHITE, 0xff4999F0, BitmapFactory.decodeResource(ge

    CircularImageView-Xamarin-:圆形 ImageView 和 Drawable、Xamarin Android 库和示例项目

    圆形 ImageView 和 Drawable、Xamarin Android 库和示例项目。 用法 Bitmap bitmap = BitmapFactory.DecodeResource (Resources, Resource.Drawable.sample); CircularDrawable d = new CircularDrawable (bitmap,...

    Android之高效加载大图的方法示例

    因为大图的原因,我们会在Crash报告中看到OOM(内存不足).Android的内存有限,这一点我们应该心里有数。 stackoverflow上有许多相关问题的回答,当你碰到oom时,可以直接跳过本文,粘贴复制答案即可。但是对于其他人...

    Android-eBook翻书效果源码

    // 当mBezierStart1.x 或者mBezierStart1.x > 480时 // 如果继续翻页,会出现BUG故在此限制 if (mBezierStart1.x || mBezierStart1.x > 480) { if (mBezierStart1.x ) mBezierStart1.x = mWidth - ...

    avatar-android-可生成圆形、方形、及方形的组合头像.zip

    Bitmap avatar = BitmapFactory.decodeResource(getResources(), R.drawable.avatar, options); //圆形的 Drawable roundedAvatarDrawable = avatarDrawableFactory.getRoundedAvatarDrawable(avatar);//...

    Android 图片处理避免出现oom的方法详解

    res资源图片压缩 decodeResource public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check ...

    Android实现图片反转、翻转、旋转、放大和缩小

    android 实现图片的翻转 ********************************************************************** Resources res = this.getContext().getResources(); img = BitmapFactory.decodeResource(res, R.drawable.aa...

    android webp编解码详解

    key words:android decode webp sample 当我敲下键盘的时候有种深深的耻辱感,看到android 4.0支持webp格式的图像,于是我狠命的找提供了什么样的api,nnd,硬是没找到,后来抱着试试的心态,用BitmapFactory来读...

Global site tag (gtag.js) - Google Analytics