减慢viewpager

时间:2017-03-05 11:20:25

标签: android image timer android-viewpager

我在视图寻呼机中设置了一组图像我希望图像能够以慢动作自动滚动。我的问题是图像移动得非常快如何在我的代码中解决该怎么做 寻呼机适配器`

public class CustomerAdapter extends PagerAdapter {
//private int[] images = {R.mipmap.img1,R.mipmap.img2,R.mipmap.img3,R.mipmap.img4};
private Context ctx;
private  String[] urls;
private LayoutInflater inflater;
final Handler handler = new Handler();
public Timer swipeTimer ;

public CustomerAdapter(Context ctx,String []urls){
    this.ctx = ctx;
    this.urls=urls;
}
@Override
public int getCount() {
    return urls.length;
}

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.swip,container,false);
    ImageView img =(ImageView)v.findViewById(R.id.itemview);

    Picasso.with(ctx).load(urls[position]).into(img);


    container.addView(v);
    return v;
}

@Override
public void destroyItem(View container, int position, Object object) {
    container.refreshDrawableState();
}
public void setTimer(final ViewPager myPager, int time, final int numPages, final int curPage){

    final Runnable Update = new Runnable() {
        int NUM_PAGES =numPages;
        int currentPage = curPage ;
        public void run() {
            if (currentPage == NUM_PAGES ) {
                currentPage = 0;
            }
            myPager.setCurrentItem(currentPage++, true);
        }
    };

    swipeTimer = new Timer();
    swipeTimer.schedule(new TimerTask() {

        @Override
        public void run() {
            handler.post(Update);
        }
    }, 1000, time*1000);

}
public void stopTimer(){
    //handler.removeCallbacks(null);
    swipeTimer.cancel();
}
}

在我的主页

 ViewPager viewPager =  (ViewPager)convertView.findViewById(R.id.view_pager);
    adapter = new CustomerAdapter(getContext(),imagesfortest);
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(0);
    adapter.setTimer(viewPager,5000,4,imagesfortest.length);

1 个答案:

答案 0 :(得分:0)

设置转换。 Reference

mPager.setPageTransformer(true, new ZoomOutPageTransformer());

然后自定义变换动画。