Resolution independence in Android SurfaceView

I am currently starting a game engine in Android, first foray into the platform and have the basics in place however i am unsure of the best way to approach resolution independence when using SurfaceView to draw graphics.

Looking for pointers as to how to keep the game / sprites etc all looking the same independent of the screen, obviously it wouldn’t be efficient to scale all the sprites every frame or store many variations for differing resolutions

  • Can't start Android studio
  • How to take snapshot of screen not only app in Android with code
  • Android: onDestroy() or similar method in Application class
  • Android 4.4 http api bugs?
  • Android: launch a custom Preference from a PreferenceActivity
  • How to change position of Toast in Android?
  • Related posts:

    get the latest fragment in backstack
    Is there a simpler/better way to put a border/outline around my TextView?
    SIG33 when debugging native Android
    Search all the occurrences of a string in the entire project in Android Studio
    Android OpenGL .OBJ file loader
    How to fix android studios 2.3 NDK bug?
  • How to implement onBackPressed() in Fragments?
  • Android - Play audio from earpiece
  • Could not fetch model of type 'IdeaProject' using Gradle installation
  • mimicking the navigation drawer of youtube/gmail app
  • How do you make a LinearLayout scrollable?
  • How does the Apple color emoji font work, and is there an Android version?
  • 2 Solutions collect form web for “Resolution independence in Android SurfaceView”

    You’d just scale the sprites when you load them. I am guessing you’re loading them as Bitmaps from a Resource, right? If that’s the case then all you need to do it something like this:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.outHeight = spriteHeight * scale;
    options.outWidth = spriteWidth * scale;
    Bitmap sprite = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.sprite, options)

    Where scale is based on the change of the screen size.

    There are two simple approaches:


    On creation of a Surface view, it calls:

    onSizeChanged  (int w, int h, int oldw, int oldh)

    Create a few global variables, x & y so if you override this with:

    onSizeChanged(int w, int h, int oldw, int oldh){

    Then in any drawing or game calculations you can use the window w and h.


    call getWidth() & getHeight() in drawing and game calculations.

    Good luck with your game!

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.