成功调用 onAnimationUpdate,但在 View::onDraw 内使用 ValueAnimator 显示没有变化

Successful call to onAnimationUpdate, but no change in display using ValueAnimator within View::onDraw

我正在尝试使用 ValueAnimator w/setAlpha 在 class 扩展 ViewOnDraw 函数中为圆圈的不透明度设置动画。我在 onAnimationUpdate 中放了一个调试 Toast。快速连续的 toast 气泡显示 alpha 值(点击 onAnimatetionUpdate 并具有正确的值),但屏幕上的形状没有改变。我在 onAnimationUpdate() 内尝试了 invalidate()postInvalidate(),但认为这是一个转移注意力的问题。

来自 https://developer.android.com/guide/topics/graphics/prop-animation

Depending on what property or object you are animating, you might need to call the invalidate() ... setAlpha() and setTranslationX() invalidate the View properly, so you do not need to invalidate the View when calling these methods with new values.

示例代码可能如下所示:

    Paint CircleFill_dah = new Paint(); // hit of dah duration
    CircleFill_dah.setStyle(Paint.Style.FILL);

    ValueAnimator animator_dah = ValueAnimator.ofInt(0,255);

    CircleFill_dah.setColor(dahColor);
    CircleFill_dah.setAlpha(0);
    animator_dah.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            int alphval = (int) animation.getAnimatedValue();
            CircleFill_dah.setAlpha(alphval);
            //we are here but are not redrawing!?
            Toast.makeText(c,"setting alpha " + alphval,Toast.LENGTH_SHORT).show();
            //view.postInvalidate(); // clears toast messages, but doesn't show animation
        }
    });
    canvas.drawCircle(50, 50, 50, CircleFill_dah);
    animator_dah.setDuration(100);
    animator_dah.setRepeatMode(ValueAnimator.REVERSE);
    animator_dah.setRepeatCount(-1);
    animator_dah.start();

实际代码是on githubclearView extends View 并实现 onDraw,调用 Touchable.draw(),后者使用 Touchable.fill()

动画的 start() 不应该在 onDraw()!

invalidate() 似乎也是必要的。

public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View v = new TestView(this);
        setContentView(v);
    }

    private static class TestView extends View {
        Paint CircleFill_dah = new Paint(); // hit of dah duration
        ValueAnimator animator_dah = ValueAnimator.ofInt(100,255);

        public TestView (Context context) {
            super(context);
            setFocusable(true);

            CircleFill_dah.setStyle(Paint.Style.FILL);

            CircleFill_dah.setColor(Color.BLUE);
            CircleFill_dah.setAlpha(50);
            animator_dah.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int alphval = (int) animation.getAnimatedValue();
                    CircleFill_dah.setAlpha(alphval);
                    invalidate();
                }
            });

            animator_dah.setDuration(100);
            animator_dah.setRepeatMode(ValueAnimator.REVERSE);
            animator_dah.setRepeatCount(-1);
            animator_dah.start();

        }
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawCircle(50, 50, 50, CircleFill_dah);
        }
    }
}