2

这是我的第一个 Android 应用程序,我在使用 drawText() 时遇到了一些困难。通过 Mario Zechner 的《Beinging Android Games 》,我一直在自学 Android 编程。我正在修改他在书中用于 Mr. Nom 游戏的代码来创建我自己的游戏。这是一个颜色学习游戏,用户在其中点击颜色网格。我能够使用 graphics.drawRect() 成功绘制颜色网格和显示用户要“查找”的颜色的空间,以及使用 graphics.drawPixmap() 的其他几个图形资源。我试图将每种颜色的名称放在每个颜色填充矩形的顶部。我在代码底部添加了任意一行文本,如下所示:

p.setTypeface(Typeface.SERIF);
p.setTextSize(100);
p.setColor(Color.WHITE);
p.setStyle(Style.FILL);
canvas.drawPaint(p);
canvas.drawText("RED",120, 120, p);

阅读类似的问题,我相信我的问题是我没有正确地告诉它与其他图形元素绘制到同一个屏幕上。如果我不清楚,我很抱歉 - 这又是我的第一个项目。请让我知道您可能需要哪些其他信息来帮助我。感谢您的时间和考虑!

以下是使用的其余 java 代码:

package com.lilyandrosieshow.colors;


import java.util.List;
import java.util.Random;

import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.graphics.Paint.Style;


import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.Screen;
import com.badlogic.androidgames.framework.Sound;
import com.lilyandrosieshow.colors.ColorGrid;

public class GameScreen extends Screen {

    Random random = new Random();
    World world;
    ColorGrid colorGrid = new ColorGrid();
    Canvas canvas = new Canvas();
    Paint p = new Paint();

    static Sound[] colorSoundL = { Assets.lred, Assets.lorange, Assets.lyellow, Assets.lgreen, 
                                   Assets.lblue, Assets.lpurple, Assets.lbrown, Assets.lgrey, 
                                   Assets.lblack, Assets.lpink, Assets.lcyan, Assets.lwhite };
    static Sound[] colorSoundR = { Assets.rred, Assets.rorange, Assets.ryellow, Assets.rgreen, 
                                   Assets.rblue, Assets.rpurple, Assets.rbrown, Assets.rgrey, 
                                   Assets.rblack, Assets.rpink, Assets.rcyan, Assets.rwhite };
    static Sound[] correct1 = { Assets.correct1, Assets.correct2, Assets.correct3, Assets.correct4, Assets.correct5 };
    static Sound[] correct2 = { Assets.correcta, Assets.correctb, Assets.correctc, Assets.correctd, Assets.correcte };
    static Sound[] wrong1 = {Assets.wrong1, Assets.wrong2, Assets.wrong3, Assets.wrong4, Assets.wrong5 };
    static Sound[] wrong2 = {Assets.wronga, Assets.wrongb, Assets.wrongc, Assets.wrongd, Assets.wronge };
    static Sound[] ask = {Assets.ask1, Assets.ask2, Assets.ask3, Assets.ask4, Assets.ask5 };
    static Sound[] repeat = { Assets.again1, Assets.again2, Assets.again3, Assets.again4, Assets.again5 };

    int row = -1;
    int column = -1;

    public GameScreen(Game game) {
        super(game);
        this.world = new World();
    }

    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        int len = touchEvents.size();

        for (int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);

