Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView, why different size?

Tags:

java

android

First thing, I know many people have asked same thing, but this one is nearly same but I have few more questions.

I have an image of 48px x 48px, so if I set this image in ImageView with

layout_width="wrap_content"
layout_height="wrap_content"

then image looks bigger but if I use following fixed size, it gets smaller

layout_width="48dp"
layout_height="48dp"

I think it's now showing properly(not pixelated ?) with 48dp, but why it got bigger if I use wrap_content ? And if the image size through wrap_content is correct then whats the point of using dp(if it make image smaller) ?

like image 230
xmen Avatar asked Mar 19 '13 14:03

xmen


People also ask

What is the purpose of the ImageView?

Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling. To learn more about Drawables, see: Drawable Resources.

What is difference between ImageView and ImageButton in android?

ImageButton has the same property as ImageView . Only one feature is extra, which is, images set through ImageButton are clickable, and actions can be attached with them upon clicking.

What is the default scale type for ImageView in android?

FIT CENTER (Default) If the asset is bigger than the ImageView , it is scaled down until both horizontal or vertical dimensions reach the edges of the ImageView . If the asset is smaller than the ImageView , it is scaled up until one of the horizontal or vertical dimensions reaches the edges of the ImageView .


3 Answers

48dp (or 48dip) stands for "Density Independent Pixel". This means that the physical size of the image as displayed on the screen of an Android should be the same regardless of screen density and size. In order to support this, Android adjusts the actual pixel size displayed based on screen density. Your findings of different sizes will vary from device to device.

The conversion of dp units to screen pixels is: pixels = dps * (density / 160). For example, on 240 dpi screen, 1dp would equal 1.5 physical pixels. Using dp units to define your application’s UI is highly recommended, as a way of ensuring proper display of your UI on different screens.

like image 162
David Manpearl Avatar answered Oct 07 '22 06:10

David Manpearl


This probably happens because you don't explicitly set the scaleType. Set your scaleType in XML as follows and you should be fine:

android:scaleType="center"
like image 31
Shade Avatar answered Oct 07 '22 05:10

Shade


Try this

<ImageView
    android:id="@+id/seprator2"
    layout_width="48dp"
    layout_height="48dp"
    android:scaleType="fitXY"
    android:src="@drawable/your_image" />

And if still it shows small then your image must contains alpha around it.

like image 35
Chirag Patel Avatar answered Oct 07 '22 07:10

Chirag Patel