Developing a graphics application that should work for any screen size requires some thinking. Typically, I create high-resolution graphics and a scale that matches the screen.
I saw a recommendation to avoid using "real pixels". I tried working with density and dp, but it seems more complicated than using the solution I use. And I could not find a better way to scale my graphics, and then use the device screen (real pixels).
I created this class to scale my images (based on real pixels) This solves most of my problems (nevertheless, some devices have different aspect ratios) and seem 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:
- Is it recommended to avoid "real pixels"?
- What is the best practice for scaling bitmaps on the screen (a good tutorial article is more than welcome).
- What are the disadvantages of using the method that I use, or what I should know when using this method.
Thanks for the tips.
[Edit] I forgot to mention that I usually use SurfaceView for my applications (if that matters)
source share