Java Android Canvas的性能与其他设备相比非常不一致
我一直在开发一款简单的2D版无止境跑步机器人游戏。我选择了帆布路线,因为OpenGL的学习曲线似乎相对较高。到目前为止,我一直在学习本教程: 游戏循环完全相同: 我的问题是我的游戏在我的Galaxy Nexus上以60 FPS的速度运行,但是当我把游戏放到我女朋友的Nexus 4上时,FPS下降到40 FPS,游戏性能非常不稳定。我表哥的星系S2也一样 我目前正在使用:Java Android Canvas的性能与其他设备相比非常不一致,java,android,bitmap,android-canvas,Java,Android,Bitmap,Android Canvas,我一直在开发一款简单的2D版无止境跑步机器人游戏。我选择了帆布路线,因为OpenGL的学习曲线似乎相对较高。到目前为止,我一直在学习本教程: 游戏循环完全相同: 我的问题是我的游戏在我的Galaxy Nexus上以60 FPS的速度运行,但是当我把游戏放到我女朋友的Nexus 4上时,FPS下降到40 FPS,游戏性能非常不稳定。我表哥的星系S2也一样 我目前正在使用: MainGamePanel.java getHolder().setFormat(PixelFormat.RGBA_888
MainGamePanel.java
getHolder().setFormat(PixelFormat.RGBA_8888);
我听说RGB_565提供了更好的性能,但这实际上将我的设备上的FPS降低到了40,并且FPS与其他设备相同
启用硬件加速,但似乎没有什么作用:雄激素单
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);
}
为了给钻取对象设置动画,我在构造器中有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);
感谢您的帮助,谢谢 之所以会这样,是因为画布基于像素绘制所有内容。像素在不同分辨率的设备中是不同的。因此,当您执行(屏幕高度+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);
我已经在我能找到的任何有像素值的地方添加了这个函数,我获得了大约5fps的速度,但是Nexus4仍然比我的GalaxyNexus慢很多。还有其他想法吗?