如何在android中的不同设备上保留相同的图像大小
我使用imageView
来显示活动图像。此图像大小不应根据设备屏幕更改。但这不起作用。我想为每个设备使用图像的实际大小。以下是我所尝试过的。如何在android中的不同设备上保留相同的图像大小
<?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>
我怎样才能使用相同的图像大小,而不在每个设备屏幕上缩放? 有什么想法。
谢谢。
使用英寸,而不是像素大小
例如,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
你必须设计布局为每一个屏幕尺寸还是应该在不同的助攻宣告你梦诗文件夹,而不是wrap_content,因为它会根据屏幕密度缩放您的图像,
值文件夹,您必须创建与维度:
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
这是非常不舒服和恼人的使用很多xmls – Vyacheslav
不,这不是我所期望的。 – Barrier
我不想缩放图像或imageView。我只是想在每台设备上保留其实际大小。 – Barrier
使用固定宽度和高度在dp
。
DP是dp单位到屏幕像素的转换很简单:px = dp *(dpi/160)。例如,在240 dpi屏幕上,1 dp等于1.5个物理像素。定义应用程序的用户界面时,应始终使用dp单位,以确保在不同密度的屏幕上正确显示您的用户界面。
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/circle"
android:id="@+id/imageView"
android:scaleType="center"/>
有关DP,SP,px的更多信息。请检查this
为此,你必须定义静态固定的高度和宽度您的ImageView,然后使用以下属性: -
imgview.setScaleType(ScaleType.FIT_XY);
使用的软件,只检查怨妇形象的实际大小像Photoshop或瘸子,然后设置实际宽度和高度(例如:50像素X 40像素),在你的ImageView这样:
<ImageView
android:layout_width="50px"
android:layout_height="40px"
android:src="@drawable/circle"
android:id="@+id/imageView"
android:scaleType="center"/>
只是它来做这样!高度是固定的,所以你需要提供一个高度值,宽度必须是match_parent.Its对我来说工作得很好。
<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"
/>
你能举个例子吗?我对你的共享链接感到困惑。 – Barrier
@Brier,已经做到了。或者你想要什么样的代码? – Vyacheslav
@Brier,你试过了吗? – Vyacheslav