我正在尝试创建一个网格,显示每 1mm 的线条和每 10mm 的粗线条,并具有以下代码:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 1,
getResources().getDisplayMetrics());
float currentH = 0;
float currentW = 0;
int countX = 0;
int countY = 0;
DisplayMetrics metrics = getResources().getDisplayMetrics();
while (currentW < metrics.widthPixels) {
if(countX % 10 == 0){
this.paint.setStrokeWidth(0.5f);
this.paint.setColor(getResources().getColor(R.color.GridGreenTens));
} else {
this.paint.setStrokeWidth(0.1f);
this.paint.setColor(getResources().getColor(R.color.GridGreen));
}
canvas.drawLine(currentW, 0, currentW, metrics.heightPixels, paint);
countX++;
currentW += px;
}
while (currentH < metrics.heightPixels) {
if(countY % 10 == 0){
this.paint.setStrokeWidth(0.5f);
this.paint.setColor(getResources().getColor(R.color.GridGreenTens));
} else {
this.paint.setStrokeWidth(0.1f);
this.paint.setColor(getResources().getColor(R.color.GridGreen));
}
canvas.drawLine(0, currentH, metrics.widthPixels, currentH, paint);
countY++;
currentH += px;
}
}
我的问题是它显示如下:
如您所见,粗线 (o.5f) 出现的频率比应有的要高得多。任何帮助将不胜感激。