1

我正在开发一个 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;
    }

请帮忙。谢谢!

4

2 回答 2

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) {
    }

这是我的滚动代码:

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);
}

}

于 2013-06-13T09:51:20.757 回答
1

您是否尝试将平滑滚动设置为真?看看这里的方法。

于 2013-05-31T07:41:37.313 回答