更新时间:2023-01-15 20:51:48
您正在呼叫 drawRect
两次(在使视图无效之前,以及在 onDraw
上)。另外,也无需存储对 Canvas
的引用。
You're calling to drawRect
twice (before invalidating the view, and on onDraw
). Also, there's no need to store a reference to Canvas
.
将所需的颜色保留在变量中,对其进行更改并使视图无效。-
Keep the desired color in a variable, change it and invalidate the view.-
public class myView extends View {
private Color color = Color.RED;
Rect rects = new Rect(30,30,80,80);
@Override
public void onDraw(Canvas canvas) {
paint.setColor(color);
canvas.drawRect(rects, paint);
}
void changeColor() {
color = Color.BLUE
invalidate();
}
}