【发布时间】:2017-07-23 15:57:42
【问题描述】:
我有两件事。
第一个是循环缩放动画,执行一种永久放大/缩小。
第二件事是TimerTask,它设置此缩放动画的持续时间每 20 秒。
问题是,当setDuration() 出现时,有时动画中会出现一种“跳跃”。
首先我将这个setDuration() 放在TimerTask 中,然后我只是尝试在TimerTask 中放置一个标志并更改onAnimationEnd() 中的持续时间,它也不起作用,同样的问题。在下面的代码中,我使用了这个标志技术。
如果还不够清楚,所有这些的目标是“无限”放大/缩小可绘制的圆圈,放大/缩小速度会随着时间的推移而降低。它确实有效,但并不顺利,如上所述。
有没有办法顺利做到这一点?
设置标志“changeDurationFlag”的 TimeTask
private void setRegularRythmeDecrease() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
if (elapsedTime > sessionLengthInSec) {
circle.clearAnimation();
}
zoomDuration = zoomDuration + (toDecreaseEveryUpdate / 2);
changeDurationFlag = true;
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
timer.schedule(task, 0, BREATH_RYTHME_UPDATE_INTERVAL_IN_SECONDS*1000);
}
我用来放大和缩小的 ScaleAnimation
public Animation scaleAnimation(View v, float startScale, float endScale, long duration) {
Animation anim = new ScaleAnimation(
startScale, endScale,
startScale, endScale,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
anim.setFillAfter(true);
anim.setDuration(duration);
return anim;
}
设置持续时间的动画监听器
zoomDuration = ZOOM_DURATION_START;
animZoomIn = scaleAnimation(circle, 1f, ZOOM_FACTOR,zoomDuration);
animZoomOut = scaleAnimation(circle, ZOOM_FACTOR, 1f,zoomDuration);
animZoomIn.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// If the flag is true (modified in the TimerTask) I set the Duration to decrease the speed
// it's where the not smoothly thing happens
if(changeDurationFlag) {
Log.d("beat ","Set breath to " + String.valueOf(zoomDuration * 2d));
animZoomIn.setDuration(zoomDuration);
animZoomOut.setDuration(zoomDuration);
changeDurationFlag = false;
}
circle.startAnimation(animZoomOut);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
animZoomOut.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
circle.startAnimation(animZoomIn);
currentDateTime = Calendar.getInstance().getTime();
elapsedTime = currentDateTime.getTime() - startDateTime.getTime();
long elapsedTimeInSeconds = TimeUnit.MILLISECONDS.toSeconds(elapsedTime);
beatCount++;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
【问题讨论】:
标签: android animation zooming timertask