-->

why is wrap content bigger than real pixel size?

2019-04-01 03:27发布

问题:

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?!

回答1:

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



回答2:

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.



回答3:

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.



回答4:

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



回答5:

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.