我正在尝试制作一个片段,该片段将使用 TouchImageView(https://github.com/MikeOrtiz/TouchImageView)显示可缩放图像
该片段还有一个用于更改图像的微调器。问题是第一个图像加载正常,但是当我使用滚动条更改图像时,我得到一个 OutOfMemoryError 并且程序崩溃。这是我的代码
public class mapFragment extends SherlockFragment {
String[] Levels = { "Ground Floor", "First Floor",
"Second Floor", "Third Floor"
};
Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
View v = inflater.inflate(R.layout.maps_layout, group, false);
final TouchImageView img = (TouchImageView) v.findViewById(R.id.touchimage1);
final Bitmap snoop = BitmapFactory.decodeResource(getResources(), R.drawable.groundfloor);
img.setImageBitmap(snoop);
final Spinner s = (Spinner) v.findViewById(
R.id.spinnerlevels);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this
.getActivity().getBaseContext(),
android.R.layout.simple_spinner_item, Levels);
s.setAdapter(adapter);
s.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
int item = s.getSelectedItemPosition();
if(item ==0){
snoop.recycle();
Bitmap snoop = BitmapFactory.decodeResource(getResources(), R.drawable.groundfloor);
img.setImageBitmap(snoop);
}
if(item ==1){
snoop.recycle();
Bitmap snoop = BitmapFactory.decodeResource(getResources(), R.drawable.firstfloor);
img.setImageBitmap(snoop);
}
if(item ==2){
snoop.recycle();
Bitmap snoop = BitmapFactory.decodeResource(getResources(), R.drawable.secondfloor);
img.setImageBitmap(snoop);
}
if(item ==3){
snoop.recycle();
Bitmap snoop = BitmapFactory.decodeResource(getResources(), R.drawable.thirdfloor);
img.setImageBitmap(snoop);
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});
img.setMaxZoom(8f);
return (v);
}
}
“recylce()”不应该删除第一个图像,让位于内存中的新图像吗?以 MB 为单位的图像大小为 1.4、1.5、1.5、1.3