1

アプリにスライドショーを実装する必要があります。これには、 を使用しHandlerました。をクリックplay buttonするとスライドショーが始まり、再生ボタンが に変わりますpause button。しかし、もう一度クリックしてスライド ショーを一時停止しても、一時停止しません。それでも、Runnable の run()は実行を続けます。これが私のコードです:

public class GalleryImageFullscreen extends TIEBaseActivity implements
    OnClickListener {
PrefStore lang_PrefStore;
Resources res;
Configuration config;
Locale mLocale = null;
String language;
boolean isLocaleChanged = false;
ViewPager imgViewPager;
ArrayList<String> imgUrlList;
private ImageLoader imageLoader;
private DisplayImageOptions options;
int position;
ImageButton btnPrev, btnPlay, btnNext;
public static final String TAG = "TIEGalleryFS";
boolean isPlaying;
Handler mHandler;
SwitchImage swImageRunnable;

protected void onCreate(Bundle savedInstanceState) {
    super.createDefaultView(savedInstanceState);

    _headerBar.view.setVisibility(View.VISIBLE);
    _footerBar.view.setVisibility(View.VISIBLE);

    imgUrlList = getIntent().getStringArrayListExtra(
            GalleryActivity.IMAGE_URL_LIST);
    Log.v(TAG, "IMAGE LIST SIZE = " + imgUrlList.size());

    position = getIntent().getIntExtra("POSITION", 0);
    Log.v(TAG, "IMAGE POSITION = " + position);

    mHandler = new Handler();
    swImageRunnable = new SwitchImage();
    super.setHeaderTitle(getResources().getString(R.string.gallery));

    res = getResources();

    lang_PrefStore = new PrefStore();
    language = lang_PrefStore.getLanguageChoice(
            GalleryImageFullscreen.this, FooterBar.LANG_SELECT);
    Log.v("GalleryImageFullscreen language choice", language);

    config = getBaseContext().getResources().getConfiguration();
    if (!language.equals("")
            && language.equals(config.locale.getLanguage())) {
        mLocale = new Locale(language);
        Locale.setDefault(mLocale);
        config.locale = mLocale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());
        Log.v("GalleryImageFullscreen onCreate languageset", "in if loop");
    }

    init();

    imageLoader = ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration
            .createDefault(GalleryImageFullscreen.this));

    options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.image_for_empty_url)
            .showImageForEmptyUri(R.drawable.image_for_empty_url)
            .cacheInMemory().cacheOnDisc()
            .bitmapConfig(Bitmap.Config.RGB_565).build();
}// onCreate

private void init() {
    loadFormFromResource(R.layout.gallery_image_fullscreen);
    imgViewPager = (ViewPager) findViewById(R.id.imgpager);
    btnPrev = (ImageButton) findViewById(R.id.btnPrev);
    btnPlay = (ImageButton) findViewById(R.id.btnPlay);
    btnNext = (ImageButton) findViewById(R.id.btnNext);

    if ((imgUrlList != null) && (imgUrlList.size() > 0)) {
        imgViewPager.setAdapter(new ImagePagerAdapter(imgUrlList));
        imgViewPager.setCurrentItem(position);
    }

    btnPrev.setOnClickListener(this);
    btnPlay.setOnClickListener(this);
    btnNext.setOnClickListener(this);
}// init

public void onClick(View v) {
    if (v.equals(btnPrev)) {
        if (position != 0) {
            position--;
            imgViewPager.setCurrentItem(position, true);
        }
        Log.v(TAG, "PREV POSITION = " + position);
    }

    if (v.equals(btnPlay)) {
        if (!isPlaying) {
            Log.v(TAG, "in if isPlaying = " + isPlaying);
            runTimerTask();
            isPlaying = true;
            btnPlay.setBackgroundResource(R.drawable.btn_pause);
        } else {
            Log.v(TAG, "in else isPlaying = " + isPlaying);
            mHandler.removeCallbacks(swImageRunnable);
            isPlaying = false;
            btnPlay.setBackgroundResource(R.drawable.btn_play);
        }

    }

    if (v.equals(btnNext)) {
        if (position != (imgUrlList.size() - 1)) {
            position++;
            imgViewPager.setCurrentItem(position, true);
        }
        Log.v(TAG, "NEXT POSITION = " + position);
    }
}// onClick

private class ImagePagerAdapter extends PagerAdapter {

    private ArrayList<String> images;
    private LayoutInflater inflater;

