导致java.lang.OutOfMemoryError的缩放位图的分配失败

时间:2014-07-20 10:03:30

标签: java android eclipse bitmap out-of-memory

我正在eclipse中开发一款针对android的whack-a-mole游戏。到目前为止,我知道我的应用程序适用于Android的每个API,除了Kitkat(4.4)。 android 4.4中发生的事情是启动画面加载正常,我的主要活动也开始正常,但要启动游戏,您需要点击主要活动的第一个屏幕。当你这样做时,模拟器显示:“不幸的是我的应用程序停止了。”

我试过this way

这是我的代码:

doTouchEvent方法

boolean doTouchEvent(MotionEvent event) {
        synchronized (mySurfaceHolder) {
            int eventaction = event.getAction();
            int X = (int) event.getX();
            int Y = (int) event.getY();

            switch (eventaction) {

            case MotionEvent.ACTION_DOWN:
                if (!gameOver) {
                    fingerX = X;
                    fingerY = Y;
                    if (!onTitle && detectMoleHit()) {
                        whacking = true;
                        molesWhacked++;
                        if (molesWhacked < highscore) {
                            highscore = highscore;
                        } else if (molesWhacked == highscore) {
                            molesWhacked = highscore;
                        } else if (molesWhacked > highscore) {
                            highscore++;
                        }
                    }
                }
                break;

            case MotionEvent.ACTION_MOVE:
                break;

            case MotionEvent.ACTION_UP:
                if (onTitle || gameOver || gameOverRed || gameOverBlue
                        || gameOverGreen || gameOverPink || gameOverYellow) {

                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    int imageHeight = options.outHeight;
                    int imageWidth = options.outWidth;
                    String imageType = options.outMimeType;

                    backgroundImg = BitmapFactory
                            .decodeResource(myContext.getResources(),
                                    R.drawable.background);
                    backgroundImg = Bitmap.createScaledBitmap(
                            backgroundImg, screenW, screenH, true);

                    maskup = BitmapFactory.decodeResource(
                            myContext.getResources(), R.drawable.maskup);
                    maskmiddle = BitmapFactory
                            .decodeResource(myContext.getResources(),
                                    R.drawable.maskmiddle);
                    maskdown = BitmapFactory.decodeResource(
                            myContext.getResources(), R.drawable.maskdown);
                    molered = BitmapFactory.decodeResource(
                            myContext.getResources(), R.drawable.molered);
                    moleblue = BitmapFactory.decodeResource(
                            myContext.getResources(), R.drawable.moleblue);
                    molegreen = BitmapFactory.decodeResource(
                            myContext.getResources(), R.drawable.molegreen);
                    molepink = BitmapFactory.decodeResource(
                            myContext.getResources(), R.drawable.molepink);
                    moleyellow = BitmapFactory
                            .decodeResource(myContext.getResources(),
                                    R.drawable.moleyellow);
                    whack = BitmapFactory.decodeResource(
                            myContext.getResources(), R.drawable.whack);
                    gameoverred = BitmapFactory.decodeResource(
                            myContext.getResources(),
                            R.drawable.gameoverred);
                    gameoverblue = BitmapFactory.decodeResource(
                            myContext.getResources(),
                            R.drawable.gameoverblue);
                    gameovergreen = BitmapFactory.decodeResource(
                            myContext.getResources(),
                            R.drawable.gameovergreen);
                    gameoverpink = BitmapFactory.decodeResource(
                            myContext.getResources(),
                            R.drawable.gameoverpink);
                    gameoveryellow = BitmapFactory.decodeResource(
                            myContext.getResources(),
                            R.drawable.gameoveryellow);

                    scaleW = (float) screenW / (float) backgroundOrigW;
                    scaleH = (float) screenH / (float) backgroundOrigH;

                    maskup = Bitmap.createScaledBitmap(maskup,
                            (int) (maskup.getWidth() * scaleW),
                            (int) (maskup.getHeight() * scaleH), true);
                    maskmiddle = Bitmap.createScaledBitmap(maskmiddle,
                            (int) (maskmiddle.getWidth() * scaleW),
                            (int) (maskmiddle.getHeight() * scaleH), true);
                    maskdown = Bitmap.createScaledBitmap(maskdown,
                            (int) (maskdown.getWidth() * scaleW),
                            (int) (maskdown.getHeight() * scaleH), true);
                    molered = Bitmap.createScaledBitmap(molered,
                            (int) (molered.getWidth() * scaleW),
                            (int) (molered.getHeight() * scaleH), true);
                    moleblue = Bitmap.createScaledBitmap(moleblue,
                            (int) (moleblue.getWidth() * scaleW),
                            (int) (moleblue.getHeight() * scaleH), true);
                    molegreen = Bitmap.createScaledBitmap(molegreen,
                            (int) (molegreen.getWidth() * scaleW),
                            (int) (molegreen.getHeight() * scaleH), true);
                    molepink = Bitmap.createScaledBitmap(molepink,
                            (int) (molepink.getWidth() * scaleW),
                            (int) (molepink.getHeight() * scaleH), true);
                    moleyellow = Bitmap.createScaledBitmap(moleyellow,
                            (int) (moleyellow.getWidth() * scaleW),
                            (int) (moleyellow.getHeight() * scaleH), true);
                    whack = Bitmap.createScaledBitmap(whack,
                            (int) (whack.getWidth() * scaleW),
                            (int) (whack.getHeight() * scaleH), true);
                    gameoverred = Bitmap.createScaledBitmap(gameoverred,
                            screenW, screenH, true);
                    gameoverblue = Bitmap.createScaledBitmap(gameoverblue,
                            screenW, screenH, true);
                    gameovergreen = Bitmap.createScaledBitmap(
                            gameovergreen, screenW, screenH, true);
                    gameoverpink = Bitmap.createScaledBitmap(gameoverpink,
                            screenW, screenH, true);
                    gameoveryellow = Bitmap.createScaledBitmap(
                            gameoveryellow, screenW, screenH, true);

                    onTitle = false;

                    pickMole();
                }
                whacking = false;
                if (gameOver || gameOverRed || gameOverBlue
                        || gameOverGreen || gameOverPink || gameOverYellow) {
                    if (molesWhacked > highscore) {
                        molesWhacked = highscore;
                    } else if (molesWhacked == highscore) {
                        molesWhacked = highscore;
                    } else if (molesWhacked < highscore) {
                        highscore = highscore;
                    }

                    molesWhacked = 0;
                    molesMissed = 0;
                    activeMoleR = 0;
                    activeMoleB = 0;
                    activeMoleG = 0;
                    activeMoleP = 0;
                    activeMoleY = 0;

                    pickMoleColor();

                    gameOver = false;
                    gameOverRed = false;
                    gameOverBlue = false;
                    gameOverGreen = false;
                    gameOverPink = false;
                    gameOverYellow = false;

                    pickMole();
                }
                break;
            }
        }
        return true;
    }

