androidandroid-4.2-jelly-beanandroid-2.2-froyo

Alternative to setAlpha in api level 8


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 android.support.v4.view.ViewPager.PageTransformer;
import android.view.View;

public class ZoomOutPageTransformer implements 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) { 
            view.setAlpha(0);

        } else if (position <= 1) { 
            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) {
                view.setTranslationX(horzMargin - vertMargin / 2);
            } else {
                view.setTranslationX(-horzMargin + vertMargin / 2);
            }


            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);


            view.setAlpha(MIN_ALPHA +
                    (scaleFactor - MIN_SCALE) /
                    (1 - MIN_SCALE) * (1 - MIN_ALPHA));

        } else { 
            view.setAlpha(0);
        }
    }
}

The problem is, the methods written in this class were added in API level 11 and i have minimum sdk version of API level 8. The following are those methods that were added in API level 11:

  1. setAlpha()
  2. setTranslationX()
  3. setScaleX()
  4. setScaleY()

What can be the solution for this problem ?


Solution

  • The easiest solution is to use the NineOldAndroids library, which backports the animations to all versions of Android. The Usage section has examples of how you'd use the library.