Amina
Amina

Reputation: 723

Android :The unit of imageLayout.getHeight

I'm creating an imageLyout in xml file ,using this code this code :

    <com.manuelpeinado.imagelayout.ImageLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:id="@+id/image_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#aaa"
    custom:fit="both"
    custom:image="@drawable/plan"
    custom:imageHeight="1744"
    custom:imageWidth="1150" >
 </com.manuelpeinado.imagelayout.ImageLayout>

When i enter to the image in the drawbale-hdpi properties i see that image dimensions are : 515 * 348 pixels.

And when i i use this java code :

                     hauteurLayout = imageLayout.getHeight();
             largeurLayout = imageLayout.getWidth() ;

I get this result : 407 * 601 .

Well i'm confused about all those dimensions, why are they differents and they are representing the same image ?

And i need to draw something in a map ,so i need to kwow the right dimensions that i have to use, to draw the object in the exact place.

And this is the image (i'm using horizontalScreeView ):

enter image description here

Upvotes: 1

Views: 302

Answers (2)

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

To support multiple screen resolutions, you should use density independent pixels (dip), also known as dp.
This is a scaling method to scale from px (which depend on the screen resolution) to dp (which scale well on every device):

private final int px2dp(int px)
{
    final float scale = getResources().getSystem().getDisplayMetrics().density;
    return (int) (px * scale);
}

Upvotes: 1

Anandroid
Anandroid

Reputation: 403

Always use dp while giving dimen to layouts in xml.

To get back exact value, get the value in px and convert to dp using:

int dpToPx(final int dp)
{
return (int) (dp * editText.getResources().getDisplayMetrics().density + 0.5f);
}

Upvotes: 0

Related Questions