3

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 2
    Why are you hardcoding a specific resolution? That is the main issue here: you can look up the actual resolution of the phone and then use that when you are drawing. – Xaver Kapeller Dec 24 '16 at 14:16
  • True, but the scale of the content can also be important. When the canvas is scaled this way it also scales the content which makes the content at a size it is possible to see. – Zoe Dec 28 '16 at 17:07
  • When drawing to the canvas is 100 px not 100px on every phone. As such I scale the canvas and I thought it would be good to have a high resolution on this project. That is why I am using HD1080 standards and why I hardcode it. With a hardcoded scale 100px is 100px on nexus 1 and s7 edge. – Zoe Dec 28 '16 at 17:25
  • Well one obvious thing to point out: That's only going to work if the device has a 16:9 aspect ratio - which of course is most devices, but **by far** not all of them. On devices with different aspect ratio everything would be scaled very weirdly. If you want this to work on all devices then you are not going to get very far with this approach. – Xaver Kapeller Dec 29 '16 at 18:26
  • Android already has a system for scaling objects on the screen properly and its the whole system around density independent pixels (or dp/dip). You should use them to determine the size of your interface elements in pixels and then layout them according to the available space. That's the only way you are going to get a system that works on every phone. – Xaver Kapeller Dec 29 '16 at 18:27
  • I don't know what kind of app you are trying to make, but if its some sort of game then most frameworks used for creating games already have solutions built in which take care of all of this. You should look into that, otherwise you are not going to get around using dip values and then layouting the interface elements. – Xaver Kapeller Dec 29 '16 at 18:29
  • I do not use any frameworks to create my game. I build it using Android Studio and a few support libraries(including google play games). Using DP would be a solution to having the elements at the same screen size, but at which point it would also be hard to keep a track of touch events as there would also have to be calculations to make sure the pointer hit in the area that is exactly where the finger touches the screen. Also, there are several non-ui objects to take into account. But it still does not answer the question of how a common phone(not just nexus or samsung) handles the canvas – Zoe Dec 29 '16 at 19:40
  • when it is scaled to a size bigger than the resolution. Screen size in pixels are the dimensions(width * height) and when scaled to a bigger size it is hard to tell what the system will do. – Zoe Dec 29 '16 at 19:41
  • You shouldn't hardcode the screen sizes and also if you need to adjust for multiple screen sizes use multiple layout buckets. Your approach sounds all wrong. – portfoliobuilder Dec 30 '16 at 19:01
  • Yeah, I get it. I should not hardcode. But I am – Zoe Dec 30 '16 at 20:00
  • @XaverKapeller I am going to use DP instead of PX to scale the objects and make sure the sizes are the same on different resolutions – Zoe Dec 31 '16 at 14:15
  • @LunarWatcher Yeah, that should be the way to go. – Xaver Kapeller Dec 31 '16 at 18:54
  • With DP the size is relative to every screen, but it appears like the same size on every screen if I remember correctly – Zoe Jan 01 '17 at 20:18

1 Answers1

0

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.

  • Let your CustomSurfaceView take full screen of device. (Don't hard code size of your view like canvas)
  • Use same scaleFactor for xScale and yScale to preserve ratio. Keep in mind that you will get black screen on sides in devices having other than 16:9 aspect ratio. (You can reposition your canvas to center and draw some kind of frame on sides)
  • Use following code to calculate ratio. (Help is 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
    }
 }
Community
  • 1
  • 1
Qamar
  • 4,959
  • 1
  • 30
  • 49
  • Using a fixed Rect(0,0,WIDTH,HEIGHT) drawn to the screen nothing shows. I copied the code over to make sure I added it correctly but it still does not show anything on screen. – Zoe Dec 30 '16 at 10:05
  • @LunarWatcher Please notify me if my recent edit answer make sense – Qamar Jan 03 '17 at 06:16