使用viewpager android幻灯片放映屏幕

时间:2013-05-31 07:36:08

标签: android timer android-viewpager

我正在开发一个Android应用程序,我想创建一个屏幕幻灯片。我正在使用viewpager在屏幕之间滑动,并使用java Timer每5秒自动幻灯片显示。

问题是,我无法从一个屏幕慢慢平稳过渡到另一个屏幕。一旦5秒完成,它立即移动到下一个屏幕。我需要一个缓慢而平稳的过渡。这是可能的吗?。请查看我的计时器代码&查看下面的寻呼机代码。

     public void slideshowtimer()
     {
      t.scheduleAtFixedRate(new TimerTask() 
       {

        @Override
        public void run() 
        {
            runOnUiThread(new Runnable() 
            {

                public void run() 
                {
                    Log.e("inside", "timer - "  + viewPagerCurrentItem);

                    myPager.setCurrentItem(viewPagerCurrentItem++);

                }

            });
        }

    }, 0, 5000);
}



              @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    t = new Timer();

    MyPagerAdapter adapter = new MyPagerAdapter();
    myPager = (ViewPager) findViewById(R.id.viewPager);
    myPager.setAdapter(adapter);

    slideshowtimer();

    //myPager.setCurrentItem(0);
}





private class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
        return 3;
    }

    public Object instantiateItem(View collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int resId = 0;
        switch (position) {
        case 0:                         

            //  resId = LayoutOne.newInstance(_context);    

            resId = R.layout.layout_one;
            break;
        case 1:
            resId = R.layout.layout_two;
            break;
        case 2:
            resId = R.layout.layout_third;
            break;

        }

        View view = inflater.inflate(resId, null);

        ((ViewPager) collection).addView(view, 0);

        return view;
    }

请帮忙。谢谢!

2 个答案:

答案 0 :(得分:1)

您是否尝试过将Scroll设置为true?看看方法here

答案 1 :(得分:1)

您是否尝试将自己的卷轴添加到ViewPager。您可以使用滚动类中的持续时间设置滚动动画。 我是这样做的:

viewFlow=(ViewPager)findViewById(R.id.presentationViewPager);
try {
        Field mScroller;
        Interpolator sInterpolator = new DecelerateInterpolator();
        mScroller = ViewPager.class.getDeclaredField("mScroller");
        mScroller.setAccessible(true); 
        FixedSpeedScroller scroller = new FixedSpeedScroller(viewFlow.getContext(),   sInterpolator);
        // scroller.setFixedDuration(5000);
        mScroller.set(viewFlow, scroller);
    } catch (NoSuchFieldException e) {
    } catch (IllegalArgumentException e) {
    } catch (IllegalAccessException e) {
    }

这是我的Scroller代码。:

public class FixedSpeedScroller extends Scroller {

private int mDuration = 600;

public FixedSpeedScroller(Context context) {
    super(context);
}

public FixedSpeedScroller(Context context, Interpolator interpolator) {
    super(context, interpolator);
}

public FixedSpeedScroller(Context context,Interpolator interpolator, boolean flywheel) {
    super(context, interpolator, flywheel);
}


@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
    // Ignore received duration, use fixed one instead
    super.startScroll(startX, startY, dx, dy, mDuration);
}

@Override
public void startScroll(int startX, int startY, int dx, int dy) {
    // Ignore received duration, use fixed one instead
    super.startScroll(startX, startY, dx, dy, mDuration);
}

}