Android中的位图:显示为50dp的资源大小

caw*_*caw 4 size resources png android image

我有一个PNG图像,我想在我的应用程序中显示.

在布局文件(.xml)中,我将宽度和高度设置为50dp(与密度无关).但是我的资源(.png)文件的大小应该是多少?

我想到了这一点(根据密度比计算):

  • ldpi资源:38
  • mdpi资源:50(基数)
  • hdpi资源:75
  • xhdpi资源:100

或者是"/ res/drawables"中的一个png文件是否足够宽50px?

aro*_*ero 11

您将不得不创建4个图像.mdpi将是基线,意思mdpi是100%.其他人遵循这个公式:

可绘

将每个图像在适当的资源文件夹,drawable-ldpi,drawable-mdpi,drawable-hdpi,drawable-xhdpi.Android会根据当前设备选择合适的drawable.

然后,在您的布局中只是wrap_content您的宽度和高度,不需要指定任何固定大小:

<ImageView
    android:src="@drawable/my_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

  • 这就是android本身的设计方式.1dpi等于160dpi配置上的1px(物理像素),这是mdpi的参考值.这就是为什么mdpi是基线.他们采用这个惯例的原因是第一个Android设备(G1)具有HVGA中dpi屏幕. (3认同)