Search code examples
androidandroid-layoutimageviewscaletype

Dont scale image in imageview but show it as a fixed sized imageview


anyone knows how to view an imageview of the same size across all devices? like i have a 1.45cm by 1.45cm image... it should be shown as 1.45x1.45 cm on all devices irrespective of their sizes... can this be done.? I need it to be of the same size because I am currently developing an optometry app. my current xml layout.. /---------------------------------/

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#ffffff"
    tools:context="com.adityamhatre.project.Etest$PlaceholderFragment" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="14.5mm"
        android:layout_height="14.5mm"
        android:scaleType="fitXY"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@raw/e11" />

</RelativeLayout>

/---------------------------------/


Solution

  • Normally in Android you should use dp to set dimensions instead of pixels/centimeters. The reason for this is because Android phone have differents densities, that is the reason why the image is not the same size on every device.

    If you want to have an image of exactly 1.45cm by 1.45cm, which is 0.57 inch by 0.57 inch, you should get the density of the screen to know what is the dpi (dots by inch) of it.

    By this logic, just follow this example: https://stackoverflow.com/a/17471780/2371161