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

  • Genymotion - which IP set in etc/hosts file for access host's local webserver via domain?
  • gradle: Android Studio inherit buildtype
  • Changing TextView Opacity in Android
  • Android download binary file problems
  • Logback-Android : “no applicable action” error when using FixedWindowRollingPolicy with a SizeBasedTriggeringPolicy
  • Smooth Progress Bar Animation
  • Related posts:

    Endless adapter for ViewPager
    Android: Compiler warnings in Android Studio
    Displaying UI widgets in Cardview in android L
    Masking(crop) image in frame
    Marshmallow RelativeLayout behaves strange with alignBaseline
    Android ViewPropertyAnimator doesn't scale at the same time
  • How make Android self components and use it from a jar library?
  • Referencing the google-play-services library
  • In eclipse, unable to reference an android library project in another android project
  • shrinkResources set to true, but all unused resources (specifically drawables) not getting removed
  • How to add a footer in ListView?
  • Change Preference Item Summary Text Color in Android 4
  • 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.