【问题标题】:Android XML Pulse Animation not working as expectedAndroid XML 脉冲动画未按预期工作
【发布时间】:2018-06-04 01:05:05
【问题描述】:

我正在尝试编写一个模仿类似于这样的脉冲动画的动画:

专注于内部蓝色圆圈(忽略外部深蓝色圆圈)

http://www.joedubs.com/wp-content/uploads/2016/01/breathe-now.gif

这是我目前编写的代码:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <scale
        android:duration="2000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.8"
        android:toYScale="0.8" />

    <scale
        android:duration="2000"
        android:fromXScale="0.8"
        android:fromYScale="0.8"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="2000"
        android:toXScale="1.8"
        android:toYScale="1.8" />

    <scale
        android:duration="2000"
        android:fromXScale="1.8"
        android:fromYScale="1.8"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="2000"
        android:toXScale="1"
        android:toYScale="1" />
</set>

虽然这在一定程度上有效,但动画在再次重复时会跳过和卡顿。有人可以稍微调整一下以模仿平滑的脉冲动画吗? (增长和减少)

【问题讨论】:

    标签: android xml animation android-animation


    【解决方案1】:

    你可以像这样添加一个 ObjectAnimator,在反向模式下创建一个脉动效果:

    ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(ImageView,
                    PropertyValuesHolder.ofFloat("scaleX", 0.5f),
                    PropertyValuesHolder.ofFloat("scaleY", 0.5f));
            scaleDown.setDuration(300);
    
            scaleDown.setRepeatCount(ObjectAnimator.INFINITE);
            scaleDown.setRepeatMode(ObjectAnimator.REVERSE);
    
            scaleDown.start();
    

    另一种实现方法是拥有一个 CustomClass 并覆盖您的 OnDraw 方法,从而产生增加或减少增加变量并调用 invalidate() 的效果。我在另一篇文章中做了这些以使我的按钮背景变大,如果您想按照这种方式进行操作,它可能对您有用。

    Pulsating Button Android

    【讨论】:

      猜你喜欢
      • 2021-10-19
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多