Android 比例动画-反向发行
Android Scale Animation - Reverse Issue
我成功地为我的图像添加了一个比例动画,使它从原来的尺寸增长到更大的尺寸。但是,我需要添加另一个动画代码副本,使其缩小到原始大小。我正在尝试 在布尔值为真时循环播放动画。
我试了一下这些参数,但我无法让它发挥作用。到目前为止,这是我的代码:
class AnimateButton extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Boolean isGlowing = true; //Make it run forever
while (isGlowing) {
scal_grow = new ScaleAnimation(0, 1.2f, 0, 1.2f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
scal_grow.setDuration(1500);
scal_grow.setFillAfter(true);
runOnUiThread(new Runnable() {
@Override
public void run() {
btn_layer.setAnimation(scal_grow);
}
});
try {
Thread.sleep(1500);
} catch (Exception e) { }
//Add a reverse animation such that it goes back to the original size
}
return null;
}
}
伙计们,我应该做出哪些改变?
要取消缩放,你可以使用这个:
ScaleAnimation unscal_grow = new ScaleAnimation(1.2f, 1.0f, 1.2f, 1.0f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
unscal_grow .setDuration(1500);
unscal_grow .setFillAfter(true);
缩放和取消缩放的方法:
private void scaleAndUnscale() {
ScaleAnimation scal_grow = new ScaleAnimation(0, 1.2f, 0, 1.2f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
scal_grow.setDuration(1500);
scal_grow.setFillAfter(true);
image1.startAnimation(scal_grow);
scal_grow.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
ScaleAnimation unscal_grow = new ScaleAnimation(1.2f, 1.0f, 1.2f, 1.0f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
unscal_grow.setDuration(1500);
unscal_grow.setStartOffset(1500);
unscal_grow.setFillAfter(true);
image1.startAnimation(unscal_grow);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
我绝对建议不要使用遗留的 android.view.animation
动画,而是使用 Property Animations。旧的视图动画不是很好,并且不能像您期望的那样在视图上工作。
使用属性动画,这个脉冲动画非常简单。您可以像这样在 XML 中定义它:
<animator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1500"
android:valueFrom="1"
android:valueTo="1.2"
android:valueType="floatType"
android:repeatCount="infinite"
android:repeatMode="reverse"/>
当您应用它时,动画将通过反向无限重复。
在 Java 中看起来像这样:
PropertyValuesHolder scalex = PropertyValuesHolder.ofFloat(View.SCALE_X, 1.2f);
PropertyValuesHolder scaley = PropertyValuesHolder.ofFloat(View.SCALE_Y, 1.2f);
ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(btn_layer, scalex, scaley);
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.setDuration(1500);
anim.start();
这将为您带来更令人期待的体验,并为您处理线程和时间安排。当您想停止动画时,只需调用 s.cancel()
就可以了。
在android中,除UI线程(主线程)外,任何其他线程都不会发生动画和UI更新。
删除 AsyncTask 并尝试使用在性能方面优于 ScaleAnimation 的 ViewPropertyAnimator。另外,它只是一行。
按比例缩放:
btn_layer.animate().scaleX(1.2f).scaleY(1.2f).setDuration(1500).start();
取消缩放:
btn_layer.animate().scaleX(0.8f).scaleY(0.8f).setDuration(1500).start();
更新
PropertyValuesHolder scalex = PropertyValuesHolder.ofFloat(View.SCALE_X, 1.2f);
PropertyValuesHolder scaley = PropertyValuesHolder.ofFloat(View.SCALE_Y, 1.2f);
ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(btn_layer, scalex, scaley);
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.setDuration(1500);
anim.start();
我成功地为我的图像添加了一个比例动画,使它从原来的尺寸增长到更大的尺寸。但是,我需要添加另一个动画代码副本,使其缩小到原始大小。我正在尝试 在布尔值为真时循环播放动画。
我试了一下这些参数,但我无法让它发挥作用。到目前为止,这是我的代码:
class AnimateButton extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Boolean isGlowing = true; //Make it run forever
while (isGlowing) {
scal_grow = new ScaleAnimation(0, 1.2f, 0, 1.2f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
scal_grow.setDuration(1500);
scal_grow.setFillAfter(true);
runOnUiThread(new Runnable() {
@Override
public void run() {
btn_layer.setAnimation(scal_grow);
}
});
try {
Thread.sleep(1500);
} catch (Exception e) { }
//Add a reverse animation such that it goes back to the original size
}
return null;
}
}
伙计们,我应该做出哪些改变?
要取消缩放,你可以使用这个:
ScaleAnimation unscal_grow = new ScaleAnimation(1.2f, 1.0f, 1.2f, 1.0f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
unscal_grow .setDuration(1500);
unscal_grow .setFillAfter(true);
缩放和取消缩放的方法:
private void scaleAndUnscale() {
ScaleAnimation scal_grow = new ScaleAnimation(0, 1.2f, 0, 1.2f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
scal_grow.setDuration(1500);
scal_grow.setFillAfter(true);
image1.startAnimation(scal_grow);
scal_grow.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
ScaleAnimation unscal_grow = new ScaleAnimation(1.2f, 1.0f, 1.2f, 1.0f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
unscal_grow.setDuration(1500);
unscal_grow.setStartOffset(1500);
unscal_grow.setFillAfter(true);
image1.startAnimation(unscal_grow);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
我绝对建议不要使用遗留的 android.view.animation
动画,而是使用 Property Animations。旧的视图动画不是很好,并且不能像您期望的那样在视图上工作。
使用属性动画,这个脉冲动画非常简单。您可以像这样在 XML 中定义它:
<animator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1500"
android:valueFrom="1"
android:valueTo="1.2"
android:valueType="floatType"
android:repeatCount="infinite"
android:repeatMode="reverse"/>
当您应用它时,动画将通过反向无限重复。
在 Java 中看起来像这样:
PropertyValuesHolder scalex = PropertyValuesHolder.ofFloat(View.SCALE_X, 1.2f);
PropertyValuesHolder scaley = PropertyValuesHolder.ofFloat(View.SCALE_Y, 1.2f);
ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(btn_layer, scalex, scaley);
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.setDuration(1500);
anim.start();
这将为您带来更令人期待的体验,并为您处理线程和时间安排。当您想停止动画时,只需调用 s.cancel()
就可以了。
在android中,除UI线程(主线程)外,任何其他线程都不会发生动画和UI更新。
删除 AsyncTask 并尝试使用在性能方面优于 ScaleAnimation 的 ViewPropertyAnimator。另外,它只是一行。
按比例缩放:
btn_layer.animate().scaleX(1.2f).scaleY(1.2f).setDuration(1500).start();
取消缩放:
btn_layer.animate().scaleX(0.8f).scaleY(0.8f).setDuration(1500).start();
更新
PropertyValuesHolder scalex = PropertyValuesHolder.ofFloat(View.SCALE_X, 1.2f);
PropertyValuesHolder scaley = PropertyValuesHolder.ofFloat(View.SCALE_Y, 1.2f);
ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(btn_layer, scalex, scaley);
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.setDuration(1500);
anim.start();