简体   繁体   English

如何计算不同设备dpi的图像尺寸

[英]How to calculate image size for different devices dpi

I have a listview layout with images 我有一个带有图像的列表视图布局

It looks like this 看起来像这样

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="240dp"
    android:adjustViewBounds="true"
    android:id="@+id/listview_item_imageView"
    android:layout_gravity="center_horizontal|top" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FAC308"
    android:id="@+id/listview_item_title"
    android:text="TITLE"
    android:textSize="20sp"
    android:paddingBottom="40dp"
    android:layout_centerInParent="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#ffffff"
    android:id="@+id/listview_item_subtitle"
    android:paddingTop="40dp"
    android:text="SUBTITLE"
    android:textSize="20sp"
    android:layout_centerInParent="true" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/orangeSeparator"
    android:src="@drawable/orangeseparator"
    android:layout_centerInParent="true" />

If I run it on device with 720x1280 240dpi it looks like this 如果我在720x1280 240dpi的设备上运行它,则看起来像这样

在此处输入图片说明

If I run it on device with 720x1280 320dpi it looks like this 如果我在720x1280 320dpi的设备上运行它,则它看起来像这样

在此处输入图片说明

I get images from the internet, so I cant prepare different versions. 我从互联网上获取图像,所以我无法准备不同的版本。

How to make it look similar across all the devices ? 如何使其在所有设备上看起来相似?

in your container you have 在你的容器里

android:layout_height="match_parent" android:layout_height =“ match_parent”

and in the image: 并在图片中:

android:layout_height="240dp" android:layout_height =“ 240dp”

you have to have the same height or 'wrap_content' for your container not to have those gaps. 您必须使容器具有相同的高度或'wrap_content'才能没有这些间隙。

you can also add to the imageview: 您还可以添加到imageview中:

android:scaleType="centerCrop"

another thing is that you can use specific library to handle loading of network images. 另一件事是,您可以使用特定的库来处理网络映像的加载。

You can use fitXY Android Developer , 您可以使用fitXY Android Developer

android:scaleType="fitXY"

or you can also create different drawable resources from the original one, with Photoshop or any similar program 或者您也可以使用Photoshop或任何类似程序创建与原始资源不同的可绘制资源

You can look this article . 你可以看这篇文章。 Make your app supporting different dpi variations and different screen types: Supporting Different Screens 使您的应用程序支持不同的dpi版本和不同的屏幕类型: 支持不同的屏幕

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

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