运行Android应用时,华为手机的FPS较低

时间:2019-03-29 14:30:03

标签: android huawei

一段时间以来,我一直在基于画布库制作Android游戏。我决定做一个无尽的跑步者。我在几种设备上进行了测试。

在我的旧Sony M4 Aqua,Huawei Y6和Samsung S8上,一切似乎都还不错,但是当我在Huawei P20 Lite上对其进行测试时,它只是加载启动屏幕,然后粉碎。

我意识到设置横向模式存在问题,因此必须将其放在manifest.xml中才能正常工作。现在可以正常工作,但是它的FPS很少。我检查了一下,问题出在背景课上,但仅在那部手机上。

package turtle.app;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;

import java.io.IOException;
import java.io.InputStream;

public class Background {

    private Bitmap background, background2;
    private Rect backSrc, backDes, backSrc2;

    private int speed;

    public Background(String path, int speed){
        this.speed = speed;
        background = getBackground(path);
        backSrc = new Rect(0, 0, Constants.SCREEN_WIDTH, background.getHeight());
        backDes = new Rect(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
        background2 = Bitmap.createBitmap(background, background.getWidth() - Constants.SCREEN_WIDTH, 0, Constants.SCREEN_WIDTH, background.getHeight());
        backSrc2 = new Rect(Constants.SCREEN_WIDTH, 0, Constants.SCREEN_WIDTH * 2, background.getHeight());
    }

    private static Bitmap getBackground(String strName) {
        AssetManager assetManager = Constants.context.getAssets();
        InputStream istr;
        Bitmap bitmap = null;
        try {
            istr = assetManager.open(strName);
            bitmap = BitmapFactory.decodeStream(istr);
        } catch (IOException e) {
            return null;
        }
        return bitmap;
    }

    public void changeSpeed(int newSpeed){
        speed = newSpeed;
    }

    public void update() {

        backSrc.left += speed;
        backSrc.right += speed;
        backSrc2.left += speed;
        backSrc2.right += speed;

        if(backSrc.right >= background.getWidth()) {
            backSrc2.left = 0;
            backSrc2.right = Constants.SCREEN_WIDTH;
            backSrc.left = - Constants.SCREEN_WIDTH;
            backSrc.right = 0;
        }
    }

    public void draw(Canvas canvas) {
        canvas.drawBitmap(background, backSrc, backDes, null);
        canvas.drawBitmap(background2, backSrc2, backDes, null);
    }
}

这肯定是我的游戏循环。

package turtle.app;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class MainThread extends Thread {
    private SurfaceHolder surfaceHolder;
    private GamePanel gamePanel;
    private boolean running;
    public static Canvas canvas;

    public void setRunning(boolean running) {
        this.running = running;
    }

    public MainThread(SurfaceHolder surfaceHolder, GamePanel gamePanel) {
        super();
        this.surfaceHolder = surfaceHolder;
        this.gamePanel = gamePanel;
    }

    public void run() {
        long lastTime = System.nanoTime();
        double amountOfTicks = 100.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int frames = 0;
        while (running) {
            canvas = null;
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            while (delta >= 1) {
                this.gamePanel.update();
                delta--;
            }
            try {
                canvas = this.surfaceHolder.lockCanvas();
                synchronized (surfaceHolder) {
                    this.gamePanel.draw(canvas);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (canvas != null) {
                    try {
                        surfaceHolder.unlockCanvasAndPost(canvas);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            frames++;
            if (System.currentTimeMillis() - timer > 1000) {
                timer += 1000;
                System.out.println("FPS: " + frames);
                frames = 0;
            }
        }
    }
}

在新的华为手机中有问题吗?还是我做错了事?

1 个答案:

答案 0 :(得分:0)

这表明使用了过多的资源。 也许您的游戏不能在低CPU设备上运行。

仍有希望:

  • 在Android Studio中进行开发时,您可以检查CPU正在运行的进程。为此,请在Profiler中单击(或在Mac中按Cmd + Shift + A并键入Profile)

enter image description here

  • 添加设备会话:

enter image description here

  • 现在您的应用程序正在运行。单击CPU并记录一个时间范围(假设是10秒),您的应用程序将在该时间范围内挣扎。

  • 停止录制,您将看到以下内容:

enter image description here

您看到底部的小块吗?这些功能被调用,例如,当您更新列表,设置一些文本,更改颜色等时。您可能会确定应用程序出了什么问题,例如,您在更改文本时,如果知道应该花费5毫秒或6毫秒,则花费300毫秒。

祝你好运。