Class Canvas


  • public class Canvas
    extends java.lang.Object
    Represents the resolution of the application graphics independently from the device's screen resolution. The canvas is initially created with a preferred width and height. When the screen or window is resized, the canvas will also be resized while keeping its original aspect ratio.

    Having a consistent canvas size across different devices ensures a consistent user experience in situations where the same application is used across a variety of devices with different screen sizes. This class is then used to translate between the two coordinate systems.

    Alternatively, a flexible canvas can be created. In this case, the canvas follows the screen resolution without considering the requested width/height or aspect ratio.

    • Constructor Summary

      Constructors 
      Constructor Description
      Canvas​(int preferredWidth, int preferredHeight, boolean flexible)  
    • Constructor Detail

      • Canvas

        public Canvas​(int preferredWidth,
                      int preferredHeight,
                      boolean flexible)
    • Method Detail

      • resizeScreen

        public void resizeScreen​(int screenWidth,
                                 int screenHeight)
        Sets the screen dimensions to the specified values. This method should be called by the renderer when the application window is first created, and whenever the window is resized.
      • offsetScreen

        public void offsetScreen​(int offsetX,
                                 int offsetY)
        Offset the canvas position on the screen. This is mainly needed for when the screen contains system UI such as a status bar or window title where no application graphics can be displayed.
      • getWidth

        public int getWidth()
      • getHeight

        public int getHeight()
      • getBounds

        public Rect getBounds()
      • getZoomLevel

        public float getZoomLevel()
      • toCanvasX

        public float toCanvasX​(int screenX)
      • toCanvasY

        public float toCanvasY​(int screenY)
      • toScreenX

        public float toScreenX​(float canvasX)
      • toScreenY

        public float toScreenY​(float canvasY)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object