drawLine()方法没有正确绘制线条(Android)

时间:2014-03-29 05:03:43

标签: android canvas android-canvas drawable

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

代码:

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;
    }


}

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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);

                }
            }