gpt4 book ai didi

java - 成功调用 onAnimationUpdate,但在 View::onDraw 中使用 ValueAnimator 时显示没有变化

转载 作者:行者123 更新时间:2023-12-02 10:39:20 26 4
gpt4 key购买 nike

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

来自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);
}
}
}

关于java - 成功调用 onAnimationUpdate,但在 View::onDraw 中使用 ValueAnimator 时显示没有变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53045497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com