There are a lot of blogs that implement ViewPager infinite rotation, but I won’t go into that. There are a lot of good open source libraries out there that are very useful, like Banner, so why don’t we implement our own alternative rotation. Look at the picture first:

public class DepthPageTransformer implements ViewPager.PageTransformer {

private Rotate3dAnimation rotation;

public void transformPage(View view, float position) {

    if (UserBean.ITEMCREND) {

        startRotation(view, 12.0);/ / tilt

    } else{

        startRotation(view, -12.0);

    }

}



private void startRotation(View image, float start, float end) {

    // Compute center point

    final float centerX = image.getWidth() / 2.0 f;

    final float centerY = image.getHeight() / 2.0 f;

    rotation = new Rotate3dAnimation(start, end, centerX, centerY, 0f.true);

    rotation.setDuration(500);

    rotation.setFillAfter(true);



    // Constant rotation

    //rotation.setInterpolator(new DecelerateInterpolator());

    image.startAnimation(rotation);





}

Copy the code

}

So we’ve got a View in the constructor transformPage(View View, float Position) and this View represents every item in the viewPager, so we’re going to use that item to implement some special effects and we’re going to see what I just saw, Rotate3dAnimation is a Google open source 3D rotation effect. I’m going to modify it a little bit here using the viewPager adapter

