【问题标题】:Android ObjectAnimator inside custom view自定义视图中的 Android ObjectAnimator
【发布时间】:2015-05-03 21:01:36
【问题描述】:
我有一个类似于进度条的自定义视图。在这个视图中,我有一个 ObjectAnimator.AnimatorUpdateListener,我试图用它来调用视图上的无效。但是,我的观点没有更新!我尝试添加一个按钮,该按钮只是将值更改为其他值并调用一次无效并且它起作用,我的视图更新以反映值更改。
我在这里遗漏了什么吗?我调用无效的次数太多了吗?
我的“进度条”从 0 处的浮点数开始,动画应该将其设置为 100。调用方法将其更新为 50 并调用无效,但 ObjectAnimator 似乎没有调用无效。
一切都在 UI 线程上调用
【问题讨论】:
标签:
android
animation
view
invalidation
【解决方案1】:
ObjectAnimator 不调用 invalidate() - 你的方法应该在需要时调用
ObjectAnimator oAnimator = ObjectAnimator.ofInt(view,"someproperty",0,100)
void setSomeProperty(value) {
mValue = value
invalidate()
}
【解决方案2】:
我用过这个,效果很好。
public interface ProgressAnimationListener {
public void onAnimationStart();
public void onAnimationFinish();
public void onAnimationProgress(int progress);
}
private ObjectAnimator progressBarAnimator;
public synchronized void animateProgressTo(final int start, final int end, final int duration, final ProgressAnimationListener listener) {
stopAnimation();
setProgress(start);
progressBarAnimator = ObjectAnimator.ofFloat(this, "animateProgress", start, end);
progressBarAnimator.setDuration(duration);
progressBarAnimator.setInterpolator(new LinearInterpolator());
progressBarAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationCancel(final Animator animation) {
}
@Override
public void onAnimationEnd(final Animator animation) {
setProgress(end);
if (listener != null) {
listener.onAnimationFinish();
}
}
@Override
public void onAnimationRepeat(final Animator animation) {
}
@Override
public void onAnimationStart(final Animator animation) {
if (listener != null) {
listener.onAnimationStart();
}
}
});
progressBarAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(final ValueAnimator animation) {
int progress = ((Float) animation.getAnimatedValue()).intValue();
if (progress != getProgress()) {
//Log.d(TAG, progress + "");
setProgress(progress);
if (listener != null) {
listener.onAnimationProgress(progress);
}
}
}
});
progressBarAnimator.start();
}
public synchronized boolean isAnimationRunning() {
return progressBarAnimator != null && progressBarAnimator.isRunning();
}
public synchronized void stopAnimation() {
if (isAnimationRunning()) {
progressBarAnimator.cancel();
progressBarAnimator = null;
}
}