How to Get Exactly the Same Point on Different Screen Sizes

How to get exactly the same point on different screen sizes?

It is possible to use constraints to match the positions of the circles with UIButtons. The trick is to use the multiplier of the constraint to scale the buttons width/height and position to the screen size.

I'll describe how to do it for one button, and then you can repeat it for the others. I assume the image is 657 wide by 918 high. If I have the dimensions reversed, you'll need to substitute the actual values for the ones I have used.

  1. Create a UIView to hold the image and buttons. Give this view an aspect ratio constraint with multiplier 657:918 which is width:height. Add the UIImageView to this view and constrain its 4 edges to the edges of the view with 0 offsets. Give this view constraints to the left and right edges of the main view and give it a vertical constraint to place it on the screen.
  2. Get the width/height of the circle in the image and the horizontal and vertical positions of the right edge and bottom edge. For example, the topmost circle is 106 x 106 and ends at horizontal position 392 and the bottom is at 338.
  3. Set the width of the button equal to the width of the containing view with multiplier 106:657 which is width of circle:width of the image.
  4. Set the height of the button equal to the height of the containing view with multiplier 106:918 which is height of circle:height of the image.
  5. Set the trailing edge of the button equal to the trailing edge of the containing view with multiplier 392:657 which is end of circle:width of image.
  6. Set the bottom edge of the button equal to the bottom edge of the containing view with multiplier 338:918 which is bottom of circle:height of the image.

This will allow the button to stay aligned with the circle on all devices. Repeat steps 2 through 6 for the other circles.

How to achieve same physics on different screen sizes?

I now use the height of the given 16 : >9 display and calculate the width it would have on a screen with a 16 : 9 display, then I calculate everything as if that was the actual width. Everything is drawn with an offset of (actualWidth-wantedWidth)/2f so that the objects appear centered. The same is possible for the height, if the ratio is 16 : <9

So in a nutshell: if the screen ratio doesn't fit, I make it fit by putting (black or textured) bars around my desired space.

Making a div appear at the same position regardless of screen size

Use vw (viewport width) instead of percentage. 1vw = 1/100th of the viewport width - regardless of device PPI, etc.
So, if you want the div to remain proportionally fixed regardless of device size and scren resolution - set your horizontal axis position with VW.

Example:

.somediv{ margin-right:20vw}

How to get relative coordinate on a large screen if we have a coordinate for small screen in android

Well sticking to what you asked ,you can get your new pixels as per follow

suppose the coordinates are (6,4) on 600*400 screen size, now calculate the % of x,y as per screen resolution ,as follow

(6 * 100 )/600 = 1%

and

(4* 100)/400 = 1%

now calculate the coordinates as per the new screen size as follow ,

(1 * 1280) /100 = 12.8 

and

(1* 800) /100 = 8

so the coordinates in the new screen size are : (12.8, 8) which were previously (6,4) .

But there are better ways to go through in requirements like these , if you could be more specific with what you are actually doing.

Best way to handle different screen sizes of the same category on Android

If you want to you can further specify screen size by the minimum dimension. Here is the directory to put the layouts for screens with the smallest width of 600dp:

layout-sw600dp

Although, I think this may only work for >=3.2 :(. However, there may be a clever way to take advantage of selector precedence. Read up.

I know this is a little late to recommend, but in general, try to using linear layout, weights, and 9-patch to make sure that your layout can be used for all resolutions. Ideally, you should only need a different layout for xlarge vs the rest.

How to get mouse position in the same place with different resolutions

the best way would be to not work with pixel coordinates but with percent coordinates.

on the screenshot, your board has a top offset and a left offset, as well as a width and a height.

for the current mouse x position, subtract the left offset and divide by the width of the game-board. the x value you will get will be:

less than 0: mouse is left of the board
between 0 and 1: relative position on the board (0 is totally on the left, 1 is totally on the right)
greater than one: mouse position is right of the board.

same applies for the y position.

If you then do all you clipping calculations based on these relative values, everything should work dependency independent.



Related Topics



Leave a reply



Submit