简体   繁体   English

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

[英]How to Animate a Fab button (zoom in/out) continuously?

I Want to create this type of animation on FloatingActionButton 我想在FloatingActionButton上创建这种类型的动画

在此处输入图片说明

What I have tried 我尝试过的

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 问题

The Above code is working fine Lolipop and above device but not working in KitKat device 上面的代码在Lolipop及以上设备上正常工作,但在KitKat设备上不工作

Below are some links I have tried 以下是我尝试过的一些链接

Can anyone help to solve the problem in KitKat device 任何人都可以帮助解决KitKat设备中的问题

If need more information please do let me know. 如果需要更多信息,请告诉我。 Thanks in advance. 提前致谢。 Your efforts will be appreciated. 您的努力将不胜感激。

You are seeing "Field requires API 21" Studio lint errors for the following lines of code the app aborts when running on Lollipop. 您会看到以下字段的“字段需要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);

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. 如您所知,这些功能是在API 21中引入的,不适用于早期的API,因此您会看到这些错误。 You can, however, get a path interpolator from the support library using PathInterpolatorCompat . 但是,您可以使用PathInterpolatorCompat从支持库获取路径插值

Helper for creating path-based [Interpolator](https://developer.android.com/reference/android/view/animation/Interpolator.html) instances. 用于创建基于路径的[Interpolator](https://developer.android.com/reference/android/view/animation/Interpolator.html)实例的助手。 On API 21 or newer, the platform implementation will be used and on older platforms a compatible alternative implementation will be used. 在API 21或更高版本上,将使用平台实现,而在较旧平台上,将使用兼容的替代实现。

I don't think that you will need a solution for the "z" translation. 我认为您不需要“ z”翻译的解决方案。 (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.) (我真的没有和没有看到任何区别,但这可能只是我。无论如何,FAB已经对高度效果产生了阴影。)

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. 这是animFab()的重做, animFab()进行了一些更改以适应21之前的API。 请确保从v4支持库中PathInterpolatorCompat First a video showing the code working on an API 19 emulator: 首先是一个视频,显示在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();  
}

Another possibility is to use AnimatedVectorDrawableCompat for the drawable animation, but that would be a complete rewrite. 另一种可能性是对可绘制的动画使用AnimatedVectorDrawableCompat ,但这将是一个完整的重写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM