Developing a graphical app that should work for any screen size requires some thinking Usually I create high resolution graphics and the scale to fit the screen.
I've seen a recommendation to avoid using "real pixels" I tried working with density and dp but it seems more complex then using the solution I use And couldn't find a better way to scale my graphics other then to use the device screen (real pixels)
I created this class to scale my images (based on real pixels) This solves most of my problems (still some devices have different aspect ration) and seems to work fine.
public class BitmapHelper {
// Scale and keep aspect ratio
static public Bitmap scaleToFitWidth(Bitmap b, int width) {
float factor = width / (float) b.getWidth();
return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), false);
}
// Scale and keep aspect ratio
static public Bitmap scaleToFitHeight(Bitmap b, int height) {
float factor = height / (float) b.getHeight();
return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factor), height, false);
}
// Scale and keep aspect ratio
static public Bitmap scaleToFill(Bitmap b, int width, int height) {
float factorH = height / (float) b.getWidth();
float factorW = width / (float) b.getWidth();
float factorToUse = (factorH > factorW) ? factorW : factorH;
return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factorToUse), (int) (b.getHeight() * factorToUse), false);
}
// Scale and dont keep aspect ratio
static public Bitmap strechToFill(Bitmap b, int width, int height) {
float factorH = height / (float) b.getHeight();
float factorW = width / (float) b.getWidth();
return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factorW), (int) (b.getHeight() * factorH), false);
}
}
and my questions are:
- Why avoiding "real pixels" is recommended?
- What is the best practice of scaling Bitmaps to screen (a good article of tutorial are more then welcome)
- What are the cons of using the method I use or what should I be aware of when using this method
Thanks for your advise
[Edit] I forgot to mention that I usually use SurfaceView for my apps (if it make any difference)