0

我正在尝试构建具有大约 100 个图像的 ADW 主题应用程序,并且我的活动有厨房来显示 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);
    }

}

我需要一个解决方案

4

1 回答 1

0

您将图像分配给每个 ImageView 两次,一次分配给 imageResource,第二次分配给它的背景。这是你的代码

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(...)。希望这会有所帮助。

于 2013-07-29T23:07:08.873 回答