Trying to get an ImageView to scale as the screen gets bigger - android

I'm currently new to Android development, and I'm was trying to get an image to scale for different screen sizes. I made a drawable-hdpi,-mdpi, and drawable-xdpi. In these folders I placed the image but at different sizes for each screen density ( Ex. for xdpi, I made the image bigger). I placed the imageView in the constraint layout and set the height and weight to wrap_content. I was expecting the image to be the same size as the corresponding drawable, but for higher screen densities it was still super small. Any thoughts? I also read about 9 patch images, but I cant just add a larger image to the higher density drawable folders ?

you should change the scaleType of imageview.
if you want to scale image with maintain aspect ratio you should use
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitcenter"/>
u can try another option like centerCrop and fitxy and see the result

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
....
Add android:scaleType="fitXY" to ImageView to adjust the width and Height of view

Related

Drawable Image does not scale as per screen density

I have an ImageView that is a part of ListItemView that is suppose to be displayed in the ListView. Without the Image, each item of listview takes less height. This is consistent across all the screens as they all are of same height. However, in one of the activities, I need to display an image in the listview items and this always increases the size of each item for that activity. I have the proper folder structure under res > drawable > family > family (hdpi), family (mdpi), family (xhdpi), family (xxhdpi), fmaily (xxxhdpi)
So each image has 5 version based upon the screen density.
I have the below in my layout file:
<ImageView
android:id="#+id/image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="right|center"/>
and I am setting the image in Java code:
ImageView imageView = listItemView.findViewById(R.id.image);
imageView.setImageResource(word.getImage());
However, no matter what resolution I choose, the image size always seems to be the same and it affects the size of the whole ListItemView in the ListView.
Size of ListView item without image:
With ImageView:
You may have noticed the change in the height of the ListView item with gray background. Is this normal. I dont want to fix the size of the image. I would like to scale it based upon screen resolution, however, it should not distort the overall height of the ListView item. This image remains the same no matter what the screen density is. why won't it scale automatically and fit the ListView item.
Is this normal or is there any workaround for this?
You are allowing the Image to decide how much space it can use by setting android:layout_height="wrap_content" and then having the width scale accordingly (in order for your layout_weight to work properly).
You could change your layout to force the ImageView to the height you prefer and let the use the scaleTypeto get the preferred scaling. In this example I used fitCenter but there are others that can be chosen (eg. fitXY).
By using the size qualifier dp the image will automatically "scale" with screen density as dp is Density-independent Pixels which means it will change with the screen density.
<ImageView
android:id="#+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:layout_weight="1"
android:layout_gravity="right|center"/>

What is the scaleType I have to use here

I have an Image of resolution 1600 x 2400. I want it to be displayed in an ImageView as follow:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="centerInside" />
I want the image to:
Take full width
Maintain Aspect ratio
Hence the imageview height must be as per the ratio
Eg: on a 320 x 600 screen, an image of size 1600 x 2400 must be shown as 320 x 480
What I currently get is an imageview with dimensions 320 x 2400 with image centered and space on top and bottom.
save the image in hdmi folder and use
android:src="#drawable/imagename"
or you can use
android:background="#drawable/imagename"
and you can set the heigth and width with
instead of fill parent or wrap or match use sizes in dp like 100dp or whatever you like.
android:layout_width="fill_parent"
android:layout_height="wrap_content"
example
android:layout_width="100dp"
1.Please go through the Developers guide first. Supporting Multiple Screens provides a pretty good overview of what you are trying to achieve. It's a challenging task being honest. Most of the iPhone devs enjoy the fixed screen size but we Android devs have to go through this problem. I can recomend few practices that I'm following
2.Try not to use Fixed height and weight as much as you can
Stick to Linear Layout if the layout is not complex. Go to relative layout only when its really mandatory.
Use Wrap and fill parent than giving fixed sizes..
These are the few I can think of.
3.Typically you just provide different layouts and resources for different screen densities and orientations as described in Android documentation
try using
android:scaleType="fitXY"
or
android:scaleType="fitCenter"
Assuming you have calculated how high your view should be: set it's layout parameters through code.
imageView.setLayoutParams(new LayoutParams(320, height));

Why is wrap_content bigger than real pixel size?