public class MyPageAdapter extends PagerAdapter {

private Context context;

private ArrayList<Integer> imgList;



public MyPageAdapter(Context context, ArrayList<Integer> newsList{

    this.context = context;

    this.imgList = newsList;

}



@Override

public int getCount(
{

    return Integer.MAX_VALUE;/ / Max



}



@Override

public boolean isViewFromObject(View view, Object object
{

    return view == object;

}



public Object instantiateItem(ViewGroup container, final int position{



    View view = View.inflate(context, R.layout.itempage, null);

    ImageView imageView = (ImageView) view.findViewById(R.id.img);

    imageView.setBackgroundResource(imgList.get(position % imgList.size()));

    imageView.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view
{

            onItemClickListener.onClick(view,position % imgList.size());

        }

    });

    container.addView(view);

    return view;



}

// Delete the view

@Override

public void destroyItem(ViewGroup container, int position, Object object
{

    container.removeView((View) object);

}



/ * *

* click

* /


public interface OnItemClickListener {

    void onClick(View view, int position);

}



OnItemClickListener onItemClickListener;



public void setOnItemClickListener(OnItemClickListener onItemClickListener{

    this.onItemClickListener = onItemClickListener;

}

Copy the code

}

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

private ViewPager viewPager;

private ArrayList<Integer> arrayList;

private MyPageAdapter myPageAdapter;

private int mPagerPosition;// The current item

private int firstTime, secondTime, slideNumber = 0;// The first and second finger touch time and the number of swipes

private int mPagerIndex = 0;

private int pageCurrentItem;// The currently selected item



@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);



    viewPager = findViewById(R.id.viewpage);

    arrayList = new ArrayList();

    arrayList.add(R.drawable.jt1);

    arrayList.add(R.drawable.jt2);

    arrayList.add(R.drawable.jt3);



    myPageAdapter = new MyPageAdapter(MainActivity.this, arrayList);

    viewPager.setAdapter(myPageAdapter);

    viewPager.setCurrentItem(Integer.MAX_VALUE / 2);// pseudo-loop MAX_VALUE the maximum value is about 2.1 billion

    viewPager.addOnPageChangeListener(this);

    viewPager.setPageMargin(-270);// The interval between items



    viewPager.setOffscreenPageLimit(2); // Cache the number of items

    viewPager.setPageTransformer(true.new DepthPageTransformer());



    ViewPagerScroller scroller = new ViewPagerScroller(MainActivity.this);

    scroller.setScrollDuration(500);// Set the transition time to 500 ms

    scroller.initViewPagerScroll(viewPager);

    handler.sendEmptyMessageDelayed(0x001.1000);



    viewPager.setOnTouchListener(new View.OnTouchListener() {

        @Override

        public boolean onTouch(View view, MotionEvent motionEvent) {



            switch (motionEvent.getAction()) {

                case MotionEvent.ACTION_DOWN:

                case MotionEvent.ACTION_MOVE:

                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mmssSSS");//yyyyMMddHH

                    firstTime = Integer.parseInt(simpleDateFormat.format(new Date()));

                    handler.removeMessages(0x001);

                    slideNumber++;// Slide times

                    break;

                case MotionEvent.ACTION_UP:

                    if (mPagerIndex >= 1) {

                        if (firstTime - secondTime > 2000) {

                            if(firstTime ! = secondTime) {

                                handler.sendEmptyMessageDelayed(0x001.2000);

                            }

                        } else {

                            handler.sendEmptyMessageDelayed(0x002.2000);

                        }

                    }

                    if (slideNumber < 2) {// Resolve if the user only slides once

                        handler.sendEmptyMessageDelayed(0x001.2000);

                    }

                    secondTime = firstTime;

                    mPagerIndex++;

                    break;



                default:

                    break;

            }





            return false;

        }

    });

    myPageAdapter.setOnItemClickListener(new MyPageAdapter.OnItemClickListener() {

        @Override

        public void onClick(View view, int position) {

            Log.i("position==", position + "");



        }

    });





}



Handler handler = new Handler() {

    @Override

    public void handleMessage(Message msg) {

        super.handleMessage(msg);

        if (msg.what == 0x001) {

            pageCurrentItem = viewPager.getCurrentItem() + 1;

            viewPager.setCurrentItem(pageCurrentItem);

            handler.removeMessages(0x001);

            handler.sendEmptyMessageDelayed(0x001.2000);



        } else if (msg.what == 0x002) {

            handler.removeMessages(0x001);

            handler.sendEmptyMessageDelayed(0x001.1000);

        }



    }

};



@Override

public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {



    if (mPagerPosition == position || mPagerPosition + 1== position && mPagerPosition ! =0) {

        UserBean.ITEMCREND = false;

    } else {

        UserBean.ITEMCREND  = true;

    }

}



@Override

public void onPageSelected(int position) {



}



@Override

public void onPageScrollStateChanged(int state) {

    if (state == 1) {// There are three states of state, as we'll see later, when the finger first touches the screen state is 1, and that's when we assign mViewPagerIndex.

        mPagerPosition = viewPager.getCurrentItem();

    }

}



@Override

protected void onDestroy(a) {

    super.onDestroy();

    handler.removeMessages(0x001);

    handler.removeMessages(0x002);

    slideNumber = 0;

}

Copy the code

}

public class ViewPagerScroller extends Scroller { private int mScrollDuration = 1000; // Slide speed

/ * *

* Set speed speed

 *

 * @param duration

* /




public void setScrollDuration(int duration) {



    this.mScrollDuration = duration;



}





public ViewPagerScroller(Context context) {



    super(context);



}



public ViewPagerScroller(Context context, Interpolator interpolator) {



    super(context, interpolator);



}



public ViewPagerScroller(Context context, Interpolator interpolator, boolean flywheel) {



    super(context, interpolator, flywheel);



}



@Override



public void startScroll(int startX, int startY, int dx, int dy, int duration) {



    super.startScroll(startX, startY, dx, dy, mScrollDuration);



}



@Override



public void startScroll(int startX, int startY, int dx, int dy) {



    super.startScroll(startX, startY, dx, dy, mScrollDuration);



}



public void initViewPagerScroll(ViewPager viewPager) {



    try {



        Field mScroller = ViewPager.class.getDeclaredField("mScroller");



        mScroller.setAccessible(true);



        mScroller.set(viewPager, this);



    } catch (Exception e) {



        e.printStackTrace();



    }



}

Copy the code

}

Gitee.com/duanpingwei…