我设计了一款可以完美缩放并适合手机屏幕的游戏。但是在模拟器和我测试过的其他设备上,它可能只占屏幕的20-50%,因为这些屏幕更大。如何缩放每个设备专用的画布?
我尝试根据页面的尺寸除以新屏幕的尺寸来确定页面的宽度和高度,并更改矩形,文本等的位置,以得出更大的尺寸但是它将不接受十进制/双精度值。
我花了几个小时研究这个问题,却一无所获。
图形/画布线程的代码如下所示:
public void run() {
while (isRunning){
if (!holder.getSurface().isValid()){
continue;
}
Canvas canvas = holder.lockCanvas();
canvas.drawARGB(255,255,255,255);
Paint paint = new Paint();
paint.setTextSize(32);
paint.setColor(Color.BLACK);
canvas.drawText( "Next Block",525,80,paint);
paint.setColor(NextBlock);
canvas.drawRect(525 , 100 ,675, 250,paint);
paint.setColor(Color.BLACK);
canvas.drawText("Held Block",525,380,paint);
paint.setColor(HeldBlock);
canvas.drawRect(525,400,675,550,paint);
paint.setColor(Color.BLACK);
paint.setTextSize(52);
canvas.drawText("Score",525,700,paint);
paint.setTextSize(82);
canvas.drawText(String.valueOf(score),525,800,paint);
paint.setTextSize(32);
for (int x = 0; x < 3; x++){
for (int y= 0; y < 5; y++){
paint.setColor(BlocksGrid[x][y]);
canvas.drawRect((squareSize * x) + (gap * x) + startX, (squareSize * y) + (gap * y) + startY,(squareSize * x) + (gap * x) + startX + squareSize,(squareSize * y) + (gap * y) + startY + squareSize,paint);
}
paint.setColor(Color.GREEN);
if (height[x] < 0){
paint.setColor(Color.RED);
}
canvas.drawRect((squareSize * x) + (gap * x) + startX, 870,(squareSize * x) + (gap * x) + startX + squareSize,945,paint);
}
holder.unlockCanvasAndPost(canvas);
}
}
我认为这应该足够了,但是如果需要更多代码,请询问。 感谢您的帮助,因为我已经花了数小时试图在程序最终完成之前解决此问题!