drawCircle方法工作不正常(Android)

drawCircle方法工作不正常(Android),android,android-canvas,ondraw,Android,Android Canvas,Ondraw,我正在尝试为游戏设计一个5*6网格的棋盘。我将使用这些圆圈在其中显示位图图像(.png)我面临的问题是。当我绘制圆形网格时,它在屏幕上无法正确展开。我使用的是android设备,它的屏幕大小为(480 x 854像素,5.0英寸(~196 ppi像素密度))。我试着用不同的组合画圆圈,但失败了 这是代码 package com.example.rectangle; import android.os.Bundle; import android.annotation.Sup

我正在尝试为游戏设计一个5*6网格的棋盘。我将使用这些圆圈在其中显示位图图像(.png)我面临的问题是。当我绘制圆形网格时,它在屏幕上无法正确展开。我使用的是android设备,它的屏幕大小为(480 x 854像素,5.0英寸(~196 ppi像素密度))。我试着用不同的组合画圆圈,但失败了

这是代码

          package com.example.rectangle;

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.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 p = new Paint();
        Paint blue=new Paint();
        // Paint green=new Paint();
        int rows=5;
        int cols=6;
        Bitmap [][] dot=new Bitmap[rows][cols];
        Canvas g=new Canvas();
        public board(Context context) {
            super(context);
            p.setARGB(255, 255, 102,0);
            blue.setARGB(255, 255, 255, 255);
            // green.setARGB(0, 0, 06, 0);

        }
        @SuppressLint("DrawAllocation")
        protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);
            canvas.drawPaint(p);
            for(int y=0; y<cols; y++)
            {
                for(int x=0; x<rows; x++)
                {
                    Bitmap grid= Bitmap.createBitmap(100,100, Config.RGB_565 );

                    dot[x][y]=grid;
                    g.setBitmap(dot[x][y]);
                    //canvas.drawCircle(50, 50, 20, blue);
                    //g.drawCircle(50, 50, 20, blue);


                }
            }
            for(int y=0; y<cols; y++)
            {
                for(int x=0; x<rows; x++)
                {
                    //canvas.drawCircle(50, 50, 20, blue);
                    //canvas.drawCircle(50, (x + 1) * 2 * 50, (y + 1) * 2 * 50, blue);
                    canvas.drawCircle(85*x, 110*y, 20, blue);
                    canvas.drawBitmap(dot[x][y], x*100, y*100,null);
                }
            }

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

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


}
package com.example.rectangle;
导入android.os.Bundle;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.Bitmap.Config;
导入android.graphics.Canvas;
导入android.graphics.Paint;
导入android.view.Menu;
导入android.view.view;
公共类MainActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(新董事会(本));
}
公营班级委员会扩展视野
{
油漆p=新油漆();
蓝色油漆=新油漆();
//油漆绿色=新油漆();
int行=5;
int cols=6;
位图[][]点=新位图[行][cols];
Canvas g=新画布();
公共董事会(背景){
超级(上下文);
p、 setARGB(255,255,102,0);
蓝色。setARGB(255、255、255、255);
//绿色.setARGB(0,0,06,0);
}
@SuppressLint(“DrawAllocation”)
受保护的void onDraw(画布)
{
super.onDraw(帆布);
帆布、拉丝漆(p);

对于(int y=0;y我正在使用此代码绘制piechart及其与我的合作:希望它能帮助:

public class AChartEnginePieChartActivity extends Activity { 

private static int[] COLORS = new int[] { Color.GREEN, Color.BLUE,Color.MAGENTA, Color.CYAN };

private static double[] VALUES = new double[] { 10, 11, 12, 13 };

private static String[] NAME_LIST = new String[] { "A", "B", "C", "D" };

private CategorySeries mSeries = new CategorySeries("");

private DefaultRenderer mRenderer = new DefaultRenderer();

private GraphicalView mChartView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
mRenderer.setZoomButtonsVisible(true);
mRenderer.setStartAngle(90);

for (int i = 0; i < VALUES.length; i++) {
mSeries.add(NAME_LIST[i] + " " + VALUES[i], VALUES[i]);
SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
renderer.setColor(COLORS[(mSeries.getItemCount() - 1) % COLORS.length]);
mRenderer.addSeriesRenderer(renderer);
}

if (mChartView != null) {
mChartView.repaint();
}

}

@Override
protected void onResume() {
super.onResume();
if (mChartView == null) {
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
mChartView = ChartFactory.getPieChartView(this, mSeries, mRenderer);
mRenderer.setClickEnabled(true);
mRenderer.setSelectableBuffer(10);

mChartView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();

if (seriesSelection == null) {
Toast.makeText(AChartEnginePieChartActivity.this,"No chart element was clicked",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AChartEnginePieChartActivity.this,"Chart element data point index "+ (seriesSelection.getPointIndex()+1) + " was clicked" + " point value="+ seriesSelection.getValue(), Toast.LENGTH_SHORT).show();
}
}
});

mChartView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast.makeText(AChartEnginePieChartActivity.this,"No chart element was long pressed", Toast.LENGTH_SHORT);
return false; 
} else {
Toast.makeText(AChartEnginePieChartActivity.this,"Chart element data point index "+ seriesSelection.getPointIndex()+ " was long pressed",Toast.LENGTH_SHORT);
return true;       
}
}
});
layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
else {
mChartView.repaint();
}
}
}
公共类AChartEnginePieChartActivity扩展活动{
私有静态int[]COLORS=newint[]{Color.GREEN,Color.BLUE,Color.MAGENTA,Color.CYAN};
私有静态double[]值=新的double[]{10,11,12,13};
私有静态字符串[]NAME_LIST=新字符串[]{“A”、“B”、“C”、“D”};
私有类别系列mSeries=新类别系列(“”);
private DefaultRenderer mRenderer=新的DefaultRenderer();
private GraphicalView mChartView;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mrender.setApplyBackgroundColor(真);
mrender.setBackgroundColor(Color.argb(100,50,50,50));
mRenderer.setChartTitleTextSize(20);
mrender.setLabelsTextSize(15);
mrender.setLegendTextSize(15);
mRenderer.setMargins(新的int[]{20,30,15,0});
mrender.setZoomButtonsVisible(true);
mrender.设置起始角(90);
对于(int i=0;i

您可以使用背景img资源替换姓名列表。

您的代码的主要问题是您得到的
x
/
y
(水平/垂直)和
/
混合。
x
(水平)应由
cols
y
(垂直)确定通过
。下面的代码是一个非常简单的示例,因为我确信,随着项目的进展,您将需要对图形布局进行调整。请注意,网格的垂直居中可能有点偏离,这取决于您的应用程序是否有标题栏,以及是否为全屏

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

    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        canvas.drawPaint(pBack);

        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);
            }
        }
    }
}
公共类板扩展视图
{
Paint pBack=新油漆();
油漆pDot=新油漆();
int cols=5;
int行=6;
公共董事会(背景)
{
超级(上下文);
pBack.setARGB(255、255、102、0);
pDot.setARGB(255、255、255、255);
}
受保护的void onDraw(画布)
{
super.onDraw(帆布);
帆布、拉丝漆(pBack);
float xStep=canvas.getWidth()/(cols+1);
float yStep=canvas.getHeight()/(行+1);
对于(int y=0;y
wuth drawCircle有什么问题吗?发布错误的图片