Android:drawText没有显示过绘制

时间:2013-04-11 00:42:32

标签: android drawtext

这是我的第一个Android应用程序,我在使用drawText()时遇到了一些困难。我一直在通过Mario Zechner的 Beginning Android Games 自学Android编程。我正在调整他用于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() {

    }

}

2 个答案:

答案 0 :(得分:0)

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

为您提供帮助:

添加到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);
}

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

答案 1 :(得分: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