简体   繁体   English

ImageView,为什么大小不一?

[英]ImageView, why different size?

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 我有一个48px x 48px的图像,所以如果我在ImageView中设置此图像

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 ? 我认为现在用48dp正确显示(不是像素化?),但是如果我使用wrap_content它为什么会变大? And if the image size through wrap_content is correct then whats the point of using dp(if it make image smaller) ? 如果通过wrap_content的图像大小是正确的,那么使用dp是什么意思(如果它使图像变小)?

48dp (or 48dip ) stands for "Density Independent Pixel". 48dp (或48dip )代表“密度独立像素”。 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. 这意味着无论屏幕密度和大小如何,Android屏幕上显示的图像的物理尺寸都应相同。 In order to support this, Android adjusts the actual pixel size displayed based on screen density. 为了支持这一点,Android会根据屏幕密度调整显示的实际像素大小。 Your findings of different sizes will vary from device to device. 您对不同尺寸的发现会因设备而异。

The conversion of dp units to screen pixels is: pixels = dps * (density / 160) . dp单位到屏幕像素的转换是: pixels = dps * (density / 160) For example, on 240 dpi screen, 1dp would equal 1.5 physical pixels. 例如,在240 dpi屏幕上, 1dp将等于1.5个物理像素。 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. 强烈建议使用dp单位来定义应用程序的UI,以确保在不同屏幕上正确显示UI。

This probably happens because you don't explicitly set the scaleType . 这可能是因为您没有显式设置scaleType Set your scaleType in XML as follows and you should be fine: 在XML中设置scaleType如下,你应该没问题:

android:scaleType="center"

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. 如果它仍然显示小,那么你的图像必须包含围绕它的alpha。

如果图像大于视图,则可以使用以下方法对其进行缩放:

android:scaleType="center"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM