【发布时间】:2019-04-20 07:48:57
【问题描述】:
如何在 Android Studio 中实现按钮的弹跳效果。所以当你推动它时它会减少和增加。在发布此问题之前,我进行了多次搜索,但没有得到任何结果。
【问题讨论】:
标签: java android spring android-studio button
如何在 Android Studio 中实现按钮的弹跳效果。所以当你推动它时它会减少和增加。在发布此问题之前,我进行了多次搜索,但没有得到任何结果。
【问题讨论】:
标签: java android spring android-studio button
首先,创建一个动画类型的资源文件。然后在xml里面添加如下动画(spring.xml):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="2500"
android:fromXScale="0.5"
android:toXScale="1.0"
android:fromYScale="0.5"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"/>
</set>
这将使按钮在 2.5 秒内从半尺寸变为全尺寸。如果你想要一个弹簧效果,你需要一个助手类。对于我的许多程序,我使用了以下内容:
class MyInterpolator implements android.view.animation.Interpolator {
private double mAmplitude = 1;
private double mFrequency = 10;
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);
}
}
如果您想以编程方式设置动画,请在按钮上使用以下方法:
public void springView(View view) {
Button button = (Button)findViewById(R.id.button);
final Animation myAnimation = AnimationUtils.loadAnimation(this, R.anim.spring);
MyInterpolator interpolator = new MyInterpolator(0.1, 10);
myAnimation.setInterpolator(interpolator);
button.startAnimation(myAnimation);
}
根据你想要那个按钮的弹性,增加新的 MyInterpolator(0.1, 10) 中的值。
【讨论】: