我一直致力于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个位图,每次游戏循环时,我都会关闭位图(想不到另一种方法来实现这一点):
drill.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);
感谢任何帮助,谢谢!
答案 0 :(得分:0)
这是因为Canvas基于pixel绘制了所有内容.Pixel在不同的分辨率设备中是不同的。所以当你执行(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);