ViewPager achieves page swiping, while viewpager slides pages.

Source: Internet
Author: User

ViewPager achieves page swiping, while viewpager slides pages.

 

To achieve the sliding paging Effect of ViewPager, you can use the setPageTransformer method of ViewPager as follows:

import android.content.Context;import android.support.v4.view.ViewPager;import android.util.AttributeSet;import android.view.View;public class ReadViewPager extends ViewPager{    public ReadViewPager(Context context)    {        super(context);    }    public ReadViewPager(Context context, AttributeSet attrs)    {        super(context, attrs);        setAnima();    }    public void setAnima()    {        setPageTransformer(true, new PageTransformer()        {            private static final float MIN_SCALE = 0.75f;            @Override            public void transformPage(View view, float position)            {                int pageWidth = view.getWidth();                int pageHeight =view.getHeight();                if (position < -1)                {                    // [-Infinity,-1)                    // This page is way off-screen to the left.                    view.setAlpha(0);                }                else if (position <= 0)                {                    // [-1,0]                    // Use the default slide transition when moving to the left page                    view.setAlpha(1);                    view.setTranslationX(0);                    view.setScaleX(1);                    view.setScaleY(1);                }                else if (position <= 1)                {                    // (0,1]                    // Fade the page out.                    view.setAlpha(1 - position);////                    // Counteract the default slide transition//                    view.setAlpha(1);                    view.setTranslationX(pageWidth * -position);////                    // Scale the page down (between MIN_SCALE and 1)                    float scaleFactor = MIN_SCALE                            + (1 - MIN_SCALE) * (1 - Math.abs(position));                    view.setScaleX(scaleFactor);                    view.setScaleY(scaleFactor);                }                else                {                    // (1,+Infinity]                    // This page is way off-screen to the right.                    view.setAlpha(0);                }            }        });    }}

The core code is the official android demo. The above implementation is page flip: when the previous page is slide out of the interface, the following page gradually appears, the transparency increases slowly, and the size increases from small to small.

However, there was a strange phenomenon. When I made a Q & A interface, I used ViewPager and View to implement infinite loop switching, and used the above animation effect, however, when I slide to the last one, that is, to start a new round of loop, the page that is slide out will become transparent, however, if I use ViewPager and Fragment to implement infinite loop switching, this will not happen.

If you want to achieve the paging effect of the fake driving test book, you just need to change the core method to the following code.

setPageTransformer(true, new PageTransformer() {            private static final float MIN_SCALE = 0.75f;            @Override            public void transformPage(View view, float position) {                int pageWidth = view.getWidth();                 int pageHeight =view.getHeight();                if (position < -1) { // [-Infinity,-1)                    // This page is way off-screen to the left.                    view.setAlpha(0);                } else if (position <= 0) { // [-1,0]                    // Use the default slide transition when moving to the left page                    view.setAlpha(1);                    view.setTranslationX(0);                    view.setScaleX(1);                    view.setScaleY(1);                } else if (position <= 1) { // (0,1]                    // Fade the page out.//                    view.setAlpha(1 - position);////                    // Counteract the default slide transition                    view.setAlpha(1);                    view.setTranslationX(pageWidth * -position);////                    // Scale the page down (between MIN_SCALE and 1)//                    float scaleFactor = MIN_SCALE//                            + (1 - MIN_SCALE) * (1 - Math.abs(position));//                    view.setScaleX(scaleFactor);//                    view.setScaleY(scaleFactor);                } else { // (1,+Infinity]                    // This page is way off-screen to the right.                    view.setAlpha(0);                }            }        });

The position parameter is described as follows:

The possible values of position are:

[-Infinity,-1) No more

(1, + Infinity] No more

[-1, 1]

For more information, see ~~

 

Suppose that ViewPager slides out of page B on page A, then:

The position change on page A is (0,-1]

The position change on page B is [1, 0].

Reference blog: http://blog.csdn.net/lmj623565791/article/details/40411921

Http://www.jianshu.com/p/251592d3ec62

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.