18

我正在开发一个应用程序,它可以FroyoJellyBean. 我有一个扩展 PageTransformer 的类,如下所示:

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);
        }
    }
}

问题是,在这个类中编写的方法被添加了API level 11,我有最小的 .sdk 版本API level 8。以下是 API 级别 11 中添加的方法:

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

有什么办法可以解决这个问题?

4

5 回答 5

51

so, some resolutions here:


setAlphaAnimation … API LEVEL 1

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)

于 2013-09-25T07:38:52.260 回答
16

最简单的解决方案是使用NineOldAndroids库,它将动画反向移植到所有版本的 Android。使用部分提供了如何使用该库的示例。

于 2013-05-26T03:12:31.960 回答
3

万一有人懒惰并希望将ZoomOutPageTransformer兼容回 API 8。使用 Nineoldandroids 和 ViewHelper 类。

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);
        }
    }
}
于 2014-01-09T20:48:02.510 回答
2

为了向前和向后兼容,我对我的视图使用以下实现:

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);
    }
}
于 2013-11-27T08:33:17.840 回答
2

另外,如果你想PageTransformer在 2.2 中使用。您必须更改在ViewPager类中进行的一些验证。看看这个例子

于 2013-10-13T22:55:19.930 回答