Android的进度条(自定义视图)值的动画不工作 - 动画对象在查看后无效 [英] Android Progressbar (custom view) value animation not working - Object Animator in View after invalidate

查看:297
本文介绍了Android的进度条(自定义视图)值的动画不工作 - 动画对象在查看后无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要动画进度条值改变,而属性,都会改变,我的自定义视图在下面给出

I need to animate progress bar value changed while valueproperty gets changed, my custom view is given below,

public class ProgressBar extends View {

    public ProgressBar(Context context) {
        this(context, null);
    }

    public ProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    ObjectAnimator animator;
    double value = 50;

    public double getValue() {
        return value;
    }

    public void setTargetValue(double targetValue) {
        animator = ObjectAnimator.ofFloat(this, "value", (float) this.value,(float) targetValue);
        animator.setDuration(1500);
        animator.start();
        this.value = targetValue;
    }
    public void setValue(double tempValue) {
        setTargetValue(tempValue);
        this.invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint =  new Paint();
        paint.setStrokeWidth(3);
        RectF borderRectF = new RectF(50,100,400,200);
        RectF backgroundRectF = new RectF(53,103,397,197);
        RectF filledRectF = new RectF(53,103,53,197);
        paint.setColor(Color.LTGRAY);
        canvas.drawRect(borderRectF, paint);
        paint.setStrokeWidth(0);
        paint.setColor(Color.WHITE);
        canvas.drawRect(backgroundRectF, paint);
        paint.setColor(Color.BLUE);
        filledRectF = getfilledRect();
        canvas.drawRect(filledRectF, paint);
    }

   private RectF getfilledRect(){
       float filledValue = (float)(53+(3.44 * this.value));
       filledValue = Math.min(filledValue,397);
        return new RectF(53,103,filledValue,197);
    }
}

但动画不能正常工作,是我想念的​​东西,或者我可以做不同的看法?

but animation is not working, am i miss something, or can i do it differently?

推荐答案

您需要两个函数,而不是这一个。一功能需用适合自己的值的新的目标被调用,以及其它功能应当用于实现沿途各个步骤。使用ObjectAnimator中的第一个,它会调用你的第二个功能很多次,每次递增一步。像这样的:

You need two functions instead of this one. One function should be called with the new target for your value, and the other function should be used to implement each step along the way. Use ObjectAnimator in the first one, and it will call your second function many times for each incremental step. Like this:

public void setProgress(float progress) {
    animator = ObjectAnimator.ofFloat(this, "value", this.value, progress);
    animator.setDuration(1500);
    animator.start();
}

public void setValue(float value) {
     this.value = value;
     invalidate();
}

private RectF getfilledRect() {
   float filledValue = 53f + (3.44f * this.value);
   filledValue = Math.min(filledValue, 397f);
   return new RectF(53f, 103f, filledValue, 197f);
}

的几个注意事项:

A few notes:


  • 您不需要称之为setTarget(本),因为你已经设置这个为目标,在第一个参数ofFloat。

  • 您可能要延迟设置你的领域的价值,直到动画完成。您可以使用AnimationListener要做到这一点,覆盖onAnimationEnd。正因为如此,该领域将UI重新presents这一点。
  • 前被设置为新的目标值
  • 的的setValue的签名必须与所示,因为那是ObjectAnimator.ofFloat如何工作的:它看起来命名的属性,需要一个浮子和返回void的二传手

  • You don't need to call "setTarget(this)" because you already set this as the target, in the first parameter to ofFloat.
  • You might want to delay setting your field "value" until the animation is complete. You can use an AnimationListener to do this, overriding onAnimationEnd. As it is, the field will be set to the new target value before the UI represents this.
  • The signature of "setValue" must be exactly as shown, because that is how ObjectAnimator.ofFloat works: it looks for a setter of the named property that takes a float and returns void.

修改

嗯,我明白了,你让你自己的进度条。在这种情况下,你有权要求无效,因为你是压倒一切的onDraw。我修改我的回答以上,改变setTargetValue到setProgress。这是一个应该只从外部该类调用的函数 - 谁知道进展如何。你不想setProgress打电话的setValue,反之亦然。

Ah, I see, you're making your own progress bar. In that case, you were right to call invalidate, since you are overriding onDraw. I've modified my answer above, changing "setTargetValue" to "setProgress". This is a function that should only be called from OUTSIDE this class -- whoever knows what the progress is. You do NOT want setProgress to call setValue, or vice versa.

新说明:


  • 您应该只使用浮点值无处不在,不是一倍,因为他们最终会在RectF反正使用。如果你一个数字后加F,那么Java跨$ P $点作为一个浮动,而不是双重的,你不必做你的方程式铸造。

  • 既然你重写的onDraw,你中间的setValue功能只需要设置你的领域的价值,并失去进度条。

这篇关于Android的进度条(自定义视图)值的动画不工作 - 动画对象在查看后无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