[英]Android Scale Animation - Reverse Issue
我設法為我的Image添加了一個縮放動畫,使它從原始大小增長到更大的大小。 但是我需要添加另一個動畫代碼副本,使其縮小到原始大小。 我試圖在布爾值為true時循環播放動畫。
我玩了一些參數,但我無法使它工作。 到目前為止,這是我的代碼:
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;
}
}
我應該做些什么改變?
在android中,除了UIThread(主線程)之外的任何其他線程都不會發生動畫和UI更新。
刪除AsyncTask並嘗試使用ViewPropertyAnimator,它在性能方面優於ScaleAnimation。 另外,它只是一條線。
縮放:
btn_layer.animate().scaleX(1.2f).scaleY(1.2f).setDuration(1500).start();
不規模:
btn_layer.animate().scaleX(0.8f).scaleY(0.8f).setDuration(1500).start();
UPDATE
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();
我肯定會建議不要使用傳統的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()
就可以了。
要取消刻度,您可以使用此:
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) {
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.