从onDraw打击app的调用方法

时间:2015-01-15 16:40:35

标签: android graphics2d

我有以下代码来破坏应用程序。像drawLine这样的方法如果它们直接位于“onDraw”中,则可以正常工作,但如果放在一个单独的方法中,则不会如下所示。有谁知道什么是错的?使用方法编译好的版本,但应用程序在启动时崩溃。

非常感谢    罗伯特

public class Painter extends View {

private static Canvas canvas;
private static Paint paintG = null;
public static int screenW; // screen width
public static int screenH; // screen height

private String tag = "Painter";

public Painter(Context context) {
    super(context);
    readDrawingData(); 
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    screenW = canvas.getWidth(); // screen width
    screenH = canvas.getHeight(); // screen height
    Rect allScreen = new Rect(0, 0, screenW, screenH);

    // works OK
    paintG = new Paint();
    paintG.setAntiAlias(true);
    paintG.setStrokeWidth(2);
    paintG.setStyle(Paint.Style.FILL);
    paintG.setColor(0xFFf7f9d2);
    canvas.drawRect(allScreen, paintG);

    // paint circle/oval - works OK
    double R = ((screenW > screenH) ? screenH : screenW) / 2 - 100;
    double Z = R + 20;
    double X = screenW /2;
    double Y = screenH /2;
    RectF oval1 = new RectF(Math.round(X-R), Math.round(Y-R), 
                            Math.round(X+R), Math.round(Y+R));
    paintG.setStyle(Paint.Style.STROKE);
    paintG.setColor(0xffff0000);
    canvas.drawOval(oval1, paintG);

    // this code works OK here
    paintG.setColor(0xffff0000);
    if (i > 0) {
        for (int j=0; j<i; j++) {
            canvas.drawLine(x1[j], y1[j], x2[j], y2[j], paintG);
        }
    }

// calling this will crash app
paintDrawing();

}

// ************************************************************
void paintDrawing() {
    // PROBLEM: same code as above - causes app to crash...
    // when outside onDraw
    paintG.setColor(0xffff0000);
    if (i > 0) {
        for (int j=0; j<i; j++) {
        canvas.drawLine(x1[j], y1[j], x2[j], y2[j], paintG);
    }
}
// ************************************************************ 

}

0 个答案:

没有答案