2013-10-31 49 views
0

我想畫一個小矩形。問題是矩形沒有任何邊框,所以結果只是一個大矩形(包含所有小矩形(如下所述的20x20矩形))。如何在Android上的畫布上繪製小切線?

在我的自定義View,我重寫的onDraw:

protected void onDraw(Canvas canvas) { 
    Paint paint = new Paint(); 
    paint.setColor(Color.WHITE); 
    canvas.drawRect(getWidth(), getHeight(), 50, 50, paint); 

    paint.setColor(Color.LTGRAY); 
    paint.setStrokeWidth(3); 
    for(int i = 0; i < 20; i++) { 
     for(int j = 0; j < 20; j++) { 
      canvas.drawRect(20*j, 20*i, 50, 50, paint); 
     } 
    } 
} 

對於Java Swing中,你可以做

g.setColor(Color.red); 
for(int x = 0; x < 20; x++) { 
    for(int y = 0; y < 20; y++) { 
     g.drawRect(20 * x, 20 * y, 20, 20); 
    } 
} 

繪製矩形和每個矩形將有紅色邊框。

我該如何在Android上實現這一目標?

+0

http://stackoverflow.com/questions/7344497/android-canvas-draw-rectangle –

回答

0

我想你需要在繪製小矩形之前將繪畫風格改爲筆畫。 使用mPaint.setStyle(Paint.Style.STROKE)以及設置筆畫寬度。 在你的代碼中,你沒有在任何地方聲明過繪製風格。 如果您使用Paint.Style.FILL它將填充矩形。