如何连续制作Fab按钮(放大缩小)动画?

如何连续制作Fab按钮(放大缩小)动画?,第1张

概述我想在FloatingActionButton上创建这种类型的动画我试过了什么publicvoidanimFab(){ObjectAnimatorscaleX=ObjectAnimator.ofFloat(fab,View.SCALE_X,from,to);ObjectAnimatorscaleY=ObjectAnimator.ofFloat(fab,View.SCALE_Y,from,to);ObjectA

我想在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();}

The Problem

上面的代码工作正常Lolipop及以上设备,但不适用于KitKat设备

Below are some links I have trIEd

> Animating Fab on click (zoom in/out)
> ObjectAnimator starting with a frame jump on Android 4.4 (nexus 5) but not in 4.1 device
> Implementing ImageView transition between activities for pre-Lollipop devices.
> How to achieve Transition animation in Pre lollipop devices
> Material Transitions in pre lollipop apps
> Android ImageView Zoom-in and Zoom-out Continuously

任何人都可以帮助解决KitKat设备中的问题

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

解决方法:

您看到“FIEld需要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从支持库中获取路径插补器.

Helper for creating path-based [Interpolator](https://developer.androID.com/reference/androID/vIEw/animation/Interpolator.HTML) instances. On API 21 or newer, the platform implementation will be used and on older platforms a compatible alternative implementation will be used.

我认为您不需要“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用于可绘制动画,但这将是完全重写.

总结

以上是内存溢出为你收集整理的如何连续制作Fab按钮(放大/缩小)动画?全部内容,希望文章能够帮你解决如何连续制作Fab按钮(放大/缩小)动画?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1099182.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存