Jimmy
Jimmy

Reputation: 10341

How to set ImageView drawable Size based on Its parent view size?

I have imageView inside a relative layout. And when i create the activity i run thread to grap bitmap from the internet and set the imageView.

What i want is to resize the bitmap to scale based on relative view size while keeping the aspect ratio. The code for resizing the image is working but what i do is i get the relative view size by calling myRelativeView.getWidth() & myRelativeView.getHeight() and i feel that i should not do that because the docs say it could return 0. So what is the best way to retrieve the size of the relative view in this case ?

Upvotes: 1

Views: 9906

Answers (2)

Sruit A.Suk
Sruit A.Suk

Reputation: 7263

FIX for your request :]

If you don't want to change size: set your ImageView scaleType to "matrix"

If you want to change size but keep aspect ratio: set your ImageView scaleType to "fitCenter" (or "fitStart", "fitEnd")

To change in code:

ImageView image;

image = (ImageView)findViewById(R.id.yourImageView1);

image.setScaleType(ImageView.ScaleType.FIT_CENTER);

To Change in XML: Example

<ImageView
    android:layout_width="match_parent"
    android:layout_width="match_parent"
    android:scaleType="fitCenter"
    android:src="ic_launcher"/>

Upvotes: 2

jersam515
jersam515

Reputation: 657

If the layout is in a proportion with the whole screen you can try:

Display display = getWindowManager().getDefaultDisplay();
        width = display.getWidth()

//*proportion;or -dp; height = display.getHeight() //*proportion2;or -dp;

Upvotes: 0

Related Questions