Alternative to setAlpha in api level 8

后端 未结 5 1336
别那么骄傲
别那么骄傲 2021-01-01 17:25

I am working on an app, which can run on Froyo as well as JellyBean. I have a class that extends PageTransformer as below:

import a         


        
5条回答
  •  失恋的感觉
    2021-01-01 18:27

    In case anyone is cough lazy and wants the ZoomOutPageTransformer compatible back to API 8. Uses nineoldandroids and the ViewHelper class.

    package com.weddingwire.vendorsearch.Animation;
    
    import android.support.v4.view.ViewPager;
    import android.view.View;
    
    import com.nineoldandroids.view.ViewHelper;
    
    public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
        private static float MIN_SCALE = 0.85f;
        private static float MIN_ALPHA = 0.5f;
    
        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.
                ViewHelper.setAlpha(view, 0);
    
            } else if (position <= 1) { // [-1,1]
                // Modify the default slide transition to shrink the page as well
                float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
                float vertMargin = pageHeight * (1 - scaleFactor) / 2;
                float horzMargin = pageWidth * (1 - scaleFactor) / 2;
                if (position < 0) {
                    ViewHelper.setTranslationX(view, (horzMargin - vertMargin / 2));
                } else {
                    ViewHelper.setTranslationX(view, -horzMargin + vertMargin / 2);
                }
    
                // Scale the page down (between MIN_SCALE and 1)
                ViewHelper.setScaleX(view, scaleFactor);
                ViewHelper.setScaleY(view, scaleFactor);
    
                // Fade the page relative to its size.
                ViewHelper.setAlpha(view, MIN_ALPHA +
                        (scaleFactor - MIN_SCALE) /
                                (1 - MIN_SCALE) * (1 - MIN_ALPHA));
    
            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                ViewHelper.setAlpha(view, 0);
            }
        }
    }
    

提交回复
热议问题