我正在尝试使用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 github。 clearView extends View
并实现onDraw
,调用使用Touchable.draw()
Touchable.fill()
答案 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);
}
}
}