Question

I am trying to scale the bitmaps, and I would like them to work on all the android phones. I have seen this code on this website, but I am not sure how and where to apply this code into my app.:

Code:

      Bitmap image1, pic1;
      image1 = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
 float xScale = (float) canvas.getWidth() / image1.getWidth();
            float yScale = (float) canvas.getHeight() / image1.getHeight();
            float scale = Math.max(xScale, yScale);     //selects the larger size to grow the  images by

            scale = (float) (scale*1.1);                     //this allows for ensuring the image covers the whole screen.

            scaledWidth = scale * image1.getWidth();
            scaledHeight = scale * image1.getHeight();

                pic1 = Bitmap.createScaledBitmap(image1, (int)scaledWidth, (int)scaledHeight, true);

then I also saw this code from this website

http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/

The last update seems to be back in 2011.

Could someone please explains which method is better for the new API such as API10 or >?

I know there is API19 now, so I am sure there must be a new version of these and better.

Would you mind to share your knowledge and kindness with us please?

Thank you very much in advance.

Was it helpful?

Solution

Your code will scale the image, but Android will do a decent job of scaling images to fit the space allotted several ways.

If you place images in folders as described by this link: and making sure each folder has the right size image, the system will pick up the right sized image based on the device it is running on. There are also scaling parameters you can apply to an image (see ImageView.ScaleType for more info)

Also, this link describes how to efficiently load images to conserve memory. This is very useful for loading images into less memory than the full size would require as well as how to load multiple images via asynchronous tasks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top