            // Color Touched
            if (event.type == TouchEvent.TOUCH_UP && 
                    event.x >= 0 && event.x  <= 320 && event.y >= 0 && event.y <= 240) {

                for (int j = 1; j <= 4; j++){
                    if (event.x < (j*80)) {
                        column = j;
                        break;
                    }
                }
                for (int j = 1; j <= 3; j++){
                    if (event.y < (j*80)) {
                        row = j;
                        break;
                    }
                }

                updateColorTouched(column, row);

                if (world.colorTouched.x == world.colorWanted.x && world.colorTouched.y == world.colorWanted.y) {
                    correct2[new Random().nextInt(5)].play(1);
                    if (world.whichHead == World.WhichHead.LILY) colorSoundL[world.colorTouched.id].play(1);
                    if (world.whichHead == World.WhichHead.ROSIE) colorSoundR[world.colorTouched.id].play(1);
                    //world.colorTouched.s.play(1);
                    world.colorWanted = colorGrid.squares.get(random.nextInt(12));
                    //Assets.ask1.play(1);
                    //colorSet[colorWanted-1].play(1);
                } else {
                    wrong2[new Random().nextInt(5)].play(1);
                    //colorSet[colorTouched-1].play(1);
                }
            }
            // A Head Was Touched
            if (event.type == TouchEvent.TOUCH_UP && event.y > 240 && event.y <= 480){
                if (event.x > 0 && event.x < 160) world.changeVoice(World.WhichHead.LILY);
                if (event.x > 161 && event.x < 320) world.changeVoice(World.WhichHead.ROSIE);
            }
        }
    }


    @Override
    public void present(float deltaTime) { // Draw everything to the screen here
            Graphics g = game.getGraphics();
            g.drawRect(0, 0, g.getWidth(), g.getHeight(), Color.rgb(75, 75, 75));
            g.drawPixmap(Assets.findthis, 5, 245);
            g.drawRect(165, 245, 150, 70, Color.rgb(world.colorWanted.r,
                    world.colorWanted.g,
                    world.colorWanted.b)); 
            g.drawPixmap(Assets.lilyheadopen, 5, 325, 5, 5, 155, 155);
            g.drawPixmap(Assets.rosieheadopen, 165, 325, 5, 5, 155, 155);
            // Draws the grid
            for (int i = 0; i < 12; i++) {
                g.drawRect((
                          (world.colorGrid.squares.get(i).x-1)*80),
                          (world.colorGrid.squares.get(i).y-1)*80,
                           80,
                           80, 
                           Color.rgb(world.colorGrid.squares.get(i).r, world.colorGrid.squares.get(i).g, world.colorGrid.squares.get(i).b));
                // ************* DRAW TEXT NAMES HERE ********************
                p.setTypeface(Typeface.SERIF);
                p.setTextSize(100);
                p.setColor(Color.WHITE);
                p.setStyle(Style.FILL);
                canvas.drawPaint(p);
                canvas.drawText("RED",120, 120, p);
            }
    }

    public void updateColorTouched(int x, int y){
        for (int i = 1; i <= 12; i++) {
            if (x == world.colorGrid.squares.get(i-1).x && y == world.colorGrid.squares.get(i-1).y){
                world.colorTouched = world.colorGrid.squares.get(i-1);
                break;
            }
        }
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }

}

4

2 回答 2

0

您可能希望使用方法来扩展接口,而不是绘制到Canvas不会在任何地方使用的对象。这将类似于在 中定义和实现的所有其他绘图调用。GraphicsdrawText(...)GraphicsAndroidGraphics

为您提供帮助:

添加到Graphics.java

public void drawText(String text, float x, float y, Paint p);

添加到AndroidGraphics.java

@Override public void drawText(String text, float x, float y, Paint p) {
    paint.set(p);
    canvas.drawText(text, x, y, paint);
}

如果您需要drawText(...)Android 类中可用的任何其他重载Canvas,您可以重复上述相同的步骤。

于 2013-04-11T02:19:07.330 回答
0

尝试这个:

p.setTypeface(Typeface.SERIF);
            p.setTextSize(100);
            p.setColor(Color.WHITE);
            p.setStyle(Style.FILL);
Bitmap bitmap = Bitmap.createBitmap(200,200,Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
            //canvas.drawPaint(p);
            canvas.drawText("RED",120, 120, p);
g.drawPixmap(new AndroidPixmap(bitmap,PixmapFormat.ARG_8888),839,282);//your actual
 //coordinates instead of 839,282 though
于 2013-04-11T02:27:20.150 回答