Java - Android应用程序跳过400多帧

 手机用户2602922607 发布于 2023-01-07 11:23

当我运行我的应用程序时,加载需要很长时间.当它开始时它是相当滞后的.当我运行我的代码时,我在logcat中收到此消息:

06-28 03:36:22.969: I/Choreographer(1506): Skipped 453 frames!  The application may be doing too much work on its main thread.

我在想我的主文件中有某种代码会减慢整个事情的速度,因为它没有在最长的时间内执行此操作.此外,它跳过453帧意味着一些事情不对.

这是我的代码,大约1000行所以如果有人想通读它会很棒但是如果没有,如果我能引导到解决这个问题的方向很好.

public class PlayScreen extends View {

// Constructor
public PlayScreen(Context context) {
    super(context);

    /**
     * Instantiate all variables.
     */

    playerBounds = new RectF();
    rowBlock = new RectF();
    rowBlock2 = new RectF();
    startPlaying = new RectF();
    continuePlaying = new RectF();
    rowGlow = new RectF();
    rowGlow2 = new RectF();
    menuBox = new RectF();
    continueplayingBounds = new RectF();

    meteorBounds = new RectF();
    pointLossBounds = new RectF();
    pointGainBounds = new RectF();
    speedBounds = new RectF();

    titleBounds = new RectF();
    playBounds = new RectF();

    doublePointsBounds = new RectF();
    bulletBounds = new RectF();
    ghostWallBounds = new RectF();
    wideGapsBounds = new RectF();


    ///// Colors /////

    colorGreen = new Paint();
    colorBlack = new Paint();
    colorWhite = new Paint();
    colorBlue = new Paint();
    colorYellow = new Paint();
    colorRed = new Paint();
    colorCyan = new Paint();
    colorMagenta = new Paint();
    colorOrange = new Paint();


    glowGreen = new Paint();
    glowWhite = new Paint();
    glowBlue = new Paint();
    glowYellow = new Paint();
    glowRed = new Paint();
    glowCyan = new Paint();
    glowMagenta = new Paint();
    glowOrange = new Paint();
    glowBlack = new Paint();

    translucentGrey = new Paint();

    transparent = new Paint();
    boostPaint = new Paint();
    countPaint = new Paint();

    align = null;

    // Set my basic color's
    colorGreen.setColor(Color.rgb(17, 255, 0));        
    colorBlack.setColor(Color.rgb(0, 0, 0));
    colorWhite.setColor(Color.rgb(255, 255, 255));
    colorBlue.setColor(Color.rgb(0, 0, 255));
    colorYellow.setColor(Color.rgb(243, 243, 21));
    colorRed.setColor(Color.rgb(255, 0, 0));
    colorCyan.setColor(Color.rgb(51, 255, 255));
    colorMagenta.setColor(Color.rgb(255, 0, 255));
    colorOrange.setColor(Color.rgb(255, 119, 0));
    transparent.setColor(Color.TRANSPARENT);

    // Set glow colors
    glowGreen.setColor(Color.argb(150, 17, 255, 0)); 
    glowWhite.setColor(Color.argb(150, 255, 255, 255));
    glowBlue.setColor(Color.argb(150, 0, 0, 255));
    glowYellow.setColor(Color.argb(150, 243, 243, 21));
    glowRed.setColor(Color.argb(150, 255, 0, 0));
    glowCyan.setColor(Color.argb(150, 51, 255, 255));
    glowMagenta.setColor(Color.argb(150, 255, 0, 255));
    glowOrange.setColor(Color.argb(150, 255, 119, 0));
    glowBlack.setColor(Color.argb(100, 0, 0, 0));

    translucentGrey.setColor(Color.argb(100, 205, 201, 201));

    lobsterFont = Typeface.createFromAsset(Assets, "fonts/Lobster.otf");

    countPaint.setColor(Color.rgb(255, 255, 255));
    countPaint.setTextSize(60);
    countPaint.setTextAlign(align.RIGHT);
    countPaint.setTypeface(lobsterFont);

    boostPaint.setColor(Color.rgb(255, 255, 255));
    boostPaint.setTextSize(40);
    boostPaint.setTextAlign(align.CENTER);
    boostPaint.setTypeface(lobsterFont);


    ///// End Colors /////

    timer = new Timer();


    random = new Random();
    Playing = false;
    event = random.nextInt(4);
    boost = random.nextInt(4);

    clock();



    box = BitmapFactory.decodeResource(getResources(), R.drawable.box);

    play = BitmapFactory.decodeResource(getResources(), R.drawable.start);
    title = BitmapFactory.decodeResource(getResources(), R.drawable.logo);


    ballBlue = BitmapFactory.decodeResource(getResources(), R.drawable.ballblue);
    ballRed = BitmapFactory.decodeResource(getResources(), R.drawable.ballred);
    ballYellow = BitmapFactory.decodeResource(getResources(), R.drawable.ballyellow);
    ballGreen = BitmapFactory.decodeResource(getResources(), R.drawable.ballgreen);

    dp = BitmapFactory.decodeResource(getResources(), R.drawable.dp);
    wg = BitmapFactory.decodeResource(getResources(), R.drawable.widegaps);
    rocket = BitmapFactory.decodeResource(getResources(), R.drawable.rocket);
    ghost = BitmapFactory.decodeResource(getResources(), R.drawable.ghost);



    // Get window size and save it to screenWidth and screenHeight.
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size); 
    screenWidth = size.x;
    screenHeight = size.y;

