Drawable Image does not scale as per screen density - android

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"/>

Related

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

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

Correct image size at the source or hardocing the dp size in layout

When you create your xml layout and say you load imageview or a button with a background from a drawable image. Then do you design the orginal drawable image so that it's size will look what you want on screen when using "wrap content"? Or do you just grab whatever size the image is and resize the widget that is using the image by putting something like in the xml
android:width ="100dp" android:height="100dp"
This best way I can suggest you is to get you image designed in proper resolution and put it in appropriate drawable folder. Let's say you are targeted highest resolution is 1080x1920, then get your screen designed in this resolution and slice the image in actual resolution and paste it in "drawable-xxhdpi" folder. It'll work for your targeted screen size and for all other resolutions smaller than the targets resolution.
In situations like this, it's better to specify the dimensions of the view.
When you set a drawable as a background to a View, it automatically takes up the size of that View.
Here's an example:
<TextView
android:layout_width="400dp"
android:layout_height="100dp"
android:background="#drawable/background" />
The background will be the image named background.png/background.xml in your drawable folder, and no matter the size, it will take up the size of the TextView, which is 400dp long and 100dp tall.

android widget 4x2 full size photo prepare?

I want to design widget about 4x2 cells size.
But I read the offical document ,
there are minWidget and minHeight individually setting in the AppWidgetProviderInfo metadata.
Now I want to set full size photo(not use 9 patch method) in 4x2 cells.
How to prepare the photo size in pixels and set in the widget layout width and height(dp)?
I had saw the minWidth and minHeight 4x2 is 250x110dp in document.
(http://developer.android.com/guide/practices/ui_guidelines/widget_design.html)
I set the value in the metadata(AppWidgetProviderInfo), and if I setting the size in the widget_layout width and height build in the
cell phone. the widget is showing very small.
How to prepare the photo pixels in the 4x2 cells full size in the different folders, please?
Thank you very much.
My English is not good. so I express my meaning hardly.
( I know the chinese)
Thank you.
If you want the image to be full size you need to specify that in XML. In XML you can specify the height and width. Then you can say adjustViewBounds and set that to "true". Next you can tell the image how you want it positioned. For myself I prefer scaleType set to "centerCrop".
<ImageView
android:id="#+id/picture"
android:layout_width="250dp"
android:layout_height="110dp"
android:adjustViewBounds="true"
android:contentDescription="#string/text"
android:scaleType="centerCrop"
android:src="#drawable/a_picture" />
Either way you can mess with the XML settings to get the image to adjust to your liking, this is just a sample to get you started
If your only doing widgets a 9 patch is recommended but you can simply place an image in your drawable hdpi, mdpi, xhdpi, and xxhdpi folders. You can find the scaling ratios at this link

Android images not being displayed at full size

I'm using the Rotten Tomatoes sample app from here: Rotten Tomatoes sample app.
But the movie poster images are not displaying full size on my phone. The layout width and height properties are both set to "wrap_content", but the images display shrunken. I have to set an actual pixel size for the width and height in order for them to display properly. I don't know why this is happening.
Does not work:
<ImageView
android:id="#+id/ivPosterImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/large_movie_poster" />
Does work:
<ImageView
android:id="#+id/ivPosterImage"
android:layout_width="121dp"
android:layout_height="179dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/large_movie_poster" />
You should take a look at What is the difference between "px", "dp", "dip" and "sp" on Android? to understand what the unit types mean and get your head around how android scales things.
You can then set the scaletype of your image view's to the various different options available and see what gives you the results you expect OR set the units to px (not recommended), bear in mind that if you use the images 'at their full size' in px the image will be scaled differently in relation to other elements on different screen sizes
ImageView doesn't resize on its own.
Once you downloaded the image, get its height and width and set the ImageView's Layout Parameters to those sizes.
In case the image's width is greater than the phone screen, set the width of the ImageView to the screen width and scale the height accordingly.

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.

Categories

Resources