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

Deadly 提交于 2019-12-24 08:34:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!