我每10秒在画布上进行一些绘画。不幸的是它会在重绘之前消失,因此我们有10秒的空白屏幕。在绘制和恢复之前我累了保存画布,没有帮助。
我将一行canvas.drawPath(linePath, linePaint);
从一个循环外部转移到循环中后出现了这个错误。
CODE:
private void drawLine(Canvas canvas) {
yStep = (yHeight) / fullChargeLevel;
xStep = (xWidth) / timeStampBarsQuantity;
boolean check = false;
float time;
float chrg;
while (batteryUsageHistory != null && batteryUsageHistory.moveToNext()) {
int charge = batteryUsageHistory.getInt(1);
int time_stamp = batteryUsageHistory.getInt(2);
if (charge < 1) {
if(check){
canvas.drawPath(linePath, linePaint); //This line I shifted into here
linePath.reset();
}
check = false;
continue;
}
time = xPos + time_stamp * xStep;
chrg = yPos - (charge * yStep);
if (!check) {
linePath.moveTo(time, chrg);
check = true;
continue;
}
linePath.lineTo(time, chrg);
}
//canvas.drawPath(linePath, linePaint); //This line I shifted from here
}
答案 0 :(得分:1)
您应该通过扩展一些View类来实现onDraw中的绘图逻辑:
protected void onDraw(Canvas canvas) {
// here goes your custom drawing logic
}
如:https://developer.android.com/training/custom-views/custom-drawing.html
中所述这是因为android在需要时重新绘制组件。总是那样你需要实现绘图方法,GUI框架会在必要时调用这个方法,而不是相反,GUI框架显示你的绘画,它只是在需要时调用你的绘画方法。
答案 1 :(得分:1)
我刚回到那条线canvas.drawPath(linePath, linePaint);
我之前感动了。它有效!。