我现在拥有一个视图,在onCreate后以编程方式添加(取决于其他一些变量)。一切正常,它绘制了圆形的一部分。
但我的问题是如何在以后重新绘制它?在获取一些数据后,我需要改变圆中的角度。
风向玫瑰的代码:
public class WindRose extends View {
public WindRose(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvasTest = canvas;
float height = (float) getHeight();
float width = (float) getWidth();
float radius;
if (width > height) {
radius = height / 2;
} else {
radius = width / 2;
}
// radius = (height )/ 2;
Path path = new Path();
path.addCircle(width, height, radius, Path.Direction.CCW);
// / 2
Resources resources = getResources();
int color = resources.getColor(R.color.green_back);
paint.setColor(color);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.FILL);
float center_x, center_y;
center_x = width / 2;
center_y = height / 2;
//Formulas :
//SD = Start Degree
//ED = End Degree
//If cakepiece passes 0 (East)
//SD, 360-(SD+ED)
//Else :
//SD, (ED-SD)
oval.set(center_x - radius, center_y - radius, center_x + radius, center_y + radius);
if (End > Start) {
canvas.drawArc(oval, Start, (End - Start), true, paint);
} else if (End < Start) {
canvas.drawArc(oval, Start, ((360 - Start) + End), true, paint);
}
}
}
如果我更新Start
和End
变量,什么都不会发生。我还尝试在类上调用invalidate
,但也没有重新绘制。
我尝试过的invalidate
:
WindRose windrose = new WindRose(this);
windrose.invalidate();
从 WindRose 所在的主类中调用了该方法。
我应该如何正确地做到这一点?