问题
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 ?
回答1:
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"/>
回答2:
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;
来源:https://stackoverflow.com/questions/9984360/how-to-set-imageview-drawable-size-based-on-its-parent-view-size