[英]How to keep same image size on different devices in android
I use a imageView
to show an image on activity . 我使用
imageView
在activity上显示图像。 This image size shouldn't change according to the device screen . 该图片大小不应根据设备屏幕改变。 But This is not working .
但这是行不通的。 I want use the actual size of the image for every device.
我想为每个设备使用图像的实际大小。 Below is what I have tried .
以下是我尝试过的方法。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/circle"
android:id="@+id/imageView"
android:scaleType="center"/>
</RelativeLayout>
How could I use same image size without scaling on each device screen ? 如何在不缩放每个设备屏幕的情况下使用相同的图像大小? Have any ideas.
有什么想法。
Thank you. 谢谢。
Use inches instead of pixels size 使用英寸而不是像素大小
for example, android:layout_width=".5in"
例如
android:layout_width=".5in"
<ImageView
android:layout_width="1in"
android:layout_height="1in"
android:src="@drawable/circle"
android:id="@+id/imageView"
android:scaleType="center"/>
https://developer.android.com/guide/practices/screens_support.html https://developer.android.com/guide/practices/screens_support.html
You have to design layouts for every screen size or you should declare your dimens in different dimes folders instead of wrap_content because it will scale your image according to screen density, 您必须针对每种屏幕尺寸设计布局,或者应在不同的dims文件夹中声明您的Dim而不是wrap_content,因为它会根据屏幕密度缩放图像,
values folders which you have to create with dimens are: 您必须使用dimens创建的values文件夹是:
values-hdpi/dimens.xml------for hd handsets
values-xhdpi/dimens.xml-----for 5" screens xHD
values-xxhdpi/dimens.xml----for nexus 5X
values-xxxhdpi/dimens.xml----for nexus 6 and 6P devices
values-sw720dp/dimens.xml-----for 9" tablets
values-sw800dp-hdpi/dimens.xml--------for 10" tablets
values-sw600dp/dimens.xml------for 7" tablets
Use fix width and height in dp
. 在
dp
使用固定宽度和高度。
DP is The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). DP是dp单位到屏幕像素的转换很简单:px = dp *(dpi / 160)。 For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels.
例如,在240 dpi屏幕上,1 dp等于1.5物理像素。 You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
定义应用程序的UI时,应始终使用dp单位,以确保在具有不同密度的屏幕上正确显示UI。
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/circle"
android:id="@+id/imageView"
android:scaleType="center"/>
For more information about DP, SP, px. 有关DP,SP,px的更多信息。 Please check this
请检查一下
为此,您必须为ImageView定义静态的固定高度和宽度,然后使用以下属性:
imgview.setScaleType(ScaleType.FIT_XY);
Just inspect the actual size of jour image using a software like photoshop or gimp, then set the real width and height (eg: 50px x 40px)in your imageview in this way: 只需使用photoshop或gimp之类的软件检查jour图像的实际大小,然后以这种方式在imageview中设置实际的宽度和高度(例如:50px x 40px):
<ImageView
android:layout_width="50px"
android:layout_height="40px"
android:src="@drawable/circle"
android:id="@+id/imageView"
android:scaleType="center"/>
Just Do it in this way!! 只是这样做!! The height is fixed so you need to provide a value for height and the width must be match_parent.Its working perfectly fine for me.
高度是固定的,因此您需要提供一个高度值,宽度必须是match_parent。
<ImageView
android:id="@+id/img_saving_image"
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_cross"
android:scaleType="fitCenter"
or
android:scaleType="fitXY"
/>
Put your image in drawable
or drawable-nodpi
. 将图像放入
drawable
或drawable-nodpi
。 Android will not scale your image size.you code do not need to change. Android不会缩放您的图片大小。您的代码无需更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.