画布上的圆圈网格(Android)

时间:2014-03-24 07:35:00

标签: android android-canvas

我是Android开发的新手。我试图制作游戏。在那个游戏中,我必须在画布上显示网格点(5 * 6)。为此,我决定先显示相同范围的圆圈,然后在其中显示位图。我按照绘制圆圈的教程。我编译了那段代码。那里没有错误或警告,但画布不显示任何圆圈。请建议我该怎么办。下面给出的是代码和logcat输出。

我的代码文件:

package com.example.circle;

import android.os.Bundle;
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(R.layout.activity_main);
    }
    public class board extends View
    {
        Paint p = new Paint();
        Paint black=new Paint();
        Paint blue=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, 255, 255);
            black.setARGB(255, 0, 0, 0);
            blue.setARGB(255, 255, 0, 0);
            for(int y=0; y<cols; y++)
            {
                for(int x=0; x<rows; x++)
                {
                    Bitmap map= Bitmap.createBitmap(100,100, Config.RGB_565);
                    dot[x][y]=map;
                    g.setBitmap(dot[x][y]);
                    g.drawCircle(50, 50, 50, black);

                }
            }
        }
        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++)
                {
                    canvas.drawBitmap(dot[x][y], x*100, y*100,null);
                }
            }

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

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


}

Logcat文件:

03-24 12:33:37.162: D/IPCThreadState(28936): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x71a620
03-24 12:33:37.165: V/MediaProvider/DrmHelper(28263): synchronized permitAccessDrm(28936) return false
03-24 12:33:37.175: D/IPCThreadState(28263): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x87ac38
03-24 12:33:37.175: D/IPCThreadState(233): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x9bcd28
03-24 12:33:37.176: D/IPCThreadState(28936): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x71a620
03-24 12:33:37.180: V/MediaProvider/DrmHelper(28263): synchronized permitAccessDrm(28936) return false
03-24 12:33:37.184: D/IPCThreadState(28263): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x87db38
03-24 12:33:37.184: D/IPCThreadState(233): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x60a430
03-24 12:33:37.186: D/IPCThreadState(28936): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x71a620
03-24 12:33:37.251: I/SurfaceFlinger(101): [SurfaceFlinger] frames:2, duration:1.502000, fps:1.331077
03-24 12:33:37.340: D/IPCThreadState(28222): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x75a150
03-24 12:33:37.341: D/IPCThreadState(233): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0xa0c9b0
03-24 12:33:37.342: D/IPCThreadState(28936): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x7b20c8
03-24 12:33:37.346: I/CallLogProvider(24184): match == 1
03-24 12:33:37.346: D/CallLogProvider(24184):    In call log providers,  selectionBuilder=((type != '4'))
03-24 12:33:37.364: D/CallLogProvider(24184): query count == 413
03-24 12:33:37.365: D/IPCThreadState(24184): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x971810
03-24 12:33:37.366: D/IPCThreadState(233): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0xd21190
03-24 12:33:37.367: D/IPCThreadState(28936): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x7c7198
03-24 12:33:37.371: V/ContactsProvider(24184): query begin uri [content://com.android.contacts/contacts] selection [  ]

1 个答案:

答案 0 :(得分:0)

setContentView(R.layout.activity_main);

将此更改为

setContentView(new board(this));

由于您没有创建类board的新实例并将其设置为内容,因此不会添加此视图。通常用大写字母开始一个类名。

onDraw for循环中添加此内容,而不是绘制位图。

canvas.drawCircle(50, (x + 1) * 2 * 50, (y + 1) * 2 * 50, black);