【问题标题】:How to Animate a Fab button (zoom in/out) continuously?如何连续动画 Fab 按钮(放大/缩小)?
【发布时间】:2019-05-03 19:23:02
【问题描述】:

我想在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 设备上不工作

以下是我尝试过的一些链接

谁能帮忙解决奇巧设备中的问题

如果需要更多信息,请告诉我。提前致谢。您的努力将不胜感激。

【问题讨论】:

  • 也许这个库会帮助你github.com/beworker/…
  • “不在 KitKat 上工作”究竟是什么意思?
  • @azizbekian 应用程序崩溃,因为在 api 级别 21 中添加了 PathInterpolatorObjectAnimator.ofFloat(fab, View.TRANSLATION_Z, to, from)

标签: java android android-layout android-animation floating-action-button


【解决方案1】:

您看到“Field requires 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。 确保从 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 用于可绘制动画,但这将是完全重写。

【讨论】:

  • 嘿@Cheticamp 它的工作就像一个魅力非常感谢你并感谢你的解释,如果可能的话,你可以使用``AnimatedVectorDrawableCompat` 提供任何示例代码
  • @悟空没问题。至于AnimatedVectorDrawableCompat,我没用过,所以没有示例代码分享。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 2017-03-09
  • 1970-01-01
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多