简体   繁体   中英

How to keep same image size on different devices in android

I use a imageView to show an image on 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"

<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

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,

values folders which you have to create with dimens are:

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 is The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.

<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. 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:

   <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.

<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 . Android will not scale your image size.you code do not need to change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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