drawCircle方法无法正常工作(Android)

时间:2014-03-26 10:25:39

标签: 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.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;
    }


}
  • 问题在于这一行:canvas.drawCircle(90 * x,110 * y,20, 蓝色);
  • 屏幕尺寸为:(480 x 854像素,5.0英寸(~196 ppi像素) 密度)

2 个答案:

答案 0 :(得分:1)

您的代码存在的主要问题是,您将x / y(水平/垂直)与rows / cols混为一谈。 x(水平)应由cols确定,y(垂直)由rows确定。下面的代码是一个非常小的例子,因为,我确信,随着项目的进展,您需要对图形布局进行调整。请注意,网格的垂直居中可能有点偏差,具体取决于您的应用是否有标题栏以及是否为全屏。

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

答案 1 :(得分:0)

我正在使用此代码绘制饼图并与我合作:希望它会有所帮助:

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

您可以使用背景图片资源替换名称列表。