Ton
Ton

Reputation: 9736

Cropping, scaling and centering a bitmap

I have a picture in a file in portrait mode and I want to use as wallpaper. this is easy so far. but the picture doesn't have same proportions as my device screen does. so there is a blank part of the screen at the bottom. I am really terrible working with graphics. How can I scale my Bitmap to fit my screen? It doesn't matter if I loose information as long as the seen part is centered.

Upvotes: 0

Views: 1795

Answers (2)

Xavi Gil
Xavi Gil

Reputation: 11568

First you get your image in a Bitmap with public static Bitmap decodeResource (Resources res, int id) (if the image is an app resource, if not, you could use public static Bitmap decodeFile (String pathName) to read a file)

Then pass that Bitmap as a parameter to the next method with the rectangle you want to crop from the Bitmap and you will have a cropped Bitmap as a result:

public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height)

Bitmap bmp = BitmapFactory.decodeResource( getResources() , R.drawable.your_image );
Bitmap croppedBmp = Bitmap.createBitmap(bmp, 100, 100, 400, 800);

Is this what you need?

Upvotes: 1

Frank Sposaro
Frank Sposaro

Reputation: 8531

This sounds like a job for the 9patch tool

http://developer.android.com/guide/developing/tools/draw9patch.html

You can use this tool to set which areas you want to stretch and which to keep the same.

Upvotes: 1

Related Questions