calculateInSampleSize方法

public int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2
            // and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

decodeSampledBitmapFromResource Method

public Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

    //public Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
        //int width = image.getWidth();
        //int height = image.getHeight();
        //float scaleWidth = ((float) newWidth) / width;
        //float scaleHeight = ((float) newHeight) / height;
        // create a matrix for the manipulation
        //Matrix matrix = new Matrix();
        // resize the bit map
        //matrix.postScale(scaleWidth, scaleHeight);
        // recreate the new Bitmap
        //Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width,
                //height, matrix, false);
        //return resizedBitmap;
    //}

}

logcat的

07-18 11:06:39.532: D/skia(4516): --- allocation failed for scaled bitmap
07-18 11:06:39.562: E/InputEventReceiver(4516): Exception dispatching input event.
07-18 11:06:39.722: E/MessageQueue-JNI(4516): Exception in MessageQueue callback: handleReceiveCallback
07-18 11:06:39.842: E/MessageQueue-JNI(4516): java.lang.OutOfMemoryError
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:445)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:475)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at com.jellyproductions.holymoly.HolyMolyView$WhackAMoleThread.doTouchEvent(HolyMolyView.java:490)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at com.jellyproductions.holymoly.HolyMolyView.onTouchEvent(HolyMolyView.java:1702)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at  android.view.View.dispatchTouchEvent(View.java:7706)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.View.dispatchPointerEvent(View.java:7886)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.os.MessageQueue.nativePollOnce(Native Method)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.os.MessageQueue.next(MessageQueue.java:138)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.os.Looper.loop(Looper.java:123)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at android.app.ActivityThread.main(ActivityThread.java:5017)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at java.lang.reflect.Method.invokeNative(Native Method)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at java.lang.reflect.Method.invoke(Method.java:515)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-18 11:06:39.842: E/MessageQueue-JNI(4516):   at dalvik.system.NativeStart.main(Native Method)
07-18 11:06:39.842: D/AndroidRuntime(4516): Shutting down VM
07-18 11:06:39.852: W/dalvikvm(4516): threadid=1: thread exiting with uncaught exception (group=0xb3aafba8)
07-18 11:06:40.002: E/AndroidRuntime(4516): FATAL EXCEPTION: main
07-18 11:06:40.002: E/AndroidRuntime(4516): Process: com.jellyproductions.holymoly, PID: 4516
07-18 11:06:40.002: E/AndroidRuntime(4516): java.lang.OutOfMemoryError
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:445)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:475)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at com.jellyproductions.holymoly.HolyMolyView$WhackAMoleThread.doTouchEvent(HolyMolyView.java:490)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at com.jellyproductions.holymoly.HolyMolyView.onTouchEvent(HolyMolyView.java:1702)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.View.dispatchTouchEvent(View.java:7706)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.View.dispatchPointerEvent(View.java:7886)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.os.MessageQueue.nativePollOnce(Native Method)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.os.MessageQueue.next(MessageQueue.java:138)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.os.Looper.loop(Looper.java:123)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at java.lang.reflect.Method.invokeNative(Native Method)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at java.lang.reflect.Method.invoke(Method.java:515)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-18 11:06:40.002: E/AndroidRuntime(4516):     at dalvik.system.NativeStart.main(Native Method)

