Alternative to setAlpha in api level 8
Asked Answered
P

5

18

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 ?

Pamphleteer answered 26/5, 2013 at 1:56 Comment(1)
stackoverflow.com/a/4814651Arsenault
D
16

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.

Durian answered 26/5, 2013 at 3:12 Comment(2)
Yes. When searched for the solution, i found, most of them were recommended for this NineOldAndroids. But I don't understand how to implement this. Can you guide me how to implement for the above class i've written.Pamphleteer
@shree202 - take a look at the ViewHelper class - just use ViewHelper.setAlpha(view, alpha) rather than view.setAlpha(alpha), etc.Durian
A
51

setAlphaAnimation … API LEVEL 1

Here is some relevant code I copied from the answer at Set Alpha/Opacity of Layout

AlphaAnimation alpha = new AlphaAnimation(0.7F, 0.7F);
alpha.setDuration(0); // Make animation instant
alpha.setFillAfter(true); // Tell it to persist after the animation
ends view.startAnimation(alpha);

setAlpha INT -> From API LEVEL 1 -> deprecated in 16

Drawable background = localLinLayout.getBackground();
background.setAlpha(180); //255 is max (visible)

setAlpha FLOAT -> API Level 11+ ->


setImageAlpha -> API LEVEL 16+ (use just for ImageViews)

Amazing answered 25/9, 2013 at 7:38 Comment(2)
what about others like setScaleX() setScaleY() ?Harass
as define in questionHarass
D
16

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.

Durian answered 26/5, 2013 at 3:12 Comment(2)
Yes. When searched for the solution, i found, most of them were recommended for this NineOldAndroids. But I don't understand how to implement this. Can you guide me how to implement for the above class i've written.Pamphleteer
@shree202 - take a look at the ViewHelper class - just use ViewHelper.setAlpha(view, alpha) rather than view.setAlpha(alpha), etc.Durian
Q
3

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);
        }
    }
}
Questor answered 9/1, 2014 at 20:48 Comment(1)
+1 Now this one would provide a nice help to some one.Pamphleteer
C
2

Also, If you want use PageTransformer even in 2.2. You must change some validation that are made in the ViewPager class. Check out this example

Cantwell answered 13/10, 2013 at 22:55 Comment(0)
R
2

To be both forward and backward compatible I use following implementation for my views:

private void fadeOut(final View view) {
    int sdk = android.os.Build.VERSION.SDK_INT;
    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        AlphaAnimation alpha = new AlphaAnimation(1f, 0f);
        alpha.setDuration(mAnimationDuration);
        alpha.setFillAfter(true);
        view.startAnimation(alpha);
    } else {
        view.animate()
        .alpha(0f)
        .setDuration(mAnimationDuration)
        .setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
            }
        });
    }
}

private void fadeIn(View view) {
    int sdk = android.os.Build.VERSION.SDK_INT;
    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        view.setVisibility(View.VISIBLE);
        AlphaAnimation alpha = new AlphaAnimation(0f, 1f);
        alpha.setDuration(mAnimationDuration);
        alpha.setFillAfter(true);
        view.startAnimation(alpha);
    } else {
        view.setAlpha(0f);
        view.setVisibility(View.VISIBLE);

        view.animate()
        .alpha(1f)
        .setDuration(mAnimationDuration)
        .setListener(null);
    }
}
Redroot answered 27/11, 2013 at 8:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.