已成功调用onAnimationUpdate,但使用View :: onDraw

时间:2018-10-29 12:26:49

标签: java android android-animation

我正在尝试使用OnDraw w / View在扩展ValueAnimator的类的setAlpha函数中为圆的不透明度设置动画。我已经在onAnimationUpdate中放入了调试Toast。快速连续的吐司气泡显示出Alpha值(onAnimatetionUpdate被点击并具有正确的值),但是形状在屏幕上没有改变。我在invalidate()中尝试过postInvalidate()onAnimationUpdate(),但认为这是一条红鲱鱼。

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

  

根据要设置动画的属性或对象,   您可能需要调用invalidate()...   setAlpha()和setTranslationX()会使View无效,   因此,使用新值调用这些方法时,无需使View无效。

示例代码如下:

    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()

1 个答案:

答案 0 :(得分:1)

动画的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);
        }
    }
}