3

我一直在开发一个简单的 2D 无尽跑酷安卓游戏。我选择了画布路线,因为 Open GL 的学习曲线似乎相对较高。到目前为止,我一直在关注本教程: http ://www.javacodegeeks.com/tutorials/android-tutorials/android-game-tutorials/

并且与游戏循环完全相同: http ://www.javacodegeeks.com/2011/07/android-game-development-game-loop.html

我的问题是我的游戏在我的 Galaxy Nexus 上以 60 FPS 的速度运行,但是当我将游戏放到我女朋友的 Nexus 4 上时,FPS 下降到 40 并且游戏性能非常不稳定。和我表弟的 Galaxy S2 一样。

我目前正在使用:

MainGamePanel.java

getHolder().setFormat(PixelFormat.RGBA_8888);

我听说 RGB_565 提供了更好的性能,但这实际上将我的设备上的 FPS 降至 40,并且 FPS 与其他设备相同

启用硬件加速,但似乎没有多大作用:

AndroidManifest

android:hardwareAccelerated="true"

在我的 MainGamePanel 中,我实例化了在线程启动之前将被绘制到画布上的所有对象:

public MainGamePanel(Context context) {
    super(context);
    getHolder().addCallback(this);
    getHolder().setFormat(PixelFormat.RGBA_8888);
    initScreenMetrics(context);
    initGameMetrics();

    BitmapOptions options = new BitmapOptions();
    drillBit = new Drill(getResources(), 0);
    drillBit.setX((screenWidth - drillBit.getBitmap().getWidth()) / 2);
    drillBackground = new DrillBackground(getResources(), drillBit.getX(), drillBit.getY());
    diamond = new Interactable(BitmapFactory.decodeResource(getResources(),R.drawable.diamond, options.getOptions()), screenWidth/2, screenHeight+30);
    potion = new Interactable(BitmapFactory.decodeResource(getResources(), R.drawable.potion, options.getOptions()), screenWidth/4, screenHeight+230);
    oil = new Interactable(BitmapFactory.decodeResource(getResources(), R.drawable.oil_1, options.getOptions()), 3*screenWidth/4, screenHeight+450);
    powerUp = new Interactable(BitmapFactory.decodeResource(getResources(), R.drawable.power_up, options.getOptions()), 
            3*screenWidth/4, screenHeight+130);

    background = new Background(getResources());

    thread = new MainThread(getHolder(), this);
    setFocusable(true);
}

为了给drillBit对象设置动画,我在构造函数中有4个位图,每次游戏循环时,我都会关闭位图(想不出另一种方法来实现这一点):

钻头.java

public Drill(Resources resource, int x) {
    BitmapOptions options = new BitmapOptions();
    bitmap1 = BitmapFactory.decodeResource(resource, R.drawable.drill_1, options.getOptions());
    bitmap2 = BitmapFactory.decodeResource(resource, R.drawable.drill_2, options.getOptions());
    bitmap3 = BitmapFactory.decodeResource(resource, R.drawable.drill_3, options.getOptions());
    bitmap4 = BitmapFactory.decodeResource(resource, R.drawable.drill_4, options.getOptions());
    currentBitmap = bitmap1;
//other stuff
}

钻孔中的绘图功能:

        if (currentBitmap == bitmap1) {
        currentBitmap = bitmap2;
    }
    else if (currentBitmap == bitmap2) {
        currentBitmap = bitmap3;
    }
    else if (currentBitmap == bitmap3) {
        currentBitmap = bitmap4;
    }
    else {
        currentBitmap = bitmap1;
    }
    canvas.draw(currentbitmap, x, y, null);

任何帮助表示赞赏,谢谢!

4

1 回答 1

0

之所以会这样,是因为 Canvas 根据像素绘制所有内容。不同分辨率设备中的像素不同。所以当你执行 (screenHeight+30) 时会在不同的设备上绘制不同的。所以你不能直接写+30,你需要在设备相关像素中转换30。

我使用这种方法来转换静态值

public int convertSizeToDeviceDependent(int value,Context mContext) {
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
        return ((dm.densityDpi * value) / 160);
    }

在哪里使用

diamond = new Interactable(BitmapFactory.decodeResource(getResources(),R.drawable.diamond, options.getOptions()), screenWidth/2, screenHeight+convertSizeToDeviceDependent(30,context));

将此方法应用于您在此行中使用硬编码值的所有地方

diamond = new Interactable(BitmapFactory.decodeResource(getResources(),R.drawable.diamond, options.getOptions()), screenWidth/2, screenHeight+30);
    potion = new Interactable(BitmapFactory.decodeResource(getResources(), R.drawable.potion, options.getOptions()), screenWidth/4, screenHeight+230);
    oil = new Interactable(BitmapFactory.decodeResource(getResources(), R.drawable.oil_1, options.getOptions()), 3*screenWidth/4, screenHeight+450);
    powerUp = new Interactable(BitmapFactory.decodeResource(getResources(), R.drawable.power_up, options.getOptions()), 
            3*screenWidth/4, screenHeight+130);
于 2013-09-17T05:43:13.357 回答