【问题标题】:drawLine() method not drawing lines properly (Android)drawLine() 方法无法正确绘制线条(Android)
【发布时间】:2014-03-29 05:03:43
【问题描述】:

我正在用 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;
    }


}

【问题讨论】:

    标签: android canvas android-canvas drawable


    【解决方案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);
    }
    

    【讨论】:

      【解决方案2】:
      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);
      
                      }
                  }
      

      【讨论】:

      猜你喜欢
      • 2022-01-07
      • 1970-01-01
      • 2012-06-30
      • 2020-02-20
      • 1970-01-01
      • 2012-07-05
      • 2012-10-05
      • 1970-01-01
      • 2023-01-06
      相关资源
      最近更新 更多