如何连续制作Fab按钮的动画(放大/缩小)? [英] How to Animate a Fab button (zoom in/out) continuously?

查看:67
本文介绍了如何连续制作Fab按钮的动画(放大/缩小)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The Above code is working fine Lolipop and above device but not working in KitKat device

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

Below are some links I have tried

  • 点击动画Fab(放大/缩小)
  • 实现ImageView棒棒糖之前设备的活动之间的转换.
  • 如何在Pre Lollipop设备中实现转场动画
  • 棒棒糖应用前的材料转换
  • Android ImageView连续放大和缩小
    • 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设备中的问题

      Can anyone help to solve the problem in KitKat device

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

      If need more information please do let me know. Thanks in advance. Your efforts will be appreciated.

      推荐答案

      您在以下代码行中看到字段需要API 21" Studio lint错误,这些代码在应用程序在Lollipop上运行时会中止.

      You are seeing "Field requires API 21" Studio lint errors for the following lines of code the app aborts when running on 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 .

      As you know, these features were introduced in API 21 and not available to earlier APIs and that's why you are seeing these errors. You can, however, get a path interpolator from the support library using PathInterpolatorCompat.

      用于创建基于路径的[Interpolator](https://developer.android.com/reference/android/view/animation/Interpolator.html)实例的助手.在API 21或更高版本上,将使用平台实现,而在旧平台上,将使用兼容的替代实现.

      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已经对高度效果有阴影了.)

      I don't think that you will need a solution for the "z" translation. (I really don't see any difference between having it and not, but that could be just me. Anyway, the FAB already has a shadow for the height effect.)

      这是animFab()的重做,做了一些更改以适应21之前的API. 确保从v4支持库中获取PathInterpolatorCompat.首先是一段视频,显示在API 19仿真器上工作的代码:

      Here is a rework of animFab() with some changes to accommodate APIs before 21. Make sure you snag the PathInterpolatorCompat from the v4 support library. First a video showing the code working on an API 19 emulator:

      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 动画,但这将是一个完整的重写.

      Another possibility is to use AnimatedVectorDrawableCompat for the drawable animation, but that would be a complete rewrite.

      这篇关于如何连续制作Fab按钮的动画(放大/缩小)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