Wednesday, June 1, 2011

[android-developers] Re-scaling PNG image to fit screen results in file about 2X the screen size

Given the following code (indentation reduced to fit max line length):

--------------------------- CUT HERE ---------------------------

// load and resize the original image
Bitmap image = BitmapFactory.decodeResource(getResources(),
mImageIds[info.position]);

// original size == w,h
int w = image.getWidth();
int h = image.getHeight();

// get display size in pixels --- this is the size we want...I think.
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
int nW = dm.widthPixels;
int nH = dm.heightPixels;

// Calculate the scale factor
float sW = ((float) nW) / w;
float sH = ((float) nH) / h;

// do NOT fsck with the aspect ratio: make sure we scale at 1:1
if (sW > sH) sW = sH;
if (sH > sW) sH = sW;

// scale the image by the calculated scale factor
Matrix matrix = new Matrix();
matrix.postScale(sW, sH);
Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0,
w, h, matrix, true);

// Load the resized image as wallpaper
try {
manager.setBitmap(resizedBitmap);
} catch (IOException e) {
e.printStackTrace();
}

--------------------------- CUT HERE ---------------------------

As I understand this, based on reading the developers guide and
the examples for each item, this *SHOULD* take the original PNG
image, determine its dimenstions, determine the screen size,
calculate the scale factor (and make sure it's 1:1 to maintain
the correct aspect ratio), resize the original image to fit the
screen (again, keeping the original aspect ratio), and set it to
wallpaper. Well, almost.

It is, in fact, rescaling the image to make it much larger (about
twice the size of the display on my Motorola Bravo---Android
2.2.1) than the window's dimensions.

I've been digging through this for over a week now, and finally
got this close tonight...but at this point, whatever it is, my
brain (with the aftermath of three tumors, triple brain surgery,
and radiation therapy, plus one chemo drug that is known to be
extremely nasty to the brain), will not allow me to see it. It
does that sometimes, and it is incredibly annoying, to say the
least.

Can anyone please point me in the right direction? What I'd
ultimately like to do is let the user select the desired
orientation (landscape or portrait), and resize to fit that
choice, and finally, lock (that might be the wrong word) the
orientation of the wallpaper to match the user's choice.

Thanks,
--jim

--
73 DE N5IAL (/4) | DMR: So fsck was originally called
spooky130u@gmail.com | something else.
< Running FreeBSD 7.0 > | Q: What was it called?
ICBM / Hurricane: | DMR: Well, the second letter was different.
30.44406N 86.59909W | -- Dennis M. Ritchie, Usenix, June 1998.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment