OnPageChangeListener alpha crossfading

后端 未结 1 1024
挽巷
挽巷 2020-12-28 10:10

There are a lot of questions with regard to crossfading in Android, but they all include animations. My question is about crossfading using the OnPageChangeListener of a Vie

相关标签:
1条回答
  • 2020-12-28 11:02

    ViewPager.PageTransformer is your friend. I'm going to take a different approach to what you tried, but it results in what I understand to bed your desired result - swiping left/right swipes the content, but fades between two background images that don't move.

    Each Fragment in the ViewPager will have a layout like so:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/image_view"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:scaleType="center" />
        <LinearLayout
            android:id="@+id/content_area"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!-- content goes here -->
        </LinearLayout>
    </FrameLayout>
    

    And you will create a PageTransformer that manipulates the layout depending the position it has been swiped:

    public class CustomPageTransformer implements ViewPager.PageTransformer {
        public void transformPage(View view, float position) {
            int pageWidth = view.getWidth();
            View imageView = view.findViewById(R.id.image_view);
            View contentView = view.findViewById(R.id.content_area);
    
            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left
            } else if (position <= 0) { // [-1,0]
                // This page is moving out to the left
    
                // Counteract the default swipe
                view.setTranslationX(pageWidth * -position);
                if (contentView != null) {
                    // But swipe the contentView
                    contentView.setTranslationX(pageWidth * position);
                }
                if (imageView != null) {
                    // Fade the image in
                    imageView.setAlpha(1 + position);
                }
    
            } else if (position <= 1) { // (0,1]
                // This page is moving in from the right
    
                // Counteract the default swipe
                view.setTranslationX(pageWidth * -position);
                if (contentView != null) {
                    // But swipe the contentView
                    contentView.setTranslationX(pageWidth * position);
                }
                if (imageView != null) {
                    // Fade the image out
                    imageView.setAlpha(1 - position);
                }
            } else { // (1,+Infinity]
                // This page is way off-screen to the right
            }
        }
    }
    

    And finally hook this PageTransformer up to your ViewPager:

    mViewPager.setPageTransformer(true, new CustomPageTransformer());
    

    I've tested it in an existing app and it works well as long as the fragment layouts have a transparent background.

    0 讨论(0)
提交回复
热议问题