4

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:

  1. Why avoiding "real pixels" is recommended?
  2. What is the best practice of scaling Bitmaps to screen (a good article of tutorial are more then welcome)
  3. 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)

Nir
  • 1,608
  • 1
  • 17
  • 29
  • Also look at [this](http://stackoverflow.com/a/823966/556337). It would be helpfull for you in questions of smart decoding ByteArrays to bitmap – teoREtik Dec 20 '11 at 17:50

1 Answers1

0
  1. Never make your app pixel perfect. It will not work on other resolutions. The way you are handling your image sizes is not needed as Android does this for you when you make good scalable layouts. You should make an image for all dpi types and seperate them in your resources by using resources qualifiers (drawable-ldp folder, drawable-mdp folder, ..etc)
  2. Don't scale bitmaps yourself, as I said, Android will do this for you. You can influence the way Android scales your bitmaps, see http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
  3. Android will calculate the right sizes for you. This way you don't have to do all the maths yourself to try and get everything on the screen. Building a good app that runs on all kind of devices requires effort in making multiple sets of drawables and probably multiple sets of layouts (landscape / portrait, small / large screens) etc.
Jordi
  • 1,357
  • 12
  • 20
  • One think I forgot to mention.. I currently use SurfaceView. Does it matther? – Nir Dec 20 '11 at 17:12
  • Ok, I understand the "never make apps pixel perfect". I am not sure I understand the right method. For example I created an image 800x480 which is the resolution of my device. Then I load it using DecodeBitmap() but "android" automatically scaled my image to 1200x720 which is in my opinion is wrong decision. Why? and how can I can trust the automatic scaling? – Nir Dec 21 '11 at 11:10
  • 800x480 is perfect for your screen, but not on some other phones and tablets. In your XML layouts, you should use the different kinds of layout types (RelativeLayout, LinearLayout, TableLayout, etc..) and provide dimensions in DP or use fill_parent or wrap_content. If you need to, you can add some weight. Please read the following article: http://developer.android.com/reference/android/view/View.html – Jordi Dec 21 '11 at 14:25
  • I think you miss the fact I'm using SurfaceView. The images I load are usually backgrounds and sprites. If the "automatic" loading is wrong for my device screen this is a bad starting point :( This way I can not control my app on other screens – Nir Dec 21 '11 at 22:17