【问题标题】:Make bounce animation制作弹跳动画
【发布时间】:2019-01-23 14:29:42
【问题描述】:

我想做图层的弹跳动画。

我已经完成了那个图层从右到中心,现在我想将它向后移动一点,然后回到中心。这会产生反弹效果。

我在想我可以用这样的翻译来做到这一点:

<translate
    android:duration="900"
    android:fromXDelta="100%p"
    android:toXDelta="0%p" />

<translate
    android:duration="900"
    android:fromXDelta="0%p"
    android:toXDelta="100%p" />

<translate
    android:duration="900"
    android:fromXDelta="70%p"
    android:toXDelta="0%p" />

好吧,这段代码不起作用,我唯一能做到的就是图层从左到中,然后动画停止。

我不能使用这个代码:因为它没有达到我想要的效果

setInterpolator(AnimationUtils.loadInterpolator(this,
                        android.R.anim.bounce_interpolator));

任何帮助将不胜感激。

【问题讨论】:

  • 您是否指定了动画的持续时间?

标签: android android-animation


【解决方案1】:

您可以使用BounceInterpolator 来实现此效果。 docs 包含一个很好的描述如何在 XML 中使用它。只要有一个像这样的动画xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android:anim/bounce_interpolator">

    <!-- Use your working translate animation here-->
    <translate
        android:duration="900"
        android:fromXDelta="100%p"
        android:toXDelta="0%p" />
</set>

【讨论】:

  • 谢谢,对于提供的解决方案,但它不是我想要的。这个动画一直在弹跳。我只需要“一次”反弹。
  • 然后创建自己的插值器,或者使用 repeat=1 + reverse 的加速-减速(看起来“相似”)
  • 我相信你想要的是一个过冲插值器@5er youtube.com/watch?v=6UL7PXdJ6-E
【解决方案2】:

使用这个xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:interpolator="@android:anim/bounce_interpolator">

    <scale
        android:duration="600"
        android:fromXScale="1.0"
        android:fromYScale="0.0"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>

这将显示带有比例的反弹效果,与翻译不同,(在某些情况下更适合),更多请查看THIS out..

【讨论】:

    【解决方案3】:

    在按钮或图片点击上添加代码

        final Animation myAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.bounce);
        // Use bounce interpolator with amplitude 0.1 and frequency 15
        MyBounceInterpolator interpolator = new MyBounceInterpolator(0.1, 15);
        myAnim.setInterpolator(interpolator);
        imgVoiceSearch.startAnimation(myAnim);
    

    添加这个类

    public class MyBounceInterpolator implements android.view.animation.Interpolator {
    private double mAmplitude = 1;
    private double mFrequency = 10;
    
    public MyBounceInterpolator(double amplitude, double frequency) {
        mAmplitude = amplitude;
        mFrequency = frequency;
    }
    
    public float getInterpolation(float time) {
        return (float) (-1 * Math.pow(Math.E, -time / mAmplitude) *
                Math.cos(mFrequency * time) + 1);
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      相关资源
      最近更新 更多