2014-03-29 60 views
1

我正在开发android游戏。我是初学者,所以我不太了解android。我正面临与画布有关的问题。我试图设计包含圆形网格的板(5 * 6)。 我想显示它们之间的线条,使它们看起来像盒子。为此,我使用drawLine()函数。我尝试了不同的值组合,但仍然无法根据需要显示画布。我想从第一行的圆圈和最左侧的圆圈列中删除正在向上行的行。我在下面分享我的代码,请查看此代码并向我建议解决方案?此链接显示目前我的设备屏幕上显示的输出(http://postimg.org/image/m4kgv4ezz/)。drawLine()方法没有正确画线(Android)

代码:

package com.example.linedraw; 

import android.os.Bundle; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Bitmap.Config; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.view.Menu; 
import android.view.View; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(new board(this)); 
    } 

    public class board extends View 
    { 
     Paint pBack = new Paint(); 
     Paint pDot = new Paint(); 

     int cols = 5; 
     int rows = 6; 

     public board(Context context) 
     { 
      super(context); 
      pBack.setARGB(255, 255, 102, 0); 
      pDot.setARGB(255, 255, 255, 255); 
     } 

     @SuppressLint("DrawAllocation") 
     protected void onDraw(Canvas canvas) 
     { 
      super.onDraw(canvas); 
      canvas.drawPaint(pBack); 
      // BitmapFactory.decodeResource(getResources(), R.drawable.marbleback); 
      //Bitmap bd= BitmapFactory.decodeResource(getResources(), R.drawable.blackball); 
      float xStep = canvas.getWidth()/(cols + 1); 
      float yStep = canvas.getHeight()/(rows + 1); 

for (int y = 0; y < rows; y++) 
     { 
      for (int x = 0; x < cols; x++) 
      { 
       canvas.drawCircle((x + 1) * xStep, (y + 1) * yStep, 20, pDot); 
       canvas.drawLine((x + 1) * xStep, (y + 1) * yStep, (rows - 1) * (2+xStep), (y+1) * (yStep), pDot); 
       //canvas.drawLine((x -1) * xStep, (y -1) * yStep, (x-1) * xStep, (y+1) * yStep, pDot); 
       canvas.drawLine((x - 1) * xStep, (y +1) * yStep, (x-1) * (xStep), (cols+1) * (1+yStep), pDot); 
       // canvas.drawLine(startX, startY, stopX, stopY, paint) 
       // canvas.drawBitmap(bd,canvas.getHeight()/(rows+1), canvas.getWidth()/(cols+1), pDot); 
      } 
     } 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 


} 

回答

1

我仍然有这个项目保存。只是几个补充for循环:

for (int y = 0; y < rows; y++) 
{ 
    for (int x = 0; x < cols; x++) 
    { 
     canvas.drawCircle((x + 1) * xStep, (y + 1) * yStep, 20, pDot); 

     if (y == 0) 
     { 
      canvas.drawLine((x + 1) * xStep, yStep, (x + 1) * xStep, rows * yStep, pDot); 
     }     
    } 

    canvas.drawLine(xStep, (y + 1) * yStep, cols * xStep, (y + 1) * yStep, pDot); 
} 
+0

工作正常。非常感谢您一次又一次的帮助。 –

0
Try below code : 
for (int y = 0; y < rows; y++) 
      { 
       for (int x = 0; x < cols; x++) 
       { 
        canvas.drawCircle((x + 1) * xStep, (y + 1) * yStep, 20, pDot); 

        canvas.drawLine(xStep, yStep, xStep, (cols) * (1+yStep), pDot); 

       } 
      } 
+0

不,它不起作用。我现在在代码中显示了这样的输出(http://postimg.org/image/bs90k7i6r/)。 –

+0

查看更新后的问题 –