I am making a new app, and I am using a Canvas. I am scaling the canvas but I set the size to:
public static final int WIDTH = 1920;
public static final int HEIGHT = 1080;
(the screen will be in landscape. That is why width is bigger than height) Meaning if this app was intended for portrait orientation:
public static final int WIDTH = 1080;
public static final int HEIGHT = 1920;
Scaled as the canvas usually is:
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 worse resolution. But there are still phones, and I am concerned as to how the scaling will react on other non-HD/better phones as my app will be pushing for a better resolution than the screen supports.
Any ideas how the phone will respond to this? Some phones can auto-adjust the scaling but is that a general function or a function only some phones have?
The SurfaceView is activated in an 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 am scaling the canvas to a specific size that is the same as HD 1080 resolution. HD 1080 screens will not do any scaling compared to the screen. 2K screens will scale to HD 1080 resolution, meaning it will go with a lower resolution than the screen's max fit.
HD1080 size is 1080x1920 which will be applied to a canvas on a screen that is smaller than that size.
But how will the scaling act on HD 720 screens or in general worse resolution than HD 1080? The app will be pushing a bigger size than the screen supports. How will the phone react to this?
Running the app on a nexus emulator(nexus 4, api 23) results to the canvas being pushed slightly off the screen. Not all phones behave like nexus as the firmware has been edited by the manufacturers, so just because it goes off the screen on the Nexus doesn't mean it will on a Sony or any other brand
Note: HD references to HD 1080 unless otherwise defined.
Don't hard code resolution. It's not recommended. Android has build-in mechanism for scaling graphics by using DP unit while drawing. detail on unit
Convert DP into pixels by dps * getResources().getDisplayMetrics().density
detail
Also walk through official docs
By the way drawing on fixed HD-1080 canvas and then resize it for other resolution will work as follow.
CustomSurfaceView
take full screen of device. (Don't hard code size of your view like canvas) 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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.