简体   繁体   English

图片占用太多内存

[英]Images taking too much memory

Hi I have pretty simply code my images are all compressed so no one has more than 50kb.嗨,我的代码非常简单,我的图像都被压缩了,所以没有人超过 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.当我启动我的应用程序时,加载了 1 个图像并且内存使用量约为 42mb,但是当我向下滚动到另一个图像时,每隔一个图像会导致内存使用量增加到 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);
}

}

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. 通用图像加载器nostra13 的一个库,它有很好的文档记录广泛使用,并以其为加载图像提供的配置设置而闻名。 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.在其中,有一个选项可以在RGB_565 中加载图像,这可以将您的内存消耗减少大约一半(实际上已经看到了结果)等等。

Use .bitmapConfig(Bitmap.Config.RGB_565) in display options.在显示选项中使用 .bitmapConfig(Bitmap.Config.RGB_565)。 Bitmaps in RGB_565 consume 2 times less memory than in ARGB_8888. RGB_565 中的位图消耗的内存比 ARGB_8888 中的少 2 倍。

And do look into WeakReferences once.并查看WeakReferences一次。 It could help in calling of Garbage Collector for memory-intensive images.它可以帮助为内存密集型图像调用垃圾收集器。

According to the docs for setImageResource,根据 setImageResource 的文档,

This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup.这会在 UI 线程上读取和解码位图,这可能会导致延迟打嗝。 If that's a concern, consider using setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.如果这是一个问题,请考虑使用 setImageDrawable(android.graphics.drawable.Drawable) 或 setImageBitmap(android.graphics.Bitmap) 和 BitmapFactory 。

Or simply use an image loading library to lazy load images或者简单地使用图像加载库来延迟加载图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM