0

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.

Terance Wijesuriya
  • 1,928
  • 9
  • 31
  • 61

7 Answers7

1

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • Can you provide me an example ? I am confused with your shared link. – Terance Wijesuriya Apr 26 '16 at 10:08
  • @Barrier ,so I don't understand which changes I want to see. This code says, this pickture in each device will have 1 inch width and 1 inch height. Do you want this or something else? – Vyacheslav Apr 26 '16 at 10:23
0

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
Ashwani
  • 1,294
  • 1
  • 11
  • 24
  • This is very uncomfortably and annoying to use a lot of xmls – Vyacheslav Apr 26 '16 at 10:09
  • No this isn't what I expected . – Terance Wijesuriya Apr 26 '16 at 10:11
  • I do not want to scale the image or imageView . I just want to keep its actual size on every devices. – Terance Wijesuriya Apr 26 '16 at 10:12
  • ok then what you can do is make use of this lib : `https://github.com/intuit/sdp` – Ashwani Apr 26 '16 at 10:12
  • @Barrier the thing is your image will be scaled automatically because of screen density of the handset screen... if the screen is mdpi then it will be exact size of your image, if it is hdpi then it will be 1.5x of your image, if it is xxhdpi then 2x and so on.. – Ashwani Apr 26 '16 at 10:17
  • you have to create different drawables folders like `drawable-hdpi drawable-xhdpi` and so on have a look at this slideshow Read only that part of this slide which is written in english, http://www.slideshare.net/ChaiwootPhrombutr/ss-28457631 – Ashwani Apr 26 '16 at 10:20
0

Use fixed width and height in dp.

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.

Source

<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 see What is the difference between px, dip, dp, and sp?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Sanjay Kakadiya
  • 1,596
  • 1
  • 15
  • 32
0

For this purpose, you have to define the static fixed height and width to your ImageView and then use the following attribute:-

imgview.setScaleType(ScaleType.FIT_XY);
Saurabh Vardani
  • 1,821
  • 2
  • 18
  • 33
0

Just inspect the actual size of jour image using a software like photoshop or gimp, then set the real width and height (e.g.: 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"/>
marcozabo
  • 174
  • 8
0

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"
    />
Akshat Vajpayee
  • 274
  • 1
  • 3
  • 15
0

Put your image in drawable or drawable-nodpi. Android will not scale your image size.you code do not need to change.

AlphaBoom
  • 56
  • 6