For example, the resolution of my game is 360 x 640, the screen resolution of the phone is 1920 x 1080 or 270 x 480 (all conditionally). How can I adjust the size of my game frame to the size of the screen? It should be borne in mind that the canvas will draw many elements - at least 2 layers (background, game objects) and the game objects themselves (for example, up to 50 dynamic units on the screen). And also, which is very important, there will be graphic control buttons on the screen. Pseudocode thread:

class DrawThread extends Thread{ private boolean runFlag = false; private SurfaceHolder surfaceHolder; private Bitmap picture1; private Bitmap picture2; private Bitmap picture3; // и тд private long prevTime; public DrawThread(SurfaceHolder surfaceHolder, Resources resources){ this.surfaceHolder = surfaceHolder; picture1,2,3,4 и тд = //получаем изображения prevTime = System.currentTimeMillis(); } public void setRunning(boolean run) { runFlag = run; } @Override public void run() { Canvas canvas; while (runFlag) { long now = System.currentTimeMillis(); long elapsedTime = now - prevTime; updatePhys(); //все расчеты проводим независимо от отрисовки canvas = null; try { canvas = surfaceHolder.lockCanvas(null); synchronized (surfaceHolder) { updateScreen(); //отрисовка всех элементов на экране } } finally { if (canvas != null) { surfaceHolder.unlockCanvasAndPost(canvas); } } } } public void updatePhys(){ //расчеты } public void updateScreen(){ canvas.drawBitmap(...) canvas.drawBitmap(...) // и тд ... } } 
  • It seems to me that it is necessary to draw objects not in absolute terms, but in relative ones, relative to the screen size. For example, this circle should have a radius of 1/12 of the width of the screen, and not 45 pixels - Vladyslav Matviienko
  • I understand what you mean, we get the display resolution ( WindowManager.getDefaultDisplay () ), we calculate the coefficient relative to our canvas and change the size of each element. But in this situation I don’t understand how to do physical calculations. - torin.dmitry
  • what are the physical calculations ? For example? - Vladyslav Matviienko
  • take for example a ball of arkanoid, which bounces off the walls of the frame. How and with respect to what should I calculate its movement trajectory, if the pixel resolution on each device is different? and the frame is adjusted to the size of each individual screen. Maybe there is an opportunity to create your freibuffer. Each frame, all elements are first drawn on a 360 x 640 bitmap, a background is superimposed, the ball is in a calculated position (the trajectory of which will be calculated relative to 360 x 640) and then this pseudo frame is scaled to the desired resolution, and so every frame. - torin.dmitry
  • This is possible, but it will look crooked if you stretch 360x640 to 1080x1920. You can count nat. data for 360h640, and drawing it all, for example, with the coefficient that you calculate at startup - the ratio of 360x640 to 1080x1920 - Vladyslav Matviienko

1 answer 1

Already forgot a little for my question, correcting.

  public void run(){ //создаем пустой прямоугольник Rect dstRect = new Rect(); while(running){ if(!holder.getSurface().isValid()){ continue; } //предварительно в engine передали наш framebuffer //наносим на framebuffer все что нужно в этом методе engine.updateGraphics(); canvas = holder.lockCanvas(); if(canvas != null){ //Ключевой момент. Устанавливаем нашему dstRect размеры разрешения юзера. canvas.getClipBounds(dstRect); dstRect.bottom = propHeight; //отрисовываем растянутый frabuffer На весь экран. canvas.drawBitmap(framebuffer, null, dstRect, null); holder.unlockCanvasAndPost(canvas); }else { System.out.println("Canvas = null!"); } } }