    density = getResources().getDisplayMetrics().density;

    rowSpace = (int) (100 * density);

    boostdiam = (int) (25 * density);

    int nor = screenHeight / rowSpace; // Number of rows to be displayed.

    /**
     * Generate rows and gaps at set intervals.
     * Chooses a random color for each row.
     */
    for (int row = 0; row < nor + 1; row ++) { 
        final int gap = (int) (random.nextInt(maxGap) + minGap * density);
        final int block = random.nextInt(screenWidth - gap);

        blockY[row] = screenHeight * 1/2 + row * rowSpace; // Positions first block at center screen.
        blockX1[row] = block;
        blockX2[row] = block + gap; 
        blockcolor[row] = random.nextInt(8);

        passedrow[row] = false;

        halfgap = (float) (blockX2[0] + blockX1[0]) / 2; // Used for centering start button to first gap.           

    }


    randomEvent = System.currentTimeMillis() + random.nextInt(EventTimeMax) + EventTimeMin;
    randomBoost = System.currentTimeMillis() + random.nextInt(BoostTimeMax) + BoostTimeMin;
    eventRandomX = random.nextInt(screenWidth);

    meteorBounds.set(eventRandomX * density, -50 * density, eventRandomX + 50 * density, 0 * density);
    pointLossBounds.set(eventRandomX * density, -50 * density, eventRandomX + 50 * density, 0 * density);
    pointGainBounds.set(eventRandomX * density, -50 * density, eventRandomX + 50 * density, 0 * density);
    speedBounds.set(eventRandomX * density, -50 * density, eventRandomX + 50 * density, 0 * density);


    System.out.println(density);

    scoreText = score.toString();

}