修改

我试图用这个改变doTouchEvent方法,但不幸的是它没有用。

boolean doTouchEvent(MotionEvent event) {
        synchronized (mySurfaceHolder) {
            int eventaction = event.getAction();
            int X = (int) event.getX();
            int Y = (int) event.getY();

            switch (eventaction) {

            case MotionEvent.ACTION_DOWN:
                if (!gameOver) {
                    fingerX = X;
                    fingerY = Y;
                    if (!onTitle && detectMoleHit()) {
                        whacking = true;
                        molesWhacked++;
                        if (molesWhacked < highscore) {
                            highscore = highscore;
                        } else if (molesWhacked == highscore) {
                            molesWhacked = highscore;
                        } else if (molesWhacked > highscore) {
                            highscore++;
                        }
                    }
                }
                break;

            case MotionEvent.ACTION_MOVE:
                break;

            case MotionEvent.ACTION_UP:
                if (onTitle || gameOver || gameOverRed || gameOverBlue
                        || gameOverGreen || gameOverPink || gameOverYellow) {

                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    int imageHeight = options.outHeight;
                    int imageWidth = options.outWidth;
                    String imageType = options.outMimeType;

                    backgroundImg = BitmapFactory
                            .decodeResource(myContext.getResources(),
                                    R.drawable.background);
                    backgroundImg = Bitmap.createScaledBitmap(
                            backgroundImg, screenW, screenH, true);

                    maskup = BitmapFactory.decodeResource(
                            myContext.getResources(), R.drawable.maskup);
                    maskmiddle = BitmapFactory
                            .decodeResource(myContext.getResources(),
                                    R.drawable.maskmiddle);
                    maskdown = BitmapFactory.decodeResource(
                            myContext.getResources(), R.drawable.maskdown);

                    whack = BitmapFactory.decodeResource(
                            myContext.getResources(), R.drawable.whack);

                    if (state == GameState.Red) {
                        molered = BitmapFactory.decodeResource(
                                myContext.getResources(), R.drawable.molered);
                        gameoverred = BitmapFactory.decodeResource(
                                myContext.getResources(),
                                R.drawable.gameoverred);

                        molered = Bitmap.createScaledBitmap(molered,
                                (int) (molered.getWidth() * scaleW),
                                (int) (molered.getHeight() * scaleH), true);
                        gameoverred = Bitmap.createScaledBitmap(gameoverred,
                                screenW, screenH, true);
                    }

                    if (state == GameState.Blue) {
                        moleblue = BitmapFactory.decodeResource(
                                myContext.getResources(), R.drawable.moleblue);
                        gameoverblue = BitmapFactory.decodeResource(
                                myContext.getResources(),
                                R.drawable.gameoverblue);

                        moleblue = Bitmap.createScaledBitmap(moleblue,
                                (int) (moleblue.getWidth() * scaleW),
                                (int) (moleblue.getHeight() * scaleH), true);
                        gameoverblue = Bitmap.createScaledBitmap(gameoverblue,
                                screenW, screenH, true);
                    }

                    if (state == GameState.Green) {
                        molegreen = BitmapFactory.decodeResource(
                                myContext.getResources(), R.drawable.molegreen);
                        gameovergreen = BitmapFactory.decodeResource(
                                myContext.getResources(),
                                R.drawable.gameovergreen);

                        molegreen = Bitmap.createScaledBitmap(molegreen,
                                (int) (molegreen.getWidth() * scaleW),
                                (int) (molegreen.getHeight() * scaleH), true);
                        gameovergreen = Bitmap.createScaledBitmap(
                                gameovergreen, screenW, screenH, true);
                    }

                    if (state == GameState.Pink) {
                        molepink = BitmapFactory.decodeResource(
                                myContext.getResources(), R.drawable.molepink);
                        gameoverpink = BitmapFactory.decodeResource(
                                myContext.getResources(),
                                R.drawable.gameoverpink);

                        molepink = Bitmap.createScaledBitmap(molepink,
                                (int) (molepink.getWidth() * scaleW),
                                (int) (molepink.getHeight() * scaleH), true);
                        gameoverpink = Bitmap.createScaledBitmap(gameoverpink,
                                screenW, screenH, true);
                    }

                    if (state == GameState.Yellow) {
                        moleyellow = BitmapFactory
                                .decodeResource(myContext.getResources(),
                                        R.drawable.moleyellow);
                        gameoveryellow = BitmapFactory.decodeResource(
                                myContext.getResources(),
                                R.drawable.gameoveryellow);

                        moleyellow = Bitmap.createScaledBitmap(moleyellow,
                                (int) (moleyellow.getWidth() * scaleW),
                                (int) (moleyellow.getHeight() * scaleH), true);
                        gameoveryellow = Bitmap.createScaledBitmap(
                                gameoveryellow, screenW, screenH, true);
                    }

                    scaleW = (float) screenW / (float) backgroundOrigW;
                    scaleH = (float) screenH / (float) backgroundOrigH;

                    maskup = Bitmap.createScaledBitmap(maskup,
                            (int) (maskup.getWidth() * scaleW),
                            (int) (maskup.getHeight() * scaleH), true);
                    maskmiddle = Bitmap.createScaledBitmap(maskmiddle,
                            (int) (maskmiddle.getWidth() * scaleW),
                            (int) (maskmiddle.getHeight() * scaleH), true);
                    maskdown = Bitmap.createScaledBitmap(maskdown,
                            (int) (maskdown.getWidth() * scaleW),
                            (int) (maskdown.getHeight() * scaleH), true);
                    whack = Bitmap.createScaledBitmap(whack,
                            (int) (whack.getWidth() * scaleW),
                            (int) (whack.getHeight() * scaleH), true);

                    onTitle = false;

                    pickMole();
                }
                whacking = false;
                if (gameOver || gameOverRed || gameOverBlue
                        || gameOverGreen || gameOverPink || gameOverYellow) {
                    if (molesWhacked > highscore) {
                        molesWhacked = highscore;
                    } else if (molesWhacked == highscore) {
                        molesWhacked = highscore;
                    } else if (molesWhacked < highscore) {
                        highscore = highscore;
                    }

                    molesWhacked = 0;
                    molesMissed = 0;
                    activeMoleR = 0;
                    activeMoleB = 0;
                    activeMoleG = 0;
                    activeMoleP = 0;
                    activeMoleY = 0;

                    pickMoleColor();

                    gameOver = false;
                    gameOverRed = false;
                    gameOverBlue = false;
                    gameOverGreen = false;
                    gameOverPink = false;
                    gameOverYellow = false;

                    pickMole();
                }
                break;
            }
        }
        return true;
    }

0 个答案:

没有答案