Reputation: 9
I'm trying to make a game for Android, I have a problem when dealing with multiple resolutions, this is a example of my gameview: http://oi42.tinypic.com/vh6ir6.jpg.
In the small area, there are several pieces of information, while the largest area is my play area. My purpose is to prevent the image (bitmap) from going beyond the edges of the playing area. This is partially working, but it only works with 800x432 resolution (the resolution of my tablet xD )
This is my code:
case MotionEvent.ACTION_MOVE:
//left edge
if (deltaX <= 90 && deltaY <= 400 && deltaY >28){
Static_PositionX = 52;
Static_PositionY = (int)deltaY-35;
Drag = false;
}
//down edge
else if (deltaY >= 380 && deltaX > 90 && deltaX <770) {
Static_PositionX = (int)deltaX-42;
Static_PositionY = 360;
Drag = false;
//right edge
}else if((deltaY <= 390 && deltaY > 20) && deltaX > 770){
Static_PositionX =723;
Static_PositionY = (int)deltaY-35;
Drag = false;
}
// up edge
else if ((deltaX >= 90 && deltaX < 770) && deltaY <= 35 ){
Static_PositionX =(int)deltaX-42;
Static_PositionY = 0;
Drag = false;
}
where deltaX and deltaY are my image coordinates and Static_PositionX/Static_PositionY are a random start position
deltaX=event.getX();
deltaY=event.getY();
Upvotes: 0
Views: 317
Reputation: 4511
Stop using absolute numbers for screen dimensions.
Refer to this answer here in SO on how to get them for current device.
Upvotes: 1
Reputation: 2042
Design your game for a specific resolution.
boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
int frameBufferWidth = isLandscape ? 480 : 320;
int frameBufferHeight = isLandscape ? 320 : 480;
Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
frameBufferHeight, Config.RGB_565);
Canvas canvas = new Canvas(frameBuffer); // Do all your drawing on frame buffer
When you actually render graphics on screen, draw the frame buffer on the actual canvas. This way all scaling for different screens would be handled for you.
Canvas canvas = holder.lockCanvas();
canvas.getClipBounds(dstRect);
canvas.drawBitmap(framebuffer, null, dstRect, null);
holder.unlockCanvasAndPost(canvas);
Upvotes: 0