// Called back to draw the view. Also called by invalidate().
@Override
protected void onDraw(Canvas canvas) {




    // Once game is started and paused is set to true, pause all counters.          
    if (Playing == true && rocketboostOn == false) { 
        scrollSpeed = oldSpeed;
        scrollSpeed -= scrollInterval;
        oldSpeed = scrollSpeed;
    }

    int rowBlockHeight = screenHeight / 32;

    playerBounds.set(playerX - playerRadius * density, (float) playerY - playerRadius * density, playerX + playerRadius * density, (float) playerY + playerRadius * density);

    int nor = screenHeight / rowSpace;

    for (int row = 0; row < nor + 1; row ++) {              
        blockY[row] = blockY[row] + scrollSpeed;

        rowBlock.set(0, (float) blockY[row],(float) blockX1[row], (float) blockY[row] + rowBlockHeight);
        rowBlock2.set((float) blockX2[row], (float)blockY[row], (float) screenWidth, (float) blockY[row] + rowBlockHeight);

        rowGlow.set(-20, (float) blockY[row] - 10, (float) blockX1[row] + 10, (float) blockY[row] + rowBlockHeight + 10);
        rowGlow2.set((float) blockX2[row] - 10, (float) blockY[row] - 10, (float) screenWidth + 20, (float) blockY[row] + rowBlockHeight + 10);

        if (blockY[row] < screenHeight) {

            if (blockcolor[row] == 0 && gwboostOn == false) {
                canvas.drawRoundRect(rowGlow, 4 * density, 4 * density, glowGreen);
                canvas.drawRoundRect(rowGlow2, 4 * density, 4 * density, glowGreen);                    
                if (rocketboostOn == false) {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, colorBlack);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, colorBlack);
                } else {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, translucentGrey);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, translucentGrey);
                }
            }
            if (blockcolor[row] == 1 && gwboostOn == false) {
                canvas.drawRoundRect(rowGlow, 4 * density, 4 * density, glowRed);
                canvas.drawRoundRect(rowGlow2, 4 * density, 4 * density, glowRed); 
                if (rocketboostOn == false) {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, colorBlack);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, colorBlack);
                } else {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, translucentGrey);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, translucentGrey);
                }
            }
            if (blockcolor[row] == 2 && gwboostOn == false) {
                canvas.drawRoundRect(rowGlow, 4 * density, 4 * density, glowBlue);
                canvas.drawRoundRect(rowGlow2, 4 * density, 4 * density, glowBlue);
                if (rocketboostOn == false) {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, colorBlack);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, colorBlack);
                } else {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, translucentGrey);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, translucentGrey);
                }          
            }
            if (blockcolor[row] == 3 && gwboostOn == false) {
                canvas.drawRoundRect(rowGlow, 4 * density, 4 * density, glowWhite);
                canvas.drawRoundRect(rowGlow2, 4 * density, 4 * density, glowWhite);
                if (rocketboostOn == false) {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, colorBlack);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, colorBlack);
                } else {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, translucentGrey);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, translucentGrey);
                }                   
            }
            if (blockcolor[row] == 4 && gwboostOn == false) {
                canvas.drawRoundRect(rowGlow, 4 * density, 4 * density, glowYellow);
                canvas.drawRoundRect(rowGlow2, 4 * density, 4 * density, glowYellow);
                if (rocketboostOn == false) {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, colorBlack);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, colorBlack);
                } else {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, translucentGrey);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, translucentGrey);
                }                    
            }     
            if (blockcolor[row] == 5 && gwboostOn == false) {
                canvas.drawRoundRect(rowGlow, 4 * density, 4 * density, glowCyan);
                canvas.drawRoundRect(rowGlow2, 4 * density, 4 * density, glowCyan);
                if (rocketboostOn == false) {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, colorBlack);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, colorBlack);
                } else {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, translucentGrey);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, translucentGrey);
                }                   
            }   
            if (blockcolor[row] == 6 && gwboostOn == false) {
                canvas.drawRoundRect(rowGlow, 4 * density, 4 * density, glowMagenta);
                canvas.drawRoundRect(rowGlow2, 4 * density, 4 * density, glowMagenta);
                if (rocketboostOn == false) {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, colorBlack);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, colorBlack);
                } else {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, translucentGrey);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, translucentGrey);
                }               
            }
            if (blockcolor[row] == 7 && gwboostOn == false) {
                canvas.drawRoundRect(rowGlow, 4 * density, 4 * density, glowOrange);
                canvas.drawRoundRect(rowGlow2, 4 * density, 4 * density, glowOrange);
                if (rocketboostOn == false) {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, colorBlack);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, colorBlack);
                } else {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, translucentGrey);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, translucentGrey);
                }   
            }
            if (gwboostOn == true) {
                canvas.drawRoundRect(rowBlock, 3 * density, 3 * density, translucentGrey);
                canvas.drawRoundRect(rowBlock2, 3 * density, 3 * density, translucentGrey);
            }
        }


        if (blockY[row] < 0) {

            final int gap = (int) (random.nextInt(maxGap) + minGap * density);
            final int block = random.nextInt(screenWidth - gap);

            if (row == 0) {
                blockY[row] = blockY[nor] + rowSpace + rowBlockHeight;
            } else {
                blockY[row] = blockY[row - 1] + rowSpace;
            }

            blockX1[row] = block;
            blockX2[row] = block + gap;
            blockcolor[row] = random.nextInt(8);
            passedrow[row] = false;

        }

        if (passedrow[row] == false) {

            if (playerBounds.top > blockY[row]) {
                passedrow[row] = true;
                score += 1;
                scoreText = score.toString();

                if (dpboostOn == true) score += 2;

            }

        }


        if (playerBounds.bottom > rowBlock.top && playerBounds.top < rowBlock.bottom && (playerBounds.left < blockX1[row] || playerBounds.right > blockX2[row]) && rocketboostOn == false && gwboostOn == false) {
                youLose();                  
        }

        if (doublePointsOn == true && dpboostOn == false) {
            doublePointsBounds.set(boostIconX, (float) blockY[boostIconRow] - boostdiam - 20, doublePointsBounds.left + boostdiam, doublePointsBounds.top + boostdiam);

            canvas.drawBitmap(dp, null, doublePointsBounds, null);

            if (hasCollided(doublePointsBounds.left, doublePointsBounds.top, doublePointsBounds.right, doublePointsBounds.bottom)) {    
                boostTime = 10;
                dpboostOn = true;                       
            }

            if (doublePointsBounds.bottom < 0) {
                doublePointsOn = false;
                randomBoost = System.currentTimeMillis() + random.nextInt(BoostTimeMax) + BoostTimeMin;
            }
        }

        if(dpboostOn == true && rocketboostOn == false && wgboostOn == false && gwboostOn == false) {
            scoreInt = 2;

            if(boostTime < 1) {
                dpboostOn = false;
                doublePointsOn = false;
                randomBoost = System.currentTimeMillis() + random.nextInt(BoostTimeMax) + BoostTimeMin;
                scoreInt = 1;

            } else {
                canvas.drawText("Double Points", screenWidth / 2, 25 * density, boostPaint);
                canvas.drawText(boostTime.toString(), screenWidth / 2, 55 * density, countPaint);
            }
        }





        if (rocketOn == true && rocketboostOn == false) {

            bulletBounds.set(boostIconX, (float) blockY[boostIconRow] - boostdiam - 20, bulletBounds.left + boostdiam, bulletBounds.top + boostdiam);
            canvas.drawBitmap(rocket, null, bulletBounds, null);

            if (hasCollided(bulletBounds.left, bulletBounds.top, bulletBounds.right, bulletBounds.bottom)) {    
                boostTime = 5;
                oldSpeed = scrollSpeed;

                rocketboostOn = true;                       
            }

            if (bulletBounds.bottom < 0) {
                rocketOn = false;
                randomBoost = System.currentTimeMillis() + random.nextInt(BoostTimeMax) + BoostTimeMin; 
            }

        }

        if (rocketboostOn == true && dpboostOn == false && wgboostOn == false && gwboostOn == false) {

            scrollSpeed = oldSpeed * 1.5;


            if(boostTime < 1) {
                rocketboostOn = false;
                rocketOn = false;
                randomBoost = System.currentTimeMillis() + random.nextInt(BoostTimeMax) + BoostTimeMin;
                scrollSpeed = oldSpeed;

            } else {
                canvas.drawText("Rocket", screenWidth / 2, 25 * density, boostPaint);
                canvas.drawText(boostTime.toString(), screenWidth / 2, 55 * density, countPaint);
            }
        }



        if (widegapsOn == true && wgboostOn == false) {

            wideGapsBounds.set(boostIconX, (float) blockY[boostIconRow] - boostdiam - 20, wideGapsBounds.left + boostdiam, wideGapsBounds.top + boostdiam);
            canvas.drawBitmap(wg, null, wideGapsBounds, null);

            if (hasCollided(wideGapsBounds.left, wideGapsBounds.top, wideGapsBounds.right, wideGapsBounds.bottom)) {    
                boostTime = 10;
                wgboostOn = true;                       
            }

            if (wideGapsBounds.top < 0) {
                widegapsOn = false;
                randomBoost = System.currentTimeMillis() + random.nextInt(BoostTimeMax) + BoostTimeMin; 
            }

        }

        if (wgboostOn == true && rocketboostOn == false && dpboostOn == false && gwboostOn == false) {                      

            minGap = 200;
            maxGap = 250;

            if(boostTime < 1) {
                wgboostOn = false;
                widegapsOn = false;

                minGap = 100;
                maxGap = 150;
                randomBoost = System.currentTimeMillis() + random.nextInt(BoostTimeMax) + BoostTimeMin;

            } else {
                canvas.drawText("Wide Gaps", screenWidth / 2, 25 * density, boostPaint);
                canvas.drawText(boostTime.toString(), screenWidth / 2, 55 * density, countPaint);
            }
        }


        if (ghostWallOn == true && gwboostOn == false) {

            ghostWallBounds.set(boostIconX, (float) blockY[boostIconRow] - boostdiam - 20, ghostWallBounds.left + boostdiam, ghostWallBounds.top + boostdiam);
            canvas.drawBitmap(ghost, null, ghostWallBounds, null);

            if (hasCollided(ghostWallBounds.left, ghostWallBounds.top, ghostWallBounds.right, ghostWallBounds.bottom)) {    
                gwboostOn = true;
                boostTime = 10;

            }

            if (ghostWallBounds.top < 0) {
                ghostWallOn = false;
                randomBoost = System.currentTimeMillis() + random.nextInt(BoostTimeMax) + BoostTimeMin;
            }


        }

        if(gwboostOn == true && rocketboostOn == false && wgboostOn == false && dpboostOn == false) {



            if(boostTime < 1) {
                gwboostOn = false;
                ghostWallOn = false;
                randomBoost = System.currentTimeMillis() + random.nextInt(BoostTimeMax) + BoostTimeMin;

            } else {
                canvas.drawText("Ghost Walls", screenWidth / 2, 25 * density, boostPaint);
                canvas.drawText(boostTime.toString(), screenWidth / 2, 55 * density, countPaint);
            }
        }

    } // End of main FOR loop
    //canvas.drawText(scrollSpeed.toString(), 200, 200, scoreColor);

    if (score < 0) {
        youLose();
    }

    int playHeight=87; int playWidth=96;
    playBounds.set(halfgap - playWidth / 2 * density, (float) (blockY[0] - rowSpace), halfgap + playWidth / 2 * density, playBounds.top + playHeight * density);

    playerBounds.set(playerX - playerRadius * density,(float) playerY - playerRadius * density, playerX + playerRadius * density, (float) playerY + playerRadius * density);

    canvas.drawOval(playerBounds, transparent); // Draw player

    if (Playing == true) canvas.drawText(scoreText, 350 * density, 30 * density, countPaint);


    int titleWidth = 256; int titleHeight = 76; 
    titleBounds.set(screenWidth / 2 - titleWidth / 2 * density, 10 * density, titleBounds.left + titleWidth * density, titleBounds.top + titleHeight * density);


    if (Playing == false) {
        canvas.drawBitmap(play, null, playBounds, null);    
        canvas.drawBitmap(title, null, titleBounds, null);
    }   


    //change to 10          // Events // 
    if (score > 0 && System.currentTimeMillis() > randomEvent) {


        if (meteorOn == false && pointLossOn == false && pointGainOn == false && speedEventOn == false) {


            event = random.nextInt(4);              
            eventY = random.nextInt(15) + 5;

            if (event == 0) {
                meteorOn = true;
            } else if (event == 1) {
                pointLossOn = true;
            } else if (event == 2) {
                pointGainOn = true;
            } else if (event == 3) {
                speedEventOn = true;
            }                   

        }



        if (meteorOn == true) {
            MeteorEvent(5, eventY);
            if (meteorBounds.centerX() < screenWidth && meteorBounds.centerY() < screenHeight && meteorBounds.centerX() > 0) {
                canvas.drawBitmap(ballRed, null, meteorBounds, null);
            }   
        }

        if (pointLossOn == true) {
            PointLossEvent(5, eventY);
            if (pointLossBounds.centerX() < screenWidth && pointLossBounds.centerY() < screenHeight && pointLossBounds.centerX() > 0) {
                canvas.drawBitmap(ballBlue, null, pointLossBounds, null);
            }
        }
        if (pointGainOn == true) {
            PointGainEvent(5, eventY);
            if (pointGainBounds.centerX() < screenWidth && pointGainBounds.centerY() < screenHeight && pointGainBounds.centerX() > 0) {
                canvas.drawBitmap(ballGreen, null, pointGainBounds, null);
            }
        }
        if (speedEventOn == true) {
            SpeedEvent(5, eventY);
            if (speedBounds.centerX() < screenWidth && speedBounds.centerY() < screenHeight && speedBounds.centerX() > 0) {
                canvas.drawBitmap(ballYellow, null, speedBounds, null);
            }
        }



    } // End of events //


    // change to 20
    if (score > 0 && System.currentTimeMillis() > randomBoost) {

        if (doublePointsOn == false && rocketOn == false && widegapsOn == false && ghostWallOn == false) {

            boost = random.nextInt(4); 

            System.out.println(boost);
            //boostIconRow = random.nextInt(nor) + nor / 2;
            boostIconRow = 1;
            boostIconX = random.nextInt((int) (screenWidth - 50 * density)) + 50;

            if (boost == 0) {
                doublePointsOn = true;
            } else if (boost == 1) {
                rocketOn = true;
            } else if (boost == 2) {
                widegapsOn = true;
            } else if (boost == 3) {
                ghostWallOn = true;
            }

            System.out.println(ghostWallOn);
        }

    }

    //canvas.drawBitmap(play, null, playBounds, null);        


    if(score > 50) {
        EventTimeMax = 4000;
        EventTimeMin = 2000;         
    } else if (score > 100) {   
        EventTimeMax = 3000;
        EventTimeMin = 1000;    
    } else if (score > 250) {
        EventTimeMax = 2000; 
        EventTimeMin = 1000;
    }



    // Delay    
    try { Thread.sleep(10); }
    catch (InterruptedException e){}
    invalidate(); // Force a re-draw
}