I am trying to understand something. A weird thing that I see is that when I put wrap_content in the width and hight, the image is bigger than the the real px (pixel) size of the image which is inserted. Why is that so?
My code with wrap_content:
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="false"
android:src="#drawable/header" />
and thats my code with exact pixel size of the image:
<ImageView
android:id="#+id/imageView1"
android:layout_width="378px"
android:layout_height="155px"
android:adjustViewBounds="false"
android:src="#drawable/header" />
As you can see, thats the exact pixel size:
Why is that? I thought that wrap_content should wrap the view to the content size so why is it bigger on screen?
If you really need to use the image's pixels as-is, and use the screen actual pixels do the following:
1) Create a res/drawable-nodpi and move your fixed-pixel non-scaling images in there.
2) You then can use wrap_content as layout_width and layout_height, and image pixels will match the device pixels with no upscaling because of dpi.
From http://developer.android.com/guide/practices/screens_support.html, the definition of nodpi :
Resources for all densities. These are density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen's density.
A very nice explaintaion for supporting the multiple screens is given at
http://developer.android.com/guide/practices/screens_support.html.
you need to put images in respective folders after scaling.
e.g. if you want 100*100 image then place
75*75 in drawable-ldpi for Low density devices
100*100 in drawable-mdpi for Medium density devices
150*150 in drawable-hdpi for High density devices
200*200 in drawable-xhdpi for Extra High density devices
wrap_content means that you want the image as it as its original size is. i.e if the size of the image is 200*200 then it will show 200*200 and if the image size is 400*400 it will show of the size 400*400.
So the reason you are getting a larger image then what you actually get when you hard code it with real pixels is because of the LARGE SIZE of the image. i.e image is actually large.
From my experience I can say that wrap_content always utilize maximum available size possible. So sometimes it stretch the image & sometimes reduce the size of the image. To use exact image use android:scaleType="fitXY"
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="false"
android:scaleType="fitXY"
android:src="#drawable/header" />
Update after 1st 2 comments:
as per android docs :
You can specify width and height with exact measurements, though you probably won't want to do this often. More often, you will use one of these constants to set the width or height:
wrap_content tells your view to size itself to the dimensions required by its content
fill_parent (renamed match_parent in API Level 8) tells your view to become as big as its parent view group will allow.
In general, specifying a layout width and height using absolute units such as pixels is not recommended. Instead, using relative measurements such as density-independent pixel units (dp), wrap_content, or fill_parent, is a better approach, because it helps ensure that your application will display properly across a variety of device screen sizes. The accepted measurement types are defined in the Available Resources document.
I found the term size itself is important, it autometically resizes the images. thats why I told that from my experience I found sometimes it stretch the image & sometimes reduce the size of the image
You need to see where you put the image. If its in hdpi it will look bigger on screen then if its in hdpi when ising wrap_content. so, in order for it to be the exact size, put it in right library.

CENTER_CROP does not maintain image ratio

By reading the Android doc, I expect a picture with this layout:
<ImageView
android:id="#+id/avatar"
android:layout_width="fill_parent"
android:layout_height="150dip"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
to fill the layout (height of 150 dip is the only condition) without any distorsion:
CENTER_CROP Scale the image uniformly (maintain the image's aspect
ratio) so that both dimensions (width and height) of the image will be
equal to or larger than the corresponding dimension of the view (minus
padding).
That works pretty well on phones:
But on tablet the image is totally distorted:
How is that possible to say: "please do not stretch horizontally"
I think you're specifying the image as a background rather than as a source. The images look stretched on the phone as well, and CenterCrop should work fine.
Try using the setImageBitmap or the "src" attribute and let me know if it works.

Why does image has larger size?

I'm using such layout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/logo"
android:orientation="horizontal" >
The drawable/logo.xml:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/login_logo"
android:gravity="center" />
The drawable/login_logo.png has 280 pixels width, my Galaxy Tab screen width is 600px but in launched app I see the image takes almost all screen width and it is about 500px
Where did I make mistake?
SOLVED
By setting
<uses-sdk android:minSdkVersion="4" />
and putting image to drawable-hdpi
As you are saying it is for galaxy tab so it is coming under drawable-hdpi.So change the resolution of image with photoshop or something and put it inside drawable-hdpi.
and try to use android:layout_width="wrap_content". instead of android:layout_width="fil_parent"
You are setting your picture in background, and the android:background tag uses 9 patch images. So your background always stretches to the width and height of the view. Your layout width is set to fill_parent, so the image width is fill_parent too.
The solution could be to make a 9 patch image of the png you are using with .9.png extension.
Read here for drawing 9 patch images - developer.android.com/guide/developing/tools/draw9patch.html
I suggest that you have a good read of the documentation. If you have placed your image in the drawable-mdpi folder, then in high density screens the image will appear to be 1.5x in size. You will have to provide different resolutions of your image for different screen densities.

Categories

Resources