juejiang
juejiang

Reputation: 333

Why the imageview's width and height is smaller than the drawable's width and height?

I'm testing ImageView's function while I comes across the questions. I have a image which is 1000*562.My testing device is Samsung I9103, the screen size of which is 480*800.My project layout is quite simple,just an ImageView.

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

<ImageView 
    android:id="@+id/test1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/pic_1000_562"/>
 </RelativeLayout>

I use the code below to get the drawable and ImageView size

    public void onWindowFocusChanged(boolean hasFocus){
    ImageView imageView=(ImageView)findViewById(R.id.test1);
    Log.v("Testresult","width= "+imageView.getWidth()+" height= "
    +imageView.getHeight());
    Log.v("Testresult","drawawidth= "+imageView.getDrawable().getBounds().width()+
            " drawableheight= "
    +imageView.getDrawable().getBounds().height());
}

But I get

11-17 17:48:46.865: V/Testresult(15225): width= 480 height= 690
11-17 17:48:46.865: V/Testresult(15225): drawawidth= 1000 drawableheight= 562

My question is why is drawabldwidth is 1000 not 480,as the image must have been scaled to fit the screen?

Upvotes: 0

Views: 659

Answers (1)

Szymon
Szymon

Reputation: 43023

This is because your ImageView takes the whole screen so it will take the resolution of the device. This part is clear, I guess.

As for the drawable, regardless of the ImageView size, it will get the same drawable and its size that you have in your resources. It doesn't matter that the ImageView is smaller, drawable is still the same.


To get the scaled image's dimensions, you can get the horizontal and vertical scale:

  • horizontal: 480 / 1000 = 0.48
  • vertical: 562 / 800 = 0.7025

The image will be scaled by the lower value (0.48 in this case). So the scaled dimentions are:

  • horizontal: 1000 * 0.48 = 480
  • vertical: 562 * 0.48 = 269.76

Upvotes: 2

Related Questions