Android ImageView width setting, height adaptive

Recently, I encountered a requirement. The requirement is to fill the image within the specified width without knowing the width and height of the image. At the same time, the height is adaptive. I found it on the Internet and there are many solutions. Later, for my own application, I chose a plan with minor modifications, and finally proved that the effect is quite good. I record it here, hoping to help people with the same needs.

well, let's get back to business

First, you need to add Android :adjustViewBounds="true" to your ImageView layout

<ImageView android:id="@+id/test_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_launcher" />

 

Then, set the ImageView.max width and max height in the code, because the adjustViewBounds property will only work after setting the max height and max width

int screenWidth = getScreenWidth(this);
ViewGroup.LayoutParams lp = testImage.getLayoutParams();
lp.width = screenWidth;
lp.height = LayoutParams.WRAP_CONTENT;
testImage.setLayoutParams(lp);

testImage.setMaxWidth(screenWidth);
testImage.setMaxHeight(screenWidth * 5); In fact, it can be determined according to the needs. I tested it here as 5 times the maximum width


ok, next, loading the picture according to the conventional method will get the expected effect, students who need it can try it, good luck.


The following is the effect of my application. If you want to see it, you can find it yourself
http://android.myapp.com/myapp/detail.htm?apkName=com.puerlink.igo

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326523167&siteId=291194637