Asad Ali Bhatti
Asad Ali Bhatti

Reputation: 11

How to resize a bitmap image : Android

I want to increase size of image. i get image with this code BitmapFactory.decodeResource(getResources(), R.drawable.car);

But this image has different size on different screens, so i want to resize image according to screen size.

Upvotes: 0

Views: 684

Answers (4)

JustDoingIt
JustDoingIt

Reputation: 146

Of course you can scale:

Bitmap bm = Bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter);

Upvotes: 1

SingleWave Games
SingleWave Games

Reputation: 2648

Yes you can do this by using the following code snippets, the first section retrns the width and height of the current screen, the second section re-sizes the image.

// This will return the screen size in pixels
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

// Use this to bind and re-size the image
Bitmap car;
car = BitmapFactory.decodeResource(getResources(), R.drawable.car);
car = Bitmap.createScaledBitmap(car, width, height, true);

Upvotes: 0

gderaco
gderaco

Reputation: 2362

Look at http://square.github.io/picasso/ you can download, set where you want the image and resize it, just in one line. It also allows disk caching.

Upvotes: 0

Brij Raj Singh - MSFT
Brij Raj Singh - MSFT

Reputation: 5113

In android, one should store each resources in at least 3 formats, xhdpi, hdpi and mdpi considering that you have stored a resource's copy in all the 3 formats at their respective folders.

res/drawable-mdpi/my_icon.png // bitmap for medium density res/drawable-hdpi/my_icon.png // bitmap for high density res/drawable-xhdpi/my_icon.png // bitmap for extra high density

The platform is enough smart to pick up right resources according to right screen size on its own.

I don't think you'll face any issues, if you have proper images in all 3 folders.

http://developer.android.com/guide/practices/screens_support.html

Upvotes: 0

Related Questions