0

ImageView がある独自の SlideShow アクティビティを作成しています。ユーザーが次または前をクリックするたびに、適切な画像を入力するだけです。

ただし、これらの画像は大きく、いくつかの画像をナビゲートするとすぐに、おそらく私のせいでメモリ リークが発生したために、OutofMemory が発生します。

問題は、新しい画像が呼び出されるたびに、前のビットマップを null にしてガベージコレクトすることですが、役に立ちません。

ここで何か不足していますか?

私は自分の活動の非常に単純なコードを添付しています:

public class SlideShowActivity extends Activity {
    private static final String LOG_TAG = SlideShowActivity.class.getSimpleName();

    private Bitmap currentBitmap;
    private Bitmap mDefaultBitmap;
    private ImageView imageView;
    private ImageButton buttonPrev;
    private ImageButton buttonNext;
    private int currentImageIndex;

    private String imguri[] = { "ruta1/imagen1.jpg", "ruta1/imagen2.jpg", "ruta1/imagen3.jpg", "ruta1/imagen4.jpg", "ruta1/imagen5.jpg", "ruta1/imagen6.jpg" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.slideshow);

        mDefaultBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.noimage);
        imageView = (ImageView) findViewById(R.id.imageViewCurrentImage);
        buttonPrev = (ImageButton) findViewById(R.id.imgbuttonprev);
        buttonNext = (ImageButton) findViewById(R.id.imgbuttonnext);

        currentImageIndex = 0;
        setCurrentImage(currentImageIndex);
        buttonPrev.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                currentImageIndex--;
                if(currentImageIndex < 0)
                    currentImageIndex = imguri.length-1;

                setCurrentImage(currentImageIndex);
            }
        });

        buttonNext.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                currentImageIndex++;
                if(currentImageIndex == imguri.length)
                    currentImageIndex = 0;
                setCurrentImage(currentImageIndex);
            }
        });

    }

    private void setCurrentImage(int idx) {

        currentBitmap = null;
        imageView.setImageBitmap(null);
        try {
            currentBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeStream(getAssets().open(imguri[idx])),800, 480, true);
            imageView.setImageBitmap(currentBitmap);
        } catch (IOException e) {
            Log.d(LOG_TAG, "No se pudo abrir el recurso" + imguri[0]);
            imageView.setImageBitmap(mDefaultBitmap);
        }
    }

}
4

3 に答える 3

1

ビットマップが不要になった場合は、bitmap.recycle() を呼び出すことをお勧めします。これにより、システムがネイティブ メモリをより積極的にリサイクルできるようになります。

setCurrentImage(int idx) を次のように変更できます

private void setCurrentImage(int idx) {
    imageView.setImageBitmap(null);
    currentBitmap.recycle();
    currentBitmap = null;
    try {
        Bitmap tempBitmap = BitmapFactory.decodeStream(getAssets().open(imguri[idx]));
        currentBitmap = Bitmap.createScaledBitmap(tempBitmap, 800, 480, true);
        tempBitmap.recycle();
        imageView.setImageBitmap(currentBitmap);
    } catch (IOException e) {
        Log.d(LOG_TAG, "No se pudo abrir el recurso" + imguri[0]);
        imageView.setImageBitmap(mDefaultBitmap);
    }
}
于 2012-12-28T16:57:26.140 に答える
0

このようにImageViewのビットマップを何度も変更すると、どのような効果が得られるのかわかりません。ただし、サポートライブラリに付属しているViewPagerを使用する必要があると思います。このようにすると、次のビューと前のビューが自動的に読み込まれ、不要になったときに削除されるため、実装が高速になります。また、画像をスライドさせることができるので、ユーザーにとってはさらに良いでしょう。

さらに改善するために、ViewPagerの機能を拡張するこのライブラリを使用できます。

http://viewpagerindicator.com/

于 2012-12-28T16:53:00.850 に答える
0

この目的には ListView を使用します。

リンクを使用してください: http://www.vogella.com/articles/AndroidListView/article.html

于 2012-12-28T16:51:09.807 に答える