public void clock() {

    timer.schedule(new TimerTask() {

        @Override
        public void run() {

            if (boostTime > 0) {
                boostTime -= 1;
            } 

        }

    }, 0, 1000);   

}

// Called back when the view is first created or its size changes.
@Override public void onSizeChanged(int w, int h, int oldW, int oldH) {

    // Set the movement bounds for the ball
    xViewMax = w - 1;
    yViewMax = h - 1;

}


public void highscoreSaver() {

    if(finalScore > MainActivity.prefs.getInt("HighScore", 0)) {
        MainActivity.prefsEditor = MainActivity.prefs.edit();
        MainActivity.prefsEditor.putInt("HighScore", score);
        MainActivity.prefsEditor.commit();

    }

}

public void youLose() {

    View LoseScreen = new LoseScreen(getContext());
    ViewGroup parent = (ViewGroup) getParent();
    finalScore = score;

    highscoreSaver();

    timer.cancel();

    parent.addView(LoseScreen);
    parent.removeView(this);
    parent.setBackgroundResource(R.drawable.bg);

}

public void MeteorEvent(float slopeX, float slopeY) {

    if (eventRandomX < screenWidth / 2) {
    meteorBounds.left += slopeX; meteorBounds.right += slopeX;
    } else {
    meteorBounds.left -= slopeX; meteorBounds.right -= slopeX;
    }
    meteorBounds.top += slopeY; meteorBounds.bottom += slopeY;

    if (!(meteorBounds.centerX() < screenWidth && meteorBounds.centerY() < screenHeight && meteorBounds.centerX() > 0)) {

        randomEvent = System.currentTimeMillis() + random.nextInt(EventTimeMax) + EventTimeMin;
        eventRandomX = random.nextInt(screenWidth - 20) + 20;
        meteorBounds.set(eventRandomX * density, -50 * density, eventRandomX + 50 * density, 0 * density);  
        meteorOn = false;
    }   
    if (hasCollided(meteorBounds.left, meteorBounds.top, meteorBounds.right, meteorBounds.bottom))  
        youLose();
}


