アプリにスライドショーを実装する必要があります。これには、 を使用し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