Reputation: 1986
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.
Upvotes: 0
Views: 2560
Reputation: 1636
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.
<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?
Upvotes: 0
Reputation: 27211
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
Upvotes: 1
Reputation: 56
Put your image in drawable
or drawable-nodpi
.
Android will not scale your image size.you code do not need to change.
Upvotes: 0
Reputation: 324
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"
/>
Upvotes: 0
Reputation: 182
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"/>
Upvotes: 0
Reputation: 1861
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);
Upvotes: 0
Reputation: 1304
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
Upvotes: 0