图像占用过多内存 [英] Images taking too much memory

查看:88
本文介绍了图像占用过多内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常简单地编写了代码,将我的图像全部压缩,因此没有人超过50kb.当我启动我的应用程序时,将加载1张图像,并且内存使用量约为42mb,但是当我向下滚动到其他图像时,每隔一张图像将导致内存使用量增加至250mb.如何解决?这是我的代码,请我是初学者,所以请尝试简单地进行解释.

Hi I have pretty simply code my images are all compressed so no one has more than 50kb. When I start my app 1 image is loaded and memory usage is around 42mb but when I scroll down to another images every other image cause memory usage to increase up to 250mb. How to fix it? Here is my code and please I am beginner so try to explain it simple.

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


public class CustomSwip extends PagerAdapter {
private int[] imageResources = {R.drawable.j1, R.drawable.jg2,R.drawable.jg3, R.drawable.jg4, R.drawable.jg5, R.drawable.jg6, R.drawable.jg7, R.drawable.jg8, R.drawable.jg9, R.drawable.jg10, R.drawable.jg11, R.drawable.jg12, R.drawable.jg13, R.drawable.jg14, R.drawable.jg15, R.drawable.jg16};
private Context ctx;
private LayoutInflater layoutInflater;

public CustomSwip(Context c) {
    ctx = c;
}

@Override
public int getCount() {

    return imageResources.length;
}


@Override
public Object instantiateItem(ViewGroup container, int position) {
    layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = layoutInflater.inflate(R.layout.activity_custom_swip, container, false);
    ImageView imageView = (ImageView) itemView.findViewById(R.id.swip_image_view);
    imageView.setImageResource(imageResources[position]);
    container.addView(itemView);
    return itemView;
}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {

}


@Override
public boolean isViewFromObject(View view, Object object) {

    return (view == object);
}

}

推荐答案

通用图像加载器 nostra13 编写的库,文档齐全广泛使用,并以配置设置而闻名,它提供了图片加载功能.在其中,有一个选项可以将图像加载到 RGB_565 中,这可以将您的内存消耗减少大约一半(实际上已经看到了结果),甚至更多.

Universal Image Loader is a library by nostra13 which is pretty well documented and widely used and is known for the configuration setting it provides for loading of images. In it, there is an option to load the images in RGB_565 which can decrease your memory consumption by about half (have seen the results practically) and many more.

在显示选项中使用.bitmapConfig(Bitmap.Config.RGB_565). RGB_565中的位图消耗的内存比ARGB_8888中的少2倍.

Use .bitmapConfig(Bitmap.Config.RGB_565) in display options. Bitmaps in RGB_565 consume 2 times less memory than in ARGB_8888.

并确实查看一次 WeakReferences .它可以帮助调用Garbage Collector获得内存密集型图像.

And do look into WeakReferences once. It could help in calling of Garbage Collector for memory-intensive images.

这篇关于图像占用过多内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