Scale a canvas in SurfaceView to a size larger than the screen size

I am creating a new application and I am using Canvas. I scale the canvas, but I set the size:

public static final int WIDTH = 1920; public static final int HEIGHT = 1080; 

(the screen will be in the landscape, so the width is greater than the height) Value if this application is intended for portrait orientation:

 public static final int WIDTH = 1080; public static final int HEIGHT = 1920; 

Scaled since canvas is usually equal to:

 public void render(Canvas c) { super.draw(c); final float scaleFactorX = getWidth()/(WIDTH*1.f); final float scaleFactorY = getHeight()/(HEIGHT*1.f); if (c != null) { final int savedState = c.save(); c.scale(scaleFactorX, scaleFactorY); /////render /////render end c.restoreToCount(savedState); } } 

Most phones today are HD or better, and very few have lower resolution. But there are phones, and I'm concerned about how scaling will respond to other phones other than HD / better, as my application will achieve better resolution than the screen supports.

Any ideas how the phone will react to this? Some phones may automatically scale, but is a common feature or function just some phones?

SurfaceView is activated in Activity and set using setContentView:

 @Override public void onCreate(Bundle sis){ super.onCreate(sis); CustomSurfaceView sf = new CustomSurfaceView(this); setContentView(sf); //initialize sf.init(this, this); } 

EDIT

To clarify:

I scale the canvas to a specific size that matches the resolution of HD 1080. The HD 1080 screen will not scale compared to the screen. 2K screens will scale to HD 1080 resolution, meaning it will work at a lower resolution than the maximum screen size.
The size of the HD1080 is 1080x1920, which will be applied to the canvas on the screen, which is smaller than this size.
But how will scanning work on HD 720 screens, or even worse than HD 1080? The application will push a larger size than the screen supports. How does the phone react to this?


Running the application in a nexus emulator (nexus 4, api 23) leads to the fact that the canvas is slightly pushed away from the screen. Not all phones behave like nexus, because the firmware has been edited by manufacturers, therefore, just because it is disconnected from the screen on Nexus, this does not mean that it will be on Sony or any other brand.


Note: HD refers to HD 1080 unless otherwise indicated.

+6
source share
1 answer

Hard resolution is not required. This is not recommended. Android has a built-in graphics scaling mechanism using the DP module while drawing. details about the device
Convert DP to pixels dps * getResources().getDisplayMetrics().density more
Also go through the official docs

By the way, drawing on a fixed canvas HD-1080, and then resizing for a different resolution will work as follows.

  • Let your CustomSurfaceView display the full screen of the device. (Do not print the size of the hard code of your kind, for example, canvas)
  • Use the same scale factor for xScale and yScale to maintain the ratio. Keep in mind that you will get a black screen on the sides in devices other than the 16: 9 format. (You can move your canvas to the center and draw some kind of frame on the sides)
  • Use the following code to calculate the ratio. (Help taken from here and here )

 public void render(Canvas c) { super.draw(c); float scaleFactor = 1f; if ((getWidth()> 0) || (getHeight() > 0)) { scaleFactor = Math.min(WIDTH/getWidth()*1f, HEIGHT/getHeight()*1f); } if (c != null) { c.scale(scaleFactor, scaleFactor ); //your code } } 
-one
source

Source: https://habr.com/ru/post/1013339/


All Articles