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

发布于 2024-10-23 19:33:23 字数 2646 浏览 1 评论 0原文

我在这里遇到一个问题,当我有 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}

I am having a problem here it crashes when I have 2 moles, but if there is just one it doesnt crash.

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);
}

Log cat of crash

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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹沉浮 2024-10-30 19:33:23

这是随机的 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;
}

It's the random x and y is taking too long to make

I'm gonna use this now

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文