[英]How to Animate a Fab button (zoom in/out) continuously?
我想在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()
的重做, animFab()
進行了一些更改以適應21之前的API。 請確保從v4支持庫中PathInterpolatorCompat
。 首先是一個視頻,顯示在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 ,但這將是一個完整的重寫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.