    ImagePagerAdapter(ArrayList<String> images) {
        this.images = images;
        inflater = getLayoutInflater();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public void finishUpdate(View container) {
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public Object instantiateItem(ViewGroup view, int position) {
        View imageLayout = inflater.inflate(R.layout.item_pager_image,
                view, false);
        ImageView imageView = (ImageView) imageLayout
                .findViewById(R.id.image);
        final ProgressBar spinner = (ProgressBar) imageLayout
                .findViewById(R.id.loading);

        imageLoader.displayImage(images.get(position), imageView, options,
                new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String imageUri, View view) {
                        spinner.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onLoadingFailed(String imageUri, View view,
                            FailReason failReason) {
                        switch (failReason.getType()) {
                        case IO_ERROR:
                            break;
                        case DECODING_ERROR:
                            break;
                        case NETWORK_DENIED:
                            break;
                        case OUT_OF_MEMORY:
                            break;
                        case UNKNOWN:
                            break;
                        }
                        spinner.setVisibility(View.GONE);
                    }

                    @Override
                    public void onLoadingComplete(String imageUri,
                            View view, Bitmap loadedImage) {
                        spinner.setVisibility(View.GONE);
                    }
                });

        ((ViewPager) view).addView(imageLayout, 0);
        return imageLayout;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View container) {
    }
}

@Override
protected void onPause() {
    super.onPause();
    if (imageLoader != null) {
        imageLoader.stop();
    }
}

private void runTimerTask() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(3000);
                    mHandler.post(swImageRunnable);
                } catch (Exception e) {
                }
            }
        }
    }).start();
}// runTimerTask

private class SwitchImage implements Runnable {
    @Override
    public void run() {
        if (position != (imgUrlList.size() - 1)) {
            Log.v(TAG, "in runnable if");
            position++;
            imgViewPager.setCurrentItem(position, true);
        } else {
            Log.v(TAG, "in runnable else");
            //mHandler.removeCallbacks(swImageRunnable);
            btnPlay.setBackgroundResource(R.drawable.btn_play);
            isPlaying = false;
        }
    }
}
}// GalleryImageFullscreen
4

1 に答える 1

0

TimerTimerTaskを使用して問題を解決しました これ が私の更新されたコードです:

public class GalleryImageFullscreen extends TIEBaseActivity implements
    OnClickListener {
PrefStore lang_PrefStore;
Resources res;
Configuration config;
Locale mLocale = null;
String language;
boolean isLocaleChanged = false;
ViewPager imgViewPager;
ArrayList<String> imgUrlList;
private ImageLoader imageLoader;
private DisplayImageOptions options;
int position;
ImageButton btnPrev, btnPlay, btnNext;
public static final String TAG = "TIEGalleryFS";
volatile boolean isPlaying;
SwitchImage swImageRunnable;
MyTimerTask myTask;
Timer myTimer;

protected void onCreate(Bundle savedInstanceState) {
    super.createDefaultView(savedInstanceState);

    _headerBar.view.setVisibility(View.VISIBLE);
    _footerBar.view.setVisibility(View.VISIBLE);

    imgUrlList = getIntent().getStringArrayListExtra(
            GalleryActivity.IMAGE_URL_LIST);
    Log.v(TAG, "IMAGE LIST SIZE = " + imgUrlList.size());

    position = getIntent().getIntExtra("POSITION", 0);
    Log.v(TAG, "IMAGE POSITION = " + position);
    swImageRunnable = new SwitchImage();
    super.setHeaderTitle(getResources().getString(R.string.gallery));

    res = getResources();

    lang_PrefStore = new PrefStore();
    language = lang_PrefStore.getLanguageChoice(
            GalleryImageFullscreen.this, FooterBar.LANG_SELECT);
    Log.v("GalleryImageFullscreen language choice", language);

    config = getBaseContext().getResources().getConfiguration();
    if (!language.equals("")
            && language.equals(config.locale.getLanguage())) {
        mLocale = new Locale(language);
        Locale.setDefault(mLocale);
        config.locale = mLocale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());
        Log.v("GalleryImageFullscreen onCreate languageset", "in if loop");
    }

    init();

    imageLoader = ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration
            .createDefault(GalleryImageFullscreen.this));

    options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.image_for_empty_url)
            .showImageForEmptyUri(R.drawable.image_for_empty_url)
            .cacheInMemory().cacheOnDisc()
            .bitmapConfig(Bitmap.Config.RGB_565).build();
}// onCreate

private void init() {
    loadFormFromResource(R.layout.gallery_image_fullscreen);
    imgViewPager = (ViewPager) findViewById(R.id.imgpager);
    btnPrev = (ImageButton) findViewById(R.id.btnPrev);
    btnPlay = (ImageButton) findViewById(R.id.btnPlay);
    btnNext = (ImageButton) findViewById(R.id.btnNext);

    if ((imgUrlList != null) && (imgUrlList.size() > 0)) {
        imgViewPager.setAdapter(new ImagePagerAdapter(imgUrlList));
        imgViewPager.setCurrentItem(position);
    }

    btnPrev.setOnClickListener(this);
    btnPlay.setOnClickListener(this);
    btnNext.setOnClickListener(this);
}// init

