我想在 FloatingActionButton
我尝试过的
public void animFab() {
ObjectAnimator scaleX = ObjectAnimator.ofFloat(fab, View.SCALE_X, from, to);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(fab, View.SCALE_Y, from, to);
ObjectAnimator translationZ = ObjectAnimator.ofFloat(fab, View.TRANSLATION_Z, from, to);
AnimatorSet set1 = new AnimatorSet();
set1.playTogether(scaleX, scaleY, translationZ);
set1.setDuration(500);
set1.setInterpolator(new AccelerateInterpolator());
set1.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
}
});
ObjectAnimator scaleXBack = ObjectAnimator.ofFloat(fab, View.SCALE_X, to, from);
ObjectAnimator scaleYBack = ObjectAnimator.ofFloat(fab, View.SCALE_Y, to, from);
ObjectAnimator translationZBack = ObjectAnimator.ofFloat(fab, View.TRANSLATION_Z, to, from);
Path path = new Path();
path.moveTo(0.0f, 0.0f);
path.lineTo(0.5f, 1.3f);
path.lineTo(0.75f, 0.8f);
path.lineTo(1.0f, 1.0f);
PathInterpolator pathInterpolator = new PathInterpolator(path);
AnimatorSet set2 = new AnimatorSet();
set2.playTogether(scaleXBack, scaleYBack, translationZBack);
set2.setDuration(500);
set2.setInterpolator(pathInterpolator);
final AnimatorSet set = new AnimatorSet();
set.playSequentially(set1, set2);
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
set.start();
}
});
set.start();
}
问题
上面的代码在Lolipop和更高版本的设备上正常工作,但在KitKat设备上不工作
以下是我尝试过的一些链接
任何人都可以帮助解决KitKat设备中的问题
如果需要更多信息,请告诉我。提前致谢。您的努力将不胜感激。
您会看到以下字段的“字段需要API 21” Studio Lint错误,该代码在应用程序在Lollipop上运行时中止。
ObjectAnimator translationZ = ObjectAnimator.ofFloat(fab, View.TRANSLATION_Z, from, to);
ObjectAnimator translationZBack = ObjectAnimator.ofFloat(fab, View.TRANSLATION_Z, to, from);
PathInterpolator pathInterpolator = new PathInterpolator(path);
如您所知,这些功能是在API 21中引入的,并且不适用于早期的API,这就是为什么您会看到这些错误。但是,您可以使用PathInterpolatorCompat从支持库获取路径插值器。
用于创建基于路径的
[Interpolator](https://developer.android.com/reference/android/view/animation/Interpolator.html)
实例的助手。在API 21或更高版本上,将使用平台实现,而在旧平台上,将使用兼容的替代实现。
我认为您不需要“ z”翻译的解决方案。(我真的看不到它与没有它之间的任何区别,但是那可能只是我。无论如何,FAB已经对高度效果产生了阴影。)
这是对animFab()
21之前的API 所做的一些更改,以确保可以PathInterpolatorCompat
从v4支持库中获取。首先是一个视频,显示在API 19模拟器上运行的代码:
public void animFab() {
ObjectAnimator scaleX = ObjectAnimator.ofFloat(fab, View.SCALE_X, from, to);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(fab, View.SCALE_Y, from, to);
AnimatorSet set1 = new AnimatorSet();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ObjectAnimator translationZ = ObjectAnimator.ofFloat(fab, View.TRANSLATION_Z, from, to);
set1.playTogether(scaleX, scaleY, translationZ);
} else {
set1.playTogether(scaleX, scaleY);
}
set1.setDuration(500);
set1.setInterpolator(new AccelerateInterpolator());
set1.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
}
});
Path path = new Path();
path.moveTo(0.0f, 0.0f);
path.lineTo(0.5f, 1.3f);
path.lineTo(0.75f, 0.8f);
path.lineTo(1.0f, 1.0f);
Interpolator pathInterpolator = PathInterpolatorCompat.create(path);
AnimatorSet set2 = new AnimatorSet();
ObjectAnimator scaleXBack = ObjectAnimator.ofFloat(fab, View.SCALE_X, to, from);
ObjectAnimator scaleYBack = ObjectAnimator.ofFloat(fab, View.SCALE_Y, to, from);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ObjectAnimator translationZBack = ObjectAnimator.ofFloat(fab, View.TRANSLATION_Z, to, from);
set2.playTogether(scaleXBack, scaleYBack, translationZBack);
} else {
set2.playTogether(scaleXBack, scaleYBack);
}
set2.setDuration(500);
set2.setInterpolator(pathInterpolator);
final AnimatorSet set = new AnimatorSet();
set.playSequentially(set1, set2);
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
set.start();
}
});
set.start();
}
另一种可能性是将AnimatedVectorDrawableCompat用于可绘制的动画,但这将是一个完整的重写。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句