问题在画布上绘制两个相同的图像

时间:2011-03-19 19:59:04

标签: android math canvas surfaceview

我遇到一个问题,当我有2个痣时会崩溃,但如果只有一个,它就不会崩溃。

private int randX(){
    int x = (int) Math.round((Math.random()*CollierSurface.getWidth()));
    if (x<CollierSurface.getWidth()) randX();
    return x;
}

private int randY(){
    int y = (int) Math.round((Math.random()*CollierSurface.getHeight()));
    if (y<CollierSurface.getWidth()) randY();
    return y;
}

private void DrawMoles() {
    if (!_canDraw) return;
    try {
        CollierCanvas = CollierHolder.lockCanvas();
            Drawable background = getResources().getDrawable(R.drawable.collierabove);
            background.setBounds(0, 0, CollierSurface.getWidth(), CollierSurface.getHeight());
            background.draw(CollierCanvas);
            newMole(randX(), randY(), mole1);
            newMole(randX(), randY(), mole2);
    } catch (SurfaceHolder.BadSurfaceTypeException e) {

    } finally {
        if (CollierCanvas != null){
        CollierHolder.unlockCanvasAndPost(CollierCanvas);
        }
    }
}

private void newMole(int x, int y, Drawable mole){
    mole = getResources().getDrawable(R.drawable.mole);
    mole.mutate().setBounds((int)x-(mole.getIntrinsicWidth()), 
                        (int)y-(mole.getIntrinsicHeight()), 
                        (int)x+(mole.getIntrinsicWidth()), 
                        (int)y+(mole.getIntrinsicHeight()));
    mole.draw(CollierCanvas);
}

记录崩溃的猫

03-19 17:48:14.342: ERROR/Setting(1969): USB debugging enabled
03-19 17:48:18.462: WARN/PowerManagerService(1928): Timer 0x3->0x3|0x1
03-19 17:48:18.777: ERROR/AndroidRuntime(10073): ERROR: thread attach failed
03-19 17:48:19.802: ERROR/AndroidRuntime(10081): ERROR: thread attach failed
03-19 17:48:19.927: WARN/Resources(1928): Converting to boolean: TypedValue{t=0x3/d=0xc4e "res/anim/accelerate_decelerate_interpolator.xml" a=2 r=0x10a0004}
03-19 17:48:19.947: WARN/Resources(1928): Converting to boolean: TypedValue{t=0x3/d=0xc4e "res/anim/accelerate_decelerate_interpolator.xml" a=2 r=0x10a0004}
03-19 17:48:19.962: WARN/ActivityThread(10088): Application com.collierhs.game is waiting for the debugger on port 8100...
03-19 17:48:22.647: ERROR/gralloc(1928): [unregister] handle 0x454ea0 still locked (state=40000001)
03-19 17:48:24.237: WARN/dalvikvm(10094): No implementation found for native Lcom/carrieriq/iqagent/client/NativeClient;.clientInit (Ljava/lang/Object;)I
03-19 17:48:39.750: WARN/ActivityManager(1928): Launch timeout has expired, giving up wake lock!    03-19 17:48:39.922: WARN/ActivityManager(1928): Activity idle timeout for HistoryRecord{4597c840 com.collierhs.game/.whack}

1 个答案:

答案 0 :(得分:0)

随机x和y花费的时间太长了

我现在要用这个

private int randX(){
    Random random = new Random();
    int x = random.nextInt(CollierSurface.getWidth());
    return x;
}

private int randY(){
    Random random = new Random();
    int y = random.nextInt(CollierSurface.getHeight());
    return y;
}