如何在onDraw中绘制4行? - android

时间:2012-02-04 08:55:15

标签: android

回到另一个问题,

如何在onDraw中绘制4行? 这些是4条常量线,红色代表我的视图边框。 我尝试绘图,只能绘制1行,甚至与我的屏幕宽度相同。

建议?

谢谢!

2 个答案:

答案 0 :(得分:4)

尝试:

public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint redPaint = new Paint();
    redPaint.setColor(Color.RED);
    redPaint.setStrokeWidth(5); // set stroke so you can actually see the lines

    canvas.drawLine(0, 0, getMeasuredWidth(), 0, redPaint);
    canvas.drawLine(getMeasuredWidth(), 0, getMeasuredWidth(), getMeasuredHeight(), redPaint);
    canvas.drawLine(getMeasuredWidth(), getMeasuredHeight(), 0, getMeasuredHeight(), redPaint);
    canvas.drawLine(0, getMeasuredHeight(), 0, 0, redPaint);
}

答案 1 :(得分:2)

void    drawLine(float startX, float startY, float stopX, float stopY, Paint paint)
Draw a line segment with the specified start and stop x,y coordinates, using the specified paint.

Paint paint = new Paint();
paint.setColor(Color.Red);

onDraw( Canvas canvas){
     canvas.drawLine(x,y,x1,y1, paint);
     canvas.drawLine(x,y,x1,y1, paint);
     canvas.drawLine(x,y,x1,y1, paint);
     canvas.drawLine(x,y,x1,y1, paint);
}

更改(x,y)和(x1,y1)

的值