Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange Behavior with sceen sizes for different Android devices
    primarykey
    data
    text
    <p>I am a college student working on a final project for android development.</p> <p>My project is a simple game of snake where the user can use the accelerometer, or buttons.</p> <p>The way my game view works is I create a grid dynamically, on my "SnakeView" which extends view like such:</p> <pre><code>/** * This is where we paint the grid and the game. */ @Override public void onDraw(Canvas canvas){ super.onDraw(canvas); if(created){ canvas.drawBitmap(mBitmap, 0, 0, mPaintScreen); mBitmap.eraseColor(Color.WHITE); for(int i = 0 ; i &lt; xlength; i++){ for(int j = 0; j &lt; ylength; j++){ int x = i * rectWidth; int y = j * rectHeight; Point p = new Point(i,j); if(snake.contains(p)){ mBitmapCanvas.drawRect(x,y, x + rectWidth, y + rectHeight, mSnakePaint); }else if(walls.contains(p)){ mBitmapCanvas.drawRect(x,y, x + rectWidth, y + rectHeight, mWallPaint); }else if (growBerries.contains(p)){ mBitmapCanvas.drawRect(x,y, x + rectWidth, y + rectHeight, mGrowBerryPaint); } } } } } </code></pre> <p>Basically I have an arraylist that contains the snake and my berries. If the point on the plain is in either arraylist, I paint the rectangle a different color. Otherwise I keep the plane white.</p> <p>For setting rectWidth and height I go in the onSizeChanged method.</p> <pre><code>@Override public void onSizeChanged(int w, int h, int oldW, int oldH){ mBitmap = Bitmap.createBitmap(getWidth(), getHeight(),Bitmap.Config.ARGB_8888); mBitmapCanvas = new Canvas(mBitmap); mBitmap.eraseColor(Color.WHITE); width = w; height = h; rectWidth = width / (xlength-1); rectHeight = height / (ylength-1); invalidate(); } </code></pre> <p>now the issue is the blocks do not format to the correct size accross different devices. I have tested this on the nexus 7 tablet (not sure dimensions but its much larger), and my phone which is a pantech flex. (roughly 4 inches)</p> <p><a href="http://i45.tinypic.com/34r7lsy.png" rel="nofollow noreferrer">myimage http://i45.tinypic.com/34r7lsy.png</a> <a href="http://i46.tinypic.com/fl98hi.png" rel="nofollow noreferrer">myimage2 http://i46.tinypic.com/fl98hi.png</a></p> <p>As you can see, the one on the right, the outer border i created doesn't match up to the screen width. I was wondering why this could be.</p> <p>I definitely know the borders are hugging my grid, it is just that the grid is not spread out to fit the whole width.</p> <p>This should work and the sizes should fit according to screen size. My only guess is that the int w and int h, are rounded off, and they do not protray the actual width and height. xlength and ylength are the number of blocks I want in the x and y direction.</p> <p>I noticed that if I adjust the amount of rectangles, it may fit on one device, but not the other, I just want it to fit perfectly on both!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload