I have a problem that is driving me nuts.
I had 4 functions that would resize images correcly throuhout UI using density (1.5) and width/height values reported back by views, bitmaps, screen etc.
This worked excellent on SG II. But on a HTC Wildfire which reports density 0.75 ... some images got about 25% too small visually on screen... But when I decided to override the density to 1.0 those images suddenly fit while other became crisp, but no longer sized as correctly... And it is driving me nuts...
I can only conlcude hat I am some places comparing apples to pears (pixels and DIPs), but I can not get my tests to fit with what I read, so in an effort to be 100% sure of some things:
...
Will I get pixels underneath here? or DIPs? :
int bitmapWidth = bitmap.getWidth();
Suppose underneat is an imageview containing a bitmap. Pixels or DIPs?
int widthParent = view.getWidth();
Underneath is actually DIPs, right?
getContext().getResources().getDisplayMetrics().widthPixels;
...
Here is my code that is not working (this code is giving a bitmap that visually takes maybe 2/3s width where it should take 100%) If you check comments the pixels values are apparently correct, but end result is not correct:
vto.addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mainLogo.getViewTreeObserver().removeGlobalOnLayoutListener(this);
mainLogo.setScaleType(ImageView.ScaleType.CENTER_INSIDE); // mainLogo = ImageView
SharedCode.sharedUtilScaleImage_Width(mainLogo, false);
}
}
);
// ...
public static void sharedUtilScaleImage_Width(ImageView view, boolean tryBackground)
{
Drawable drawing = null;
boolean useBackground = false;
if (drawing == null) {
drawing = view.getDrawable();
}
if (drawing == null) {
if (tryBackground) {
drawing = view.getBackground();
useBackground = true;
}
}
if (drawing == null) {
return;
}
if (!(drawing instanceof BitmapDrawable)) {
return;
}
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
if (bitmap == null) {
return;
}
//--
int bitmapWidth = bitmap.getWidth(); // returns 770 (which is correct checking on disk)
int bitmapHeight = bitmap.getHeight();
// float density = 1;
// density = MicApp.getContext().getResources().getDisplayMetrics().density; // 1.5
float widthScreen = MicApp.getContext().getResources().getDisplayMetrics().widthPixels; // returns 480
int widthParent = view.getWidth(); // returns 480, should be same as screen
//--
float xScale = ( (float) widthParent / (float) bitmapWidth);
Matrix matrix = new Matrix();
matrix.postScale(xScale, xScale);
//--
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
int bitmapWidth2 = scaledBitmap.getWidth(); // 480
int bitmapHeight2 = scaledBitmap.getHeight();
//--
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
//--
if (useBackground) {
LayoutParams layoutparams = new LayoutParams(bitmapWidth, bitmapHeight);
view.setLayoutParams(layoutparams);
view.setBackgroundDrawable(result);
}
else {
view.setImageDrawable(result);
}
}