我正在处理一个可以在Froyo
和JellyBean
上运行的应用。我有一个扩展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
。以下是API级别11中添加的那些方法:
setAlpha()
setTranslationX()
setScaleX()
setScaleY()
这个问题的解决方案是什么?
答案 0 :(得分:51)
所以,这里有一些决议:
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 - &gt;来自API LEVEL 1 - &gt;已弃用16
Drawable background = localLinLayout.getBackground();
background.setAlpha(180); //255 is max (visible)
setAlpha
FLOAT - &gt; API等级11+ - &gt;
setImageAlpha
- &gt; API LEVEL 16 + (仅用于ImageViews)
答案 1 :(得分:16)
最简单的解决方案是使用NineOldAndroids库,将动画向后移植到所有版本的Android。 Usage section提供了如何使用该库的示例。
答案 2 :(得分: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);
}
}
}
答案 3 :(得分:2)
另外,如果你想在2.2中使用PageTransformer
。您必须更改ViewPager
类中的一些验证。看看这个example
答案 4 :(得分: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);
}
}