public boolean hasCollided (float left, float top, float right, float bottom) {

    if (((playerBounds.bottom > top && playerBounds.bottom < bottom) || (playerBounds.top < bottom && playerBounds.top > top)) 
        && ((playerBounds.left > left && playerBounds.left < right) || (playerBounds.right < right && playerBounds.right > left))) {

        return true;        
    } else {    
        return false;
    }
}

public boolean onTouchEvent(MotionEvent e) {

    // Get finger's X and Y position
    float x = e.getX();
    float y = e.getY();

    if(x > playBounds.left || x < playBounds.right && y < playBounds.bottom && y > playBounds.top) {

        Playing = true;         
    }


    if (Playing == true) {

                switch (e.getAction()) {

                case MotionEvent.ACTION_MOVE:
                    playerX = x;
                    playerY = y;
                    break;

                case MotionEvent.ACTION_UP:
                    youLose();
                }
            }

    return true;
}

}

Kaustubh.. 5

你在主线程上做了很多工作,因此android正在跳过框架,你的应用程序正在慢慢打开.使用线程或AsynkTask进行后台工作.看看这个和这个

1 个回答
  • 你在主线程上做了很多工作,因此android正在跳过框架,你的应用程序正在慢慢打开.使用线程或AsynkTask进行后台工作.看看这个和这个

    2023-01-07 11:26 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有