我的应用程序带有SQLite
数据库,其中保存了drawables(字符串)的名称。多个drawable保存在res/drawable
目录中,其scalling大小为drawable-hdpi, drawable-mdpi, drawable-xhdpi
等。此设备使用mdpi。它们的大小约为80 kB。每次我调用update()
方法时,我都会为同一个SetImageResource
调用ImageView
方法,而我只更改image file
并且需要大约0.8 MB的内存增加。怎么避免这个?
private ImageView mImageName;
private void update(){
mImageName.setImageResource(getResources().getIdentifier("@drawable/" + mQuestionLibrary.getImage(mQuestionNumber), null, getPackageName()));
}
答案 0 :(得分:1)
您必须使用图像资源导入器为不同的屏幕设置多种尺寸。
然后你需要通过以下代码设置你的图像:
mImageName.setImageResource(R.drawable.resource_name);
您也可以将mQuestionLibrary.getImage(mQuestionNumber)
设想为返回资源ID而不是资源名称,然后使用:
mImageName.setImageResource(mQuestionLibrary.getImage(mQuestionNumber));
答案 1 :(得分:1)
我找到了解决方案。问题是我已经在ImageView
布局属性中设置了adjustViewBounds
和不必要的src
。这些属性占用了大量内存。
<ImageView
android:id="@+id/iV_pytanie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/tab1"
/>
我删除了它们,每个图像的内存减少了大约0.5 MB。