简体   繁体   English

画布上的圆圈网格(Android)

[英]Grid of Circles on Canvas (Android)

I am new to android development. 我是android开发的新手。 I was trying to make game. 我正在尝试制作游戏。 In that game I have to show grid of dots (5*6) on canvas. 在那个游戏中,我必须在画布上显示点网格(5 * 6)。 For this purpose I decided to first show circles of same range and then show bitmaps in them. 为此,我决定首先显示相同范围的圆圈,然后在其中显示位图。 I followed a tutorial for drawing circles. 我遵循了有关绘制圆圈的教程。 I compiled that code. 我编译了该代码。 There is no error or warning there, but canvas don't show any circle. 那里没有错误或警告,但画布没有显示任何圆圈。 Please suggest me what should I do. 请建议我该怎么办。 Below given is code and logcat out put. 下面给出的是代码和logcat的输出。

My Code File: 我的代码文件:

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 File: 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 [  ]
setContentView(R.layout.activity_main);

change this to 更改为

setContentView(new board(this));

Since you are not creating a new instance of the class board and setting as the content, this view wont get added. 由于您没有创建班级board的新实例并将其设置为内容,因此不会添加该视图。 It is conventional to start a class name with capital letter. 通常用大写字母开头一个类名。

Add this inside the onDraw for loop instead of drawing the bitmap. 将其添加到onDraw for循环内,而不是绘制位图。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM