Android 中的弹跳动画
Bouncing Animation in Android
我想创建如下动画;
我使用了以下代码:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator" >
<scale
android:duration="10000"
android:fromXScale="1"
android:fromYScale="0.5"
/>
</set>
但无法锻炼!任何帮助都将不胜感激。
谢谢
在 Res/anim 中创建此 bounce_animation.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:fromXScale="1.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0"
android:duration="600" />
</set>
然后在您的 Activity class 中,将动画设置为要绑定到的元素。比如用户在TextView中点击,会弹跳
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bounceAnimation = AnimationUtils.loadAnimation(this, R.anim.bounce_animation)
val textViewToBounce = findViewById(R.id.textViewToBounce)
textViewToBounce.setOnClickListener {
textViewToBounce.startAnimation(bounceAnimation)
}
}
试试下面的代码
此代码中不需要任何 xml 文件来实现您想要的弹跳动画。
- 向上弹跳动画
doBounceAnimation(yourView);
private void doBounceAnimation(View targetView) {
Interpolator interpolator = new Interpolator() {
@Override
public float getInterpolation(float v) {
return getPowOut(v,2);//Add getPowOut(v,3); for more up animation
}
};
ObjectAnimator animator = ObjectAnimator.ofFloat(targetView, "translationY", 0, 25, 0);
animator.setInterpolator(interpolator);
animator.setStartDelay(200);
animator.setDuration(800);
animator.setRepeatCount(5);
animator.start();
}
private float getPowOut(float elapsedTimeRate, double pow) {
return (float) ((float) 1 - Math.pow(1 - elapsedTimeRate, pow));
}
- 向下弹跳动画
doBounceAnimation(yourView);
private void doBounceAnimation(View targetView) {
Interpolator interpolator = new Interpolator() {
@Override
public float getInterpolation(float v) {
return getPowIn(v,2);//Add getPowIn(v,3); for more down animation
}
};
ObjectAnimator animator = ObjectAnimator.ofFloat(targetView, "translationY", 0, 25, 0);
animator.setInterpolator(interpolator);
animator.setStartDelay(200);
animator.setDuration(800);
animator.setRepeatCount(5);
animator.start();
}
private float getPowIn(float elapsedTimeRate, double pow) {
return (float) Math.pow(elapsedTimeRate, pow);
}
希望对您有所帮助!
谢谢。
我想创建如下动画;
我使用了以下代码:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator" >
<scale
android:duration="10000"
android:fromXScale="1"
android:fromYScale="0.5"
/>
</set>
但无法锻炼!任何帮助都将不胜感激。
谢谢
在 Res/anim 中创建此 bounce_animation.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:fromXScale="1.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0"
android:duration="600" />
</set>
然后在您的 Activity class 中,将动画设置为要绑定到的元素。比如用户在TextView中点击,会弹跳
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bounceAnimation = AnimationUtils.loadAnimation(this, R.anim.bounce_animation)
val textViewToBounce = findViewById(R.id.textViewToBounce)
textViewToBounce.setOnClickListener {
textViewToBounce.startAnimation(bounceAnimation)
}
}
试试下面的代码
此代码中不需要任何 xml 文件来实现您想要的弹跳动画。
- 向上弹跳动画
doBounceAnimation(yourView);
private void doBounceAnimation(View targetView) {
Interpolator interpolator = new Interpolator() {
@Override
public float getInterpolation(float v) {
return getPowOut(v,2);//Add getPowOut(v,3); for more up animation
}
};
ObjectAnimator animator = ObjectAnimator.ofFloat(targetView, "translationY", 0, 25, 0);
animator.setInterpolator(interpolator);
animator.setStartDelay(200);
animator.setDuration(800);
animator.setRepeatCount(5);
animator.start();
}
private float getPowOut(float elapsedTimeRate, double pow) {
return (float) ((float) 1 - Math.pow(1 - elapsedTimeRate, pow));
}
- 向下弹跳动画
doBounceAnimation(yourView);
private void doBounceAnimation(View targetView) {
Interpolator interpolator = new Interpolator() {
@Override
public float getInterpolation(float v) {
return getPowIn(v,2);//Add getPowIn(v,3); for more down animation
}
};
ObjectAnimator animator = ObjectAnimator.ofFloat(targetView, "translationY", 0, 25, 0);
animator.setInterpolator(interpolator);
animator.setStartDelay(200);
animator.setDuration(800);
animator.setRepeatCount(5);
animator.start();
}
private float getPowIn(float elapsedTimeRate, double pow) {
return (float) Math.pow(elapsedTimeRate, pow);
}
希望对您有所帮助!
谢谢。