[英]Android ViewPager - adding slideshow with fade-in/out animation to switch between views
I use ViewPager
to implement my own image gallery. 我使用ViewPager
来实现我自己的图库。 The views consists of ImageView
s. 视图由ImageView
组成。 The user can normally navigate between images using gestures (as in ViewPager
). 用户通常可以使用手势在图像之间导航(如在ViewPager
)。
What I want to add is the slideshow feature. 我要添加的是幻灯片功能。 When user chooses an option "slideshow" from the menu I want to be able to start the animation of ViewPager
items - ideally the animation would be the fade-in/out effect between images/slides. 当用户从菜单中选择“幻灯片放映”选项时,我希望能够启动ViewPager
项目的动画 - 理想情况下,动画将是图像/幻灯片之间的淡入/淡出效果。
Is it possible to implement this with ViewPager
? 是否可以使用ViewPager
实现此ViewPager
?
Here's how I did it. 这就是我做到的。 First I used a lot of the code from the Android Displaying Bitmaps in Your UI tutorial . 首先,我在您的UI教程中使用了Android显示位图中的大量代码。
Then, I added the following tweaks: 然后,我添加了以下调整:
In the OnCreate of the activity that contains the ViewPager: 在包含ViewPager的活动的OnCreate中:
mPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
PhotoViewerFragment currentFragment = mAdapter
.getFragment(arg0);
if (currentFragment != null) {
if (mRunningSlideshow) {
currentFragment.addImageTransition();
}
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
});
Runnable that runs slideshow in the activity that contains the ViewPager: 在包含ViewPager的活动中运行幻灯片的Runnable:
private Runnable runSlideshow = new Runnable() {
public void run() {
// Second parameter of false turns ViewPager scroll animation off
mPager.setCurrentItem(mPager.getCurrentItem() + 1, false);
mSlideshowHandler.postDelayed(runSlideshow,
SLIDESHOW_IMAGE_DURATION);
}
};
In the fragment that displays the image: 在显示图像的片段中:
private static final int FADE_IN_TIME = 200;
/**
* Adds fade-in transition when in slideshow mode. Called from PhotoViewerActivity.
*/
public void addImageTransition() {
// Transition drawable with a transparent drawable and the final bitmap
final TransitionDrawable td = new TransitionDrawable(new Drawable[] {
new ColorDrawable(android.R.color.transparent),
mImageView.getDrawable() });
mImageView.setImageDrawable(td);
td.startTransition(FADE_IN_TIME);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.