首页 > 解决方案 > 运行安卓应用时华为手机帧率低

问题描述

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

在我的旧索尼 M4 Aqua、华为 Y6 和三星 S8 上,一切似乎都很好,但是当我在华为 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;
            }
        }
    }
}

是华为新手机的问题还是我做错了什么?

标签: androidhuawei-mobile-services

解决方案


这是使用过多资源的标志。也许您的游戏不应该在低 CPU 设备上运行。

还是有希望的:

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

在此处输入图像描述

  • 添加设备的会话:

在此处输入图像描述

  • 现在您的应用程序正在运行。单击 CPU 并记录您的应用程序遇到困难的时间范围(例如 10 秒)。

  • 停止录制,你会得到这样的东西:

在此处输入图像描述

你看到底部的小块了吗?这些是正在调用的函数,例如,当您更新列表、设置一些文本、更改颜色等时。您可能会识别出您的应用程序中出了什么问题,例如,您正在更改一个文本,而当您知道它应该需要 5 毫秒或 6 毫秒时,它需要 300 毫秒。

祝你好运。


推荐阅读