在imageswitcher上滑动时的outofmemory

时间:2011-05-18 19:04:01

标签: android

有没有办法禁用图像切换器上的幻灯片滚动 我的应用有超过400张图片 如果我点击imageswitcher一切正常 但是当我在一段时间后滑动滚动它会因为内存错误而崩溃

无论如何都要禁用该功能

package com.quartergames.owen.MyRoadTrip;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ViewSwitcher.ViewFactory;

public class ImageSwitcherView extends Activity implements ViewFactory {
    Integer pics[] = { R.drawable.image000, R.drawable.image001,
            R.drawable.image002, R.drawable.image003, R.drawable.image004,
            R.drawable.image005, R.drawable.image006, R.drawable.image007,
            R.drawable.image008, R.drawable.image009, R.drawable.image010,
            R.drawable.image011, R.drawable.image012, R.drawable.image013,
            R.drawable.image014, R.drawable.image015, R.drawable.image016,
            R.drawable.image017, R.drawable.image018, R.drawable.image019,
            R.drawable.image020, R.drawable.image021, R.drawable.image022,
            R.drawable.image023, R.drawable.image024, R.drawable.image025,
            R.drawable.image026, R.drawable.image027, R.drawable.image028,
            R.drawable.image029, R.drawable.image030, R.drawable.image031,
            R.drawable.image032, R.drawable.image033, R.drawable.image034,
            R.drawable.image035, R.drawable.image036, R.drawable.image037,
            R.drawable.image038, R.drawable.image039, R.drawable.image040,
            R.drawable.image041, R.drawable.image042, R.drawable.image043,
            R.drawable.image044, R.drawable.image045, R.drawable.image046,
            R.drawable.image047, R.drawable.image048, R.drawable.image049,
            R.drawable.image050, R.drawable.image051, R.drawable.image052,
            R.drawable.image053, R.drawable.image054, R.drawable.image055,
            R.drawable.image056, R.drawable.image057, R.drawable.image058,
            R.drawable.image059, R.drawable.image060, R.drawable.image061,
            R.drawable.image062, R.drawable.image063, R.drawable.image064,
            R.drawable.image065, R.drawable.image066, R.drawable.image067,
            R.drawable.image068, R.drawable.image069, R.drawable.image070,
            R.drawable.image071, R.drawable.image072, R.drawable.image073,
            R.drawable.image074, R.drawable.image075, R.drawable.image076,
            R.drawable.image077, R.drawable.image078, R.drawable.image079,
            R.drawable.image080, R.drawable.image081, R.drawable.image082,
            R.drawable.image083, R.drawable.image084, R.drawable.image085,
            R.drawable.image086, R.drawable.image087, R.drawable.image088,
            R.drawable.image089, R.drawable.image090, R.drawable.image091,
            R.drawable.image092, R.drawable.image093, R.drawable.image094,
            R.drawable.image095, R.drawable.image096, R.drawable.image097,
            R.drawable.image098, R.drawable.image099, R.drawable.image100,
            R.drawable.image101, R.drawable.image102, R.drawable.image103,
            R.drawable.image104, R.drawable.image105, R.drawable.image106,
            R.drawable.image107, R.drawable.image108, R.drawable.image109,
            R.drawable.image110, R.drawable.image111, R.drawable.image112,
            R.drawable.image113, R.drawable.image114, R.drawable.image115,
            R.drawable.image116, R.drawable.image117, R.drawable.image118,
            R.drawable.image119, R.drawable.image120, R.drawable.image121,
            R.drawable.image122, R.drawable.image123, R.drawable.image124,
            R.drawable.image125, R.drawable.image126, R.drawable.image127,
            R.drawable.image128, R.drawable.image129, R.drawable.image130,
            R.drawable.image131, R.drawable.image132, R.drawable.image133,
            R.drawable.image134, R.drawable.image135, R.drawable.image136,
            R.drawable.image137, R.drawable.image138, R.drawable.image139,
            R.drawable.image140, R.drawable.image141, R.drawable.image142,
            R.drawable.image143, R.drawable.image144, R.drawable.image145,
            R.drawable.image146, R.drawable.image147, R.drawable.image148,
            R.drawable.image149, R.drawable.image150, R.drawable.image151,
            R.drawable.image152, R.drawable.image153, R.drawable.image154,
            R.drawable.image155, R.drawable.image156, R.drawable.image157,
            R.drawable.image158, R.drawable.image159, R.drawable.image160,
            R.drawable.image161, R.drawable.image162, R.drawable.image163,
            R.drawable.image164, R.drawable.image165, R.drawable.image166,
            R.drawable.image167, R.drawable.image168, R.drawable.image169,
            R.drawable.image170, R.drawable.image171, R.drawable.image172,
            R.drawable.image173, R.drawable.image174, R.drawable.image175,
            R.drawable.image176, R.drawable.image177, R.drawable.image178,
            R.drawable.image179, R.drawable.image180, R.drawable.image181,
            R.drawable.image182, R.drawable.image183, R.drawable.image184,
            R.drawable.image185, R.drawable.image186, R.drawable.image187,
            R.drawable.image188, R.drawable.image189, R.drawable.image190,
            R.drawable.image191, R.drawable.image192, R.drawable.image193,
            R.drawable.image194, R.drawable.image195, R.drawable.image196,
            R.drawable.image197, R.drawable.image198, R.drawable.image199,
            R.drawable.image200, R.drawable.image221, R.drawable.image222,
            R.drawable.image223, R.drawable.image224, R.drawable.image225,
            R.drawable.image226, R.drawable.image227, R.drawable.image228,
            R.drawable.image229, R.drawable.image210, R.drawable.image211,
            R.drawable.image212, R.drawable.image213, R.drawable.image214,
            R.drawable.image215, R.drawable.image216, R.drawable.image217,
            R.drawable.image218, R.drawable.image219, R.drawable.image220,
            R.drawable.image221, R.drawable.image222, R.drawable.image223,
            R.drawable.image224, R.drawable.image225, R.drawable.image226,
            R.drawable.image227, R.drawable.image228, R.drawable.image229,
            R.drawable.image230, R.drawable.image231, R.drawable.image232,
            R.drawable.image233, R.drawable.image234, R.drawable.image235,
            R.drawable.image236, R.drawable.image237, R.drawable.image238,
            R.drawable.image239, R.drawable.image240, R.drawable.image241,
            R.drawable.image242, R.drawable.image243, R.drawable.image244,
            R.drawable.image245, R.drawable.image246, R.drawable.image247,
            R.drawable.image248, R.drawable.image249, R.drawable.image250,
            R.drawable.image251, R.drawable.image252, R.drawable.image253,
            R.drawable.image254, R.drawable.image255, R.drawable.image256,
            R.drawable.image257, R.drawable.image258, R.drawable.image259,
            R.drawable.image260, R.drawable.image261, R.drawable.image262,
            R.drawable.image263, R.drawable.image264, R.drawable.image265,
            R.drawable.image266, R.drawable.image267, R.drawable.image268,
            R.drawable.image269, R.drawable.image270, R.drawable.image271,
            R.drawable.image272, R.drawable.image273, R.drawable.image274,
            R.drawable.image275, R.drawable.image276, R.drawable.image277,
            R.drawable.image278, R.drawable.image279, R.drawable.image280,
            R.drawable.image281, R.drawable.image282, R.drawable.image283,
            R.drawable.image284, R.drawable.image285, R.drawable.image286,
            R.drawable.image287, R.drawable.image288, R.drawable.image289,
            R.drawable.image290, R.drawable.image291, R.drawable.image292,
            R.drawable.image293, R.drawable.image294, R.drawable.image295,
            R.drawable.image296, R.drawable.image297, R.drawable.image298,
            R.drawable.image299, R.drawable.image300, R.drawable.image301,
            R.drawable.image302, R.drawable.image303, R.drawable.image304,
            R.drawable.image305, R.drawable.image306, R.drawable.image307,
            R.drawable.image308, R.drawable.image309, R.drawable.image310,
            R.drawable.image311, R.drawable.image312, R.drawable.image313,
            R.drawable.image314, R.drawable.image315, R.drawable.image316,
            R.drawable.image317, R.drawable.image318, R.drawable.image319,
            R.drawable.image320, R.drawable.image321, R.drawable.image322,
            R.drawable.image323, R.drawable.image324, R.drawable.image325,
            R.drawable.image326, R.drawable.image327, R.drawable.image328,
            R.drawable.image329, R.drawable.image330, R.drawable.image331,
            R.drawable.image332, R.drawable.image333, R.drawable.image334,
            R.drawable.image335, R.drawable.image336, R.drawable.image337,
            R.drawable.image338, R.drawable.image339, R.drawable.image340,
            R.drawable.image341, R.drawable.image342, R.drawable.image343,
            R.drawable.image344, R.drawable.image345, R.drawable.image346,
            R.drawable.image347, R.drawable.image348, R.drawable.image349,
            R.drawable.image350, R.drawable.image351, R.drawable.image352,
            R.drawable.image353, R.drawable.image354, R.drawable.image355,
            R.drawable.image356, R.drawable.image357, R.drawable.image358,
            R.drawable.image359, R.drawable.image360, R.drawable.image361,
            R.drawable.image362, R.drawable.image363, R.drawable.image364,
            R.drawable.image365, R.drawable.image366, R.drawable.image367,
            R.drawable.image368, R.drawable.image369, R.drawable.image370,
            R.drawable.image371, R.drawable.image372, R.drawable.image373,
            R.drawable.image374, R.drawable.image375, R.drawable.image376,
            R.drawable.image377, R.drawable.image378, R.drawable.image379,
            R.drawable.image380, R.drawable.image381, R.drawable.image382,
            R.drawable.image383, R.drawable.image384, R.drawable.image385,
            R.drawable.image386, R.drawable.image387, R.drawable.image388,
            R.drawable.image389, R.drawable.image390, R.drawable.image391,
            R.drawable.image392, R.drawable.image393, R.drawable.image394,
            R.drawable.image395, R.drawable.image396, R.drawable.image397,
            R.drawable.image398, R.drawable.image399, R.drawable.image400,
            R.drawable.image401, R.drawable.image402, R.drawable.image403,
            R.drawable.image404, R.drawable.image405 };

