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 won开发者_JAVA百科t work, only pixels work whhen using canvas
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;
}
}
Take a look at AndEngine's Cameras. Everything you are looking for can be found there, and it is open source! I have used this engine for a number of games and prototype apps. It is very well documented.
AndEngine.org
Source Code
The android supported way is by creating several images for each bitmap you need for your app of varying sizes and letting the app decide which to use for a given screen density. Those images would go in the drawable-ldpi, drawable-hdpi, etc, folders. Check out the link and read it closly: http://developer.android.com/guide/practices/screens_support.html
Not very memory friendly though. :(
精彩评论