Android 制作弹跳动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23937748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make bounce animation
提问by 5er
I would like to do bounce animation of layer.
我想做层的反弹动画。
I have done that layer comes from right to center, now I would like to move it a little back and then back to center. That would create bounce effect.
我已经完成了从右到中心的图层,现在我想将它向后移动一点,然后再回到中心。那会产生反弹效果。
I was thinking that I can do it with a translate like this:
我想我可以用这样的翻译来做到这一点:
<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" />
Well this code does not working, only thing I can achieve is that Layer comes from left to center, and then animation stops.
好吧,这段代码不起作用,我唯一能做到的是图层从左到中,然后动画停止。
I cannot use this code: because it does not achieve what I want
我不能使用此代码:因为它没有实现我想要的
setInterpolator(AnimationUtils.loadInterpolator(this,
android.R.anim.bounce_interpolator));
Any help would be appreciated.
任何帮助,将不胜感激。
回答by Steve Benett
You can use the BounceInterpolatorto have this effect. The docscontain a very good description how to use it in XML. Just have a animation xml like this:
您可以使用BounceInterpolator来实现这种效果。该文档包含一个很好的说明了如何使用它的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>
回答by Gagan Deep
use this xml
使用这个 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 will show bounce effect with scale ,different from translate,(fits better in some situations),for more check THISout..
这将显示带有比例的反弹效果,与翻译不同,(在某些情况下更适合),更多检查这个..
回答by Pratibha Sarode
Add code on button or image click
在按钮或图像单击上添加代码
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);
Add this class
添加这个类
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);
}
}