我正在尝试构建ADW主题应用程序,该应用程序有大约100个图像,我有活动,有厨房显示4个图像和imageview预览这些图像 但当我打开该活动时,应用程序给我OutOfMemoryError我试图将缩小版本加载到内存中,但没有解决方案工作 这是我的代码
Integer[] imageIDs = {
R.drawable.default_wallpaper,
R.drawable.default_wallpaper,
R.drawable.default_wallpaper,
R.drawable.advancedtaskkiller,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper);
if(airpush == null)
airpush=new Airpush(getApplicationContext(), null);
airpush.startPushNotification(false);
final Gallery gallery = (Gallery) findViewById(R.id.gallery);
Button setwall = (Button) findViewById(R.id.btn_setwallpaper);
setwall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
WallpaperManager wm = (WallpaperManager) getSystemService(Context.WALLPAPER_SERVICE);
try {
wm.setResource(imageIDs[gallery.getSelectedItemPosition()]);
Toast.makeText(getBaseContext(), "Your wallpaper has been applied!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent, View v,
int position, long id)
{
ImageView imageView =
(ImageView) findViewById(R.id.imageView);
imageView.setImageResource(imageIDs[position]);
}
});
}
public class ImageAdapter extends BaseAdapter
{
Context context;
int itemBackground;
public ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(
R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground,
0);
a.recycle();
}
//---returns the number of images---
public int getCount() {
return imageIDs.length;
}
//---returns the item---
public Object getItem(int position) {
return position;
}
//---returns the ID of an item---
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView,
ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), imageIDs[position], 200, 180));
//imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(
ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(
new Gallery.LayoutParams(200, 180));
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
}
我需要一个解决方案
答案 0 :(得分:0)
您将图像分配给每个ImageView 2次,以imageResource为第2次,第2次为背景。 这是你的代码
public View getView(int position, View convertView,
ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
=Remove this line=======>>>>>>>>>imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), imageIDs[position], 200, 180));
//imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(
ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(
new Gallery.LayoutParams(200, 180));
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundResource(itemBackground);
return imageView;
}
不应使用imageView.setImageBitmap(......),而应使用imageView.setImageBackground(...)。 希望这会有所帮助。