public void onClick(View v) {
    if (v.equals(btnPrev)) {
        if (position != 0) {
            position--;
            imgViewPager.setCurrentItem(position, true);
        }
        Log.v(TAG, "PREV POSITION = " + position);
    }

    if (v.equals(btnPlay)) {
        if (!isPlaying) {
            Log.v(TAG, "in if isPlaying = " + isPlaying);
            runTimerTask();
            isPlaying = true;
            btnPlay.setBackgroundResource(R.drawable.btn_pause);
        } else {
            Log.v(TAG, "in else isPlaying = " + isPlaying);
            isPlaying = false;
            myTask.cancel();
            myTimer.cancel();
            btnPlay.setBackgroundResource(R.drawable.btn_play);
        }

    }

    if (v.equals(btnNext)) {
        if (position != (imgUrlList.size() - 1)) {
            position++;
            imgViewPager.setCurrentItem(position, true);
        }
        Log.v(TAG, "NEXT POSITION = " + position);
    }
}// onClick

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.tie_options_menu, menu);
    return true;
}// onCreateOptionsMenu

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_Eng:
        lang_PrefStore.setLanguageChoice(GalleryImageFullscreen.this,
                FooterBar.LANG_SELECT, "en");
        mLocale = new Locale("en");
        Locale.setDefault(mLocale);
        config.locale = mLocale;
        getResources().updateConfiguration(config,
                getResources().getDisplayMetrics());
        isLocaleChanged = true;
        Toast.makeText(GalleryImageFullscreen.this, "English",
                Toast.LENGTH_SHORT).show();
        refreshActivity();
        break;

    case R.id.menu_German:
        lang_PrefStore.setLanguageChoice(GalleryImageFullscreen.this,
                FooterBar.LANG_SELECT, "de");
        mLocale = new Locale("de");
        Locale.setDefault(mLocale);
        config.locale = mLocale;
        getResources().updateConfiguration(config,
                getResources().getDisplayMetrics());
        isLocaleChanged = true;
        Toast.makeText(GalleryImageFullscreen.this, "German",
                Toast.LENGTH_SHORT).show();
        refreshActivity();
        break;

    case R.id.menu_Russian:
        lang_PrefStore.setLanguageChoice(GalleryImageFullscreen.this,
                FooterBar.LANG_SELECT, "ru");
        mLocale = new Locale("ru");
        Locale.setDefault(mLocale);
        config.locale = mLocale;
        getResources().updateConfiguration(config,
                getResources().getDisplayMetrics());
        isLocaleChanged = true;
        Toast.makeText(GalleryImageFullscreen.this, "Russian",
                Toast.LENGTH_SHORT).show();
        refreshActivity();
        break;
    }// switch
    return super.onOptionsItemSelected(item);
}// onOptionsItemSelected

private class ImagePagerAdapter extends PagerAdapter {

    private ArrayList<String> images;
    private LayoutInflater inflater;

    ImagePagerAdapter(ArrayList<String> images) {
        this.images = images;
        inflater = getLayoutInflater();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public void finishUpdate(View container) {
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public Object instantiateItem(ViewGroup view, int position) {
        View imageLayout = inflater.inflate(R.layout.item_pager_image,
                view, false);
        ImageView imageView = (ImageView) imageLayout
                .findViewById(R.id.image);
        final ProgressBar spinner = (ProgressBar) imageLayout
                .findViewById(R.id.loading);

        imageLoader.displayImage(images.get(position), imageView, options,
                new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String imageUri, View view) {
                        spinner.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onLoadingFailed(String imageUri, View view,
                            FailReason failReason) {
                        switch (failReason.getType()) {
                        case IO_ERROR:
                            break;
                        case DECODING_ERROR:
                            break;
                        case NETWORK_DENIED:
                            break;
                        case OUT_OF_MEMORY:
                            break;
                        case UNKNOWN:
                            break;
                        }
                        spinner.setVisibility(View.GONE);
                    }

                    @Override
                    public void onLoadingComplete(String imageUri,
                            View view, Bitmap loadedImage) {
                        spinner.setVisibility(View.GONE);
                    }
                });

        ((ViewPager) view).addView(imageLayout, 0);
        return imageLayout;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View container) {
    }
}

@Override
protected void onPause() {
    super.onPause();
    if (imageLoader != null) {
        imageLoader.stop();
    }
}

private void runTimerTask() {
    myTask = new MyTimerTask();
    myTimer = new Timer();
    myTimer.scheduleAtFixedRate(myTask, 0, 3000);
}// runTimerTask

private class SwitchImage implements Runnable {
    @Override
    public void run() {
        if (position != (imgUrlList.size() - 1)) {
            Log.v(TAG, "in runnable if");
            position++;
            imgViewPager.setCurrentItem(position, true);
        } else {
            Log.v(TAG, "in runnable else");
            btnPlay.setBackgroundResource(R.drawable.btn_play);
            isPlaying = false;
        }
    }// run
}// SwitchImage

class MyTimerTask extends TimerTask {
    final Handler mHandler = new Handler(Looper.getMainLooper());

    public void run() {
        mHandler.post(swImageRunnable);
    }// run
}// MyTimerTask

@Override
protected void onDestroy() {
    if(myTask != null) {
        myTask = null;
    }

    if(myTimer != null) {
        myTimer.purge();
    }
    super.onDestroy();
}
}// GalleryImageFullscreen
于 2013-04-17T06:11:27.100 に答える