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 です。