android API level 8中setAlpha的替代方法

vjhs03f7  于 2023-03-27  发布在  Android
关注(0)|答案(5)|浏览(207)

我正在开发一个应用程序,它可以在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中添加的,而我有最低的API level 8 sdk版本。以下是在API级别11中添加的方法:

  1. setAlpha()
  2. setTranslationX()
  3. setScaleX()
  4. setScaleY()
    解决这个问题的办法是什么?
xoefb8l8

xoefb8l81#

setAlphaAnimation ... API LEVEL 1

下面是我从设置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 -〉在16中弃用

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

setAlpha FLOAT -〉API Level 11+ -〉
setImageAlpha-〉API LEVEL 16+(仅用于ImageViews)

q5lcpyga

q5lcpyga2#

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

f8rj6qna

f8rj6qna3#

如果有人 * 咳嗽 * 懒惰,并希望ZoomOutPageTransformer兼容回到API 8。使用nineoldandroid和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);
        }
    }
}
7hiiyaii

7hiiyaii4#

另外,如果你想在2.2中使用PageTransformer,你必须修改ViewPager类中的一些验证。看看这个example

mzsu5hc0

mzsu5hc05#

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

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

相关问题