Search code examples
androidscreenscalesurfaceview

Android Development: Scale to fit different devices


Im making a game and using surfaceview. I load bitmaps that represent character and background and so on.

But HOW do I properly scale it to fit large devices and small devices and other devices?

//Simon

NOTE* dpi wont work, only pixels work whhen using canvas


Solution

  • You can implement a helper method that you can use to request calculations based on the device's dpi.

    Based on @MitulNakum answer from this qustion, do this:

    //note that mdpi is the standard
    float getAdjustedDimension(float value){
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        switch(metrics.densityDpi){
             case DisplayMetrics.DENSITY_LOW:
                  //api 4 and higher
                  return 0.75 * value;
             case DisplayMetrics.DENSITY_MEDIUM:
                  //api 4 and higher
                  return value;
             case DisplayMetrics.DENSITY_HIGH:
                  //api 4 and higher
                  return value * 1.25;
             case DisplayMetrics.DENSITY_XHIGH
                  //api 9 and higher
                  return value * 2;
             case DisplayMetrics.DENSITY_TV
                  //api 13 and higher
                  return value * 1.33125;
        }
    }