跳帧和口吃应用AsyncTask

时间:2015-11-11 17:31:41

标签: java android lag

我正在制作一款需要碰撞检测的游戏。

应用程序很好,直到我添加此功能

public boolean CoinisCollisionDetectedWith(ImageView x_Image, ImageView y_Image,
            RelativeLayout.LayoutParams y_Params, RelativeLayout.LayoutParams x2_Params) {
        loc_CoinisCollisionWith = new int[8];

        x_Image.getLocationInWindow(loc_CoinisCollisionWith);
        rc1_CoinisCollisionWith = new Rect(loc_CoinisCollisionWith[0], loc_CoinisCollisionWith[1],
                loc_CoinisCollisionWith[0] + x_Image.getWidth(), loc_CoinisCollisionWith[1] + x_Image.getHeight());

        y_Image.getLocationInWindow(loc_CoinisCollisionWith);
        rc2_CoinisCollisionWith = new Rect(loc_CoinisCollisionWith[0], loc_CoinisCollisionWith[1],
                loc_CoinisCollisionWith[0] + y_Image.getWidth(), loc_CoinisCollisionWith[1] + y_Image.getHeight());
        if (Rect.intersects(rc2_CoinisCollisionWith, rc1_CoinisCollisionWith)) {

            collisionBounds_CoinisCollisionWith = getCollisionBounds(rc2_CoinisCollisionWith, rc1_CoinisCollisionWith);

            for (int i = collisionBounds_CoinisCollisionWith.left; i < collisionBounds_CoinisCollisionWith.right; i++) {
                for (int j = collisionBounds_CoinisCollisionWith.top; j < collisionBounds_CoinisCollisionWith.bottom; j++) {
                    if (i - (int) y_Params.leftMargin >= 0 && j - (int) y_Params.topMargin >= 0
                            && i - (int) y_Params.leftMargin >= 0 && j - (int) y_Params.topMargin >= 0
                            && i - (int) y_Params.leftMargin < x_Image.getWidth()
                            && j - (int) y_Params.topMargin < x_Image.getHeight()
                            && i - (int) y_Params.leftMargin < y_Image.getWidth()
                            && j - (int) y_Params.topMargin < y_Image.getHeight()) {

                        sprite1Pixel_CoinisCollisionWith = getBitmapPixel(x_Image, y_Params, i, j);
                        sprite2Pixel_CoinisCollisionWith = getBitmapPixel(y_Image, y_Params, i, j);
                        if (isFilled(sprite1Pixel_CoinisCollisionWith) && isFilled(sprite2Pixel_CoinisCollisionWith)) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }



private int getBitmapPixel(ImageView sprite, RelativeLayout.LayoutParams params, int i, int j) {
        sprite.buildDrawingCache();
        Bitmap bitmap = sprite.getDrawingCache();
        return bitmap.getPixel(i - (int) params.leftMargin, j - (int) params.topMargin);

    }

    private static Rect getCollisionBounds(Rect rect1, Rect rect2) {
        int left = (int) Math.max(rect1.left, rect2.left);
        int top = (int) Math.max(rect1.top, rect2.top);
        int right = (int) Math.min(rect1.right, rect2.right);
        int bottom = (int) Math.min(rect1.bottom, rect2.bottom);
        return new Rect(left, top, right, bottom);
    }

    private static boolean isFilled(int pixel) {
        return pixel != Color.TRANSPARENT;
    }

这些功能基本上给出了像素完美碰撞。

现在这些功能正常。我的意思是他们做了他们需要做的事。

但问题是该应用程序是口吃,基本上无法移动。

private class LongOperation extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            mUpdateUI_6 = new Runnable() {
                public void run() {

                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }

                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }

                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }

                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }

                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }

                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith(x, y, x_Params, y_Params)) {
                        x_Params.topMargin = 0 - ((height / 9) * 6);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }

                    if (CoinisCollisionDetectedWith2(x,y,x_Params, y_Params)) {
                        GameOver();
                    }
                    if (CoinisCollisionDetectedWith2(x,y,x_Params, y_Params)) {
                        GameOver();
                    }
                    if (CoinisCollisionDetectedWith2(x,y,x_Params, y_Params)) {
                        GameOver();
                    }
                    if (CoinisCollisionDetectedWith2(x,y,x_Params, y_Params)) {
                        GameOver();
                    }
                    if (CoinisCollisionDetectedWith2(x,y,x_Params, y_Params)) {
                        GameOver();
                    }
                    if (CoinisCollisionDetectedWith2(x,y,x_Params, y_Params)) {
                        GameOver();
                    }
                    if (CoinisCollisionDetectedWith2(x,y,x_Params, y_Params)) {
                        score_Number++;
                        score_View.setText(String.valueOf(score_Number));
                        x_Params.topMargin = 0 - ((height / 9) * 6);

                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith3(x,y,x_Params, y_Params)) {
                        score_Number++;
                        score_View.setText(String.valueOf(score_Number));
                        x_Params.topMargin = 0 - ((height / 9) * 12);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith3(x,y,x_Params, y_Params)) {
                        score_Number++;
                        score_View.setText(String.valueOf(score_Number));
                        x_Params.topMargin = 0 - ((height / 9) * 18);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith3(x,y,x_Params, y_Params)) {
                        score_Number++;
                        score_View.setText(String.valueOf(score_Number));
                        x_Params.topMargin = 0 - ((height / 9) * 24);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith3(x,y,x_Params, y_Params)) {
                        score_Number++;
                        score_View.setText(CoinisCollisionDetectedWith3(x,y,x_Params, y_Params));
                        x_Params.topMargin = 0 - ((height / 9) * 30);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }
                    if (CoinisCollisionDetectedWith3(x, y, x_Params)) {
                        score_Number++;
                        score_View.setText(CoinisCollisionDetectedWith3(x,y,x_Params, y_Params));
                        x_Params.topMargin = 0 - ((height / 9) * 36);
                        x_Params.leftMargin = new Random().nextInt(width - x_Params.width);
                    }

                    loop_Handler_6.postDelayed(mUpdateUI_6, 95);

                }
            };
            loop_Handler_6.post(mUpdateUI_6);
            return "Executed";
        }

“CoinisCollisionDetectedWith2”和“CoinisCollisionDetectedWith3”功能与“CoinisCollisionDetectedWith”具有相同的方法,只是具有不同的视图。

有人知道如何解决它吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

使用Traceview(更多信息here)来分析您的代码并确定哪些代码很慢。优化慢速部分:)