    ImageSwitcher iSwitcher;
    private int currentlySelectedPic = 0;
    private int currentlySelectedIndex = 0;
    SharedPreferences preferences;
    Gallery gallery;
    SqlLiteHelper slHelper;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.galleryview);
        slHelper = new SqlLiteHelper(getApplicationContext());
        Bundle extras = getIntent().getExtras();
        if (currentlySelectedIndex > 0){
        }else{

        currentlySelectedIndex = extras.getInt("bookmark");
        Log.e("Test",String.valueOf(currentlySelectedIndex));

        }


        iSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher01);
        iSwitcher.setFactory(this);
        iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));

        gallery = (Gallery) findViewById(R.id.Gallery01);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setSelection(currentlySelectedIndex);
        gallery.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                currentlySelectedPic = pics[arg2];
                currentlySelectedIndex = arg2;
                iSwitcher.setImageResource(pics[arg2]);
            }
        });


    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                || keyCode == KeyEvent.KEYCODE_HOME) {
            preferences = PreferenceManager.getDefaultSharedPreferences(this);

            SharedPreferences.Editor editor = preferences.edit();
            editor.putInt("bookmark", gallery.getSelectedItemPosition());
            editor.commit();
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    public class ImageAdapter extends BaseAdapter {

        private Context ctx;

        public ImageAdapter(Context c) {
            ctx = c;
        }

        @Override
        public int getCount() {

            return pics.length;
        }

        @Override
        public Object getItem(int arg0) {

            return arg0;
        }

        @Override
        public long getItemId(int arg0) {

            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            ImageView iView = new ImageView(ctx);
            iView.setImageResource(pics[arg0]);

            iView.setScaleType(ImageView.ScaleType.FIT_XY);
            iView.setLayoutParams(new Gallery.LayoutParams(150, 150));
            return iView;
        }

    }

    @Override
    public View makeView() {
        ImageView iView = new ImageView(this);
        iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        iView.setLayoutParams(new ImageSwitcher.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        iView.setBackgroundColor(0xFF000000);

        iView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent myIntent = new Intent(ImageSwitcherView.this,
                        imageView.class);
                currentlySelectedPic = pics[currentlySelectedIndex];
                myIntent.putExtra("resID", currentlySelectedPic);
                myIntent.putExtra("index", currentlySelectedIndex);
                startActivityForResult(myIntent, 1);
            }
        });

        return iView;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        currentlySelectedIndex = data.getIntExtra("bookmark", 0);
        gallery.setSelection(currentlySelectedIndex);
        iSwitcher.setImageResource(pics[currentlySelectedIndex]);
    }
}

2 个答案:

答案 0 :(得分:2)

  1. 我强烈建议检查getView中的View参数(代码中的arg1)并使用它来代替创建新的ImageView以正确回收旧的ImageView。如果可能的话,您甚至可以尝试dispose上一个位图。

  2. 这是在调试器中吗?已知使用调试器会在处理位图时泄漏内存(在某些情况下)。

  3. “停用此功能”?什么功能?

  4. 你也可以考虑使用某种LRU缓存来制作位图并处理最旧的缓存,尽管这可能比你必须参与的更多。

  5. 旁注:你是在创建一个Integer数组而不是一个int数组的原因吗?

答案 1 :(得分:0)

好的,所以我简化了应用并删除了imageswitcher。一旦这样做,我注意到它在同一个位置弄乱了。所以我看了那个位置的图像。它们最大的图像(600kb,400kb)也是这些图像具有非常高的尺寸(1600,1400)。将这些图像更改为800x600可缩小尺寸并修复问题。我认为它的尺寸大于尺寸。因为其中一个有问题的图像大约是300kb(这与其他没有问题的图像类似)。总之,修复图像上的尺寸可以解决问题。