onDraw(Canvas canvas)从未到过?

时间:2013-06-13 14:33:47

标签: java android android-canvas ondraw

任何人都可以提供帮助,我的应用程序似乎没有在GraView类中执行onDraw(),即使从类内部或外部调用.invalidate也是如此。我设法找出system.err它确实使视图无效,但它永远不会进入onDraw(),即使应用程序的其余部分仍在运行。我发现很多解决方案都是在构造函数中添加setWillNotDraw(false),但这并没有解决任何问题。

GravIO.java文件:

package dabawi.gravitas;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class GravIO extends Activity implements Runnable, OnTouchListener   {
public final static int clock = 1000;
private GravEngine engine;
private GraView TV;
private SensorManager mSensorManager;
private Sensor mSensor;
private Thread t1;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    t1 = new Thread(this);
    t1.start();
    engine = new GravEngine();
    TV = new GraView(this, engine);
    setContentView(TV);
    TV.setOnTouchListener(this);
    TV.setVisibility(View.VISIBLE);     
    //mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    System.err.println("Starting Engine") ;

    run();
}

@Override
public void run() {
    while (true) {
        try {
            System.err.println("Tik") ;
            engine.tick();
            TV.invalidate();
            Thread.sleep(GravIO.clock);
        } catch (InterruptedException ex) {
            System.err.println("faal");
        }
    }
}

@Override
public boolean onTouch(View view, MotionEvent event) {
    engine.switchGravity();
    return true;
}

}

GraView.java文件:

package dabawi.gravitas;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

@SuppressLint({ "DrawAllocation", "ViewConstructor" })
public class GraView extends View {
private GravEngine engine;
private int screenWidth, screenHeight, ySpace = 5, xSpace, scale;
private Paint paint;
private int xLoc, yLoc;
private boolean xWall = false, yWall = false;

public GraView(Context context, GravEngine engine) {
    super(context);
    setWillNotDraw(false);
    this.engine = engine;
    calcScale(context);
}

@Override // We think the problem is with this method, that it is never called upon.
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    System.err.println("Calculating position");
    calcPos();
    canvas = new Canvas();
    // drawing background
    paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    canvas.drawPaint(paint);

    drawGame(canvas);
}

private void drawGame(Canvas canvas) {
    drawRoom(canvas);
    drawPlayer(canvas);
}


@SuppressWarnings("deprecation")
private void calcScale(Context context) {
    WindowManager wm = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    Display d = wm.getDefaultDisplay();

    screenWidth = d.getWidth();
    screenHeight = d.getHeight();
    scale = screenHeight / ySpace;
    xSpace = screenWidth / scale;
}

private void calcPos() {
    xLoc = (engine.getxPlayer() / GravEngine.roomScaling);
    yLoc = (engine.getyPlayer() / GravEngine.roomScaling);
    if (xLoc < (xSpace + 1) / 2) {
        xLoc = (xSpace + 1) / 2;
        xWall = true;
    } else if (xLoc > (GravRoom.xSize - ((xSpace + 1) / 2))) {
        xLoc = GravRoom.xSize - ((xSpace + 1) / 2);
        xWall = true;
    } else {
        xWall = false;
    }
    if (yLoc < (ySpace + 1) / 2) {
        yLoc = (ySpace + 1) / 2;
        yWall = true;
    } else if (yLoc > (GravRoom.xSize - ((ySpace + 1) / 2))) {
        xLoc = GravRoom.ySize - ((ySpace + 1) / 2);
        yWall = true;
    } else {
        yWall = false;
    }
}

private void drawPlayer(Canvas canvas) {
    float xPos = engine.getxPlayer() / GravEngine.roomScaling, yPos = engine
            .getyPlayer() / GravEngine.roomScaling;
    if (xWall) {

    }
    if (yWall) {

    }
    paint.setColor(Color.BLUE);
    int left = (int) (xPos * scale), top = (int) (yPos * scale);
    int right = left + (GravEngine.pxSize / GravEngine.roomScaling) * scale;
    int bot = top + (GravEngine.pySize / GravEngine.roomScaling) * scale;
    canvas.drawRect(left, top, right, bot, paint);
}

private void drawRoom(Canvas canvas) {
    for (int i = 0, x = xLoc - ((xSpace + 1) / 2); i < xSpace + 1; x++, i++) {
        for (int j = 0, y = yLoc - ((ySpace + 1) / 2); i < ySpace + 1; y++, j++) {
            drawRoomPart(x,y,i,j,canvas);
        }
    }
}

private void drawRoomPart(int x, int y, int i, int j, Canvas canvas) {
    if (x >= 0 && y >= 0 && x < GravRoom.xSize && y < GravRoom.ySize) {
        short type = engine.getRoom(engine.getCurrentRoom()).getGridPos(x,y);
        if (type != 0) {
            drawBlock(canvas, i, x, j, y, type);
        }
    }
}

private void drawBlock(Canvas canvas, int i, int x, int j, int y, short type) {
    int left = i * scale, top = y * scale;
    int right = left + scale;
    int bot = top + scale;
    paint.setColor(colorBlock(type));
    canvas.drawRect(left, top, right, bot, paint);
    System.err.println("Left" + left + "   top:   " + top + "  right:  "
            + right + "   bot:   " + bot);
}

private int colorBlock(short type) {
    if (type == 1) {
        return Color.DKGRAY;
    } else if (type == 2) {
        return Color.CYAN;
    } else if (type == 3) {
        return Color.GREEN;
    } else if (type == 4) {
        return Color.RED;
    } else {
        return Color.MAGENTA;
    }
}

// private void imageBlock(short type) {
//
// }

}

2 个答案:

答案 0 :(得分:0)

哎哟!

您对run方法的调用是在UIThread中执行的(作为onCreate方法),并且它在其中执行无限循环。 只需在调用后添加println,您将看到UI线程永远不会被释放,您的应用程序甚至不应该启动!

答案 1 :(得分:0)

您没有看到任何更改,因为您正在创建新画布并在其上绘图。要解决此问题,请删除

canvas = new Canvas();

此外,您应该在Paint方法之外发起onDraw(这是Android文档推荐的优化)。