我现在有一个在 onCreate 之后以编程方式添加的 View (取决于其他一些变量)。一切正常,它绘制了一个圆圈的一部分。
但我的问题是我以后如何重新绘制它?获取一些数据后,我需要更改圆圈中的角度。
风玫瑰代码:
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
但我也没有得到任何重绘。
使我尝试过的无效:
WindRose windrose = new WindRose(this);
windrose.invalidate();
从 WindRose 所属的主类调用。
我应该如何正确执行此操作?
最佳答案
也许调用 View 的 invalidate()
方法会有帮助。
您可以在此处阅读更多内容 ( http://developer.android.com/reference/android/view/View.html ),但是: “要强制绘制 View ,请调用 invalidate()。”
请注意,您只能使 View 的一部分无效
关于android - 重绘 View Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22233221/