Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我做大循环时,应用程序占用太多内存_Java_Android_Ram - Fatal编程技术网

Java 当我做大循环时,应用程序占用太多内存

Java 当我做大循环时,应用程序占用太多内存,java,android,ram,Java,Android,Ram,我有这个代码,我想做同样的事情,但花费更少的内存。有没有想法在我生成的300个ImageView中的每一个中设置一个图像加载程序或类似的东西来解决这个问题 DisplayMetrics lDisplayMetrics = getResources().getDisplayMetrics(); int widthPixels = lDisplayMetrics.widthPixels; for (int getDrawables = 0; getDrawables < 300; getDra

我有这个代码,我想做同样的事情,但花费更少的内存。有没有想法在我生成的300个
ImageView
中的每一个中设置一个图像加载程序或类似的东西来解决这个问题

DisplayMetrics lDisplayMetrics = getResources().getDisplayMetrics();
int widthPixels = lDisplayMetrics.widthPixels;
for (int getDrawables = 0; getDrawables < 300; getDrawables++)
{
    final ImageView icoView = new ImageView(ImageEditor.this);
    icoView.setImageResource(getResources().getIdentifier("icon_"+getDrawables, "drawable", getPackageName()));
    icoView.setLayoutParams(new LinearLayout.LayoutParams(widthPixels/7,widthPixels/7));
    gridlayout.addView(icoView);
    int idGen = View.generateViewId();
    icoView.setId(idGen);
    icoView.setTag("icon_"+getDrawables);
    gridlayout.setColumnCount(6);
}
DisplayMetrics lDisplayMetrics=getResources().getDisplayMetrics();
int-widthPixels=lDisplayMetrics.widthPixels;
对于(int-getDrawables=0;getDrawables<300;getDrawables++)
{
最终ImageView图标视图=新的ImageView(ImageEditor.this);
setImageResource(getResources().getIdentifier(“图标”+getDrawables,“drawable”,getPackageName());
设置布局参数(新的LinearLayout.LayoutParams(宽度像素/7,宽度像素/7));
gridlayout.addView(icoView);
int idGen=View.generateViewId();
icoView.setId(idGen);
setTag(“图标”+getDrawables);
gridlayout.setColumnCount(6);
}

不要在布局中添加太多图像视图。无论如何,我想它们不会一下子全部可见。使用a或a之类的工具为您管理视图的回收。

解决方案: 添加
新的绘图工具
,但分辨率较低,并将其用于我的标签选择器。作为一个回收者,我只减少了500MB的内存使用。
我看到的另一种方法是将绘图转换为位图,然后设置位图的大小。

你能给我发送一个链接或其他东西来获取相关参考信息吗?这给我带来了与ram相同的问题(我尝试GridView),但没有解决<代码>E/AndroidRuntime:致命异常:主进程:com.rogerrv.fillthem,PID:3723 java.lang.OutOfMemoryError:无法分配7225356字节的分配,其中包含3387472个可用字节和3MB,直到dalvik.system.VMRuntime.newNonMovableArray的OOM为止(本机方法)在android.graphics.BitmapFactory.NativeDecodeset(本机方法)…中,您是否为图像创建了适配器并通过
setAdapter()
将其绑定?是的,我使用适配器是因为可绘制的大小,您是否一次实例化所有图像视图?始终使用convertView并用新内容填充现有图像视图。