its a little hard to explain but ill try it anyways.
Im displaying an Image within an ImageView, the imageview is set to use 50% of the ScreenHeight using weight attribute.
Now if the ImageAspectRatio doesnt fit the Ratio of the ImageView, i have an empty space at the bottom and onTop of the ImageView.
Basically i want my ImageView to have a maxSize which is 50% of the ScreenHeight, but to shrink in height if the image in it doesnt use its full height, or at least move to the top of the ImageView.
So, what i did here is i set the View to its max size, and i set the image the way the ImageView usually would. After that i way for the ImageView to layout, then get its Height, as thats the max height i want the View to have.
Then i calculate the Scaling i need to apply. I scale the Image and set the view to Wrap content as it should have the views previous height as a bounding.
Now, this works fine on a Samsung GT-I9000 running CyagenoMod (Android 4.2.2) also tried this on a Kindle HD. But when doing the same on a Nexus 4(running android 4.3) it seems to ignore everything i do.
By that i mean, the image appears to scale properly (imageHeight after scaling something like 53x), but the image takes almost the whole Screen. I dont get why it works on the other 2 devices but on the Nexus.
Is there any new API concerning ImageView for Android 4.3 i dont know about?
@Override
public void setImageResource(final int resId) {
// this.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 0.5f));
super.setImageResource(resId);
getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
@Override
public boolean onPreDraw() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int boundingY = getHeight();
int boundingX = getWidth();
float xScale = ((float) boundingX) / width;
float yScale = ((float) boundingY) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
MyImageView.this.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT ));
MyImageView.super.setImageBitmap(scaledBitmap);
getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
});
}
Ok here is a picture of what im trying to do/doing.
Consider the blueish thing the size of the ImageView, and the Black rectangle, the actual Size of the Image. The Blue size is what i get, if i set the Size of the ImageView to width = matchParent and height = 0 with weight 0.5. This is the MAX Height i want my ImageView to have.
Now, it is possible that the Image has a width >= width of the ImageView. in that case, i want to resize the Height, to fit the Height of the Image, so i resize the Image, to find within those bounds and set the Image to WrapContent as the Image is the exact size i want the View to be.
But this is also possible for an Image. 2*width = height In that case i dont want the ImageView to do anything. Just Maintain its max height and display the give Image.
This works, as i already said on some devices, GT-I9000 and Amazon Kindle HD, but on a Nexus 4 the images that have a much bigger height than width tend to occupy the whole damn screen.
I debugged this and retrieved ImageViewHeight and BitmapHeight. Both stated that the Image and the ImageView is 534 PX high.
I really dont get why i works on the other 2 devices, but not on the nexus.
<include layout="@layout/own_action_bar" />
<*.view.MyImageView
android:id="@+id/drawFragment_iv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:adjustViewBounds="true"
android:contentDescription="@string/hello" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5" >
something in here
<RelativeLayout/>
<LinearLayout/>