android network download pictures, set the width and height, geometric scaling

Use Picasso components to download the picture will find the picture width and height will not deform geometric scaling control, even if you set scaleType picture may be of no use api for Picasso to,
Picasso.with(this.activity)
    .load(AppModel.GetInstance().userAvatarPath)
    .noFade ()
    .fit()
    .into(avatar)
 
Glide library will be more convenient to use, good control of the downloaded images wider than higher zoom
 
 
1, the following layout:
 
<ImageView
android:id="@+id/qrcodeImg"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="fitCenter"
android:background="@color/blue"
/>
 

 

1> scaleType used to fitCenter, the default value is fitCenter, so you can not write, write on the safe side,
2> We do not know about to download a remote image width and height much, here or set width, height, otherwise the code will complain
3> to set the background color ImageView it is to clearly know the picture after download and display, in fact, the display range and scale mode is like
 
2, as follows:
Set the width and height at the same time, and finally set the maximum height, maximum width, does not exceed 70% of the screen width, screen height does not exceed 60% of the code setAdjustViewBounds see online that is in line with maxwidth, maxheight use, keep the geometric , but the actual test found no change to remove the phrase,
I started this practice to avoid the development of the game in the kind of trouble more ideas: first get the real picture of the aspect ratio of the network, and then the screen aspect ratio is calculated by comparing the appropriate width or height, then scaling.
To ensure that the geometric scaling, but also to ensure all images visible, not exceed, not trimmed.
Glide.with(getContext()).load(AppModel.GetInstance().getUserQrcodePath()).into(qrcodeImg);
 
int screenWidth = QMUIDisplayHelper.getScreenWidth(getContext());
int screenHeight = QMUIDisplayHelper.getScreenHeight(getContext());
ViewGroup.LayoutParams layoutParams = (ViewGroup.LayoutParams) qrcodeImg.getLayoutParams();
 
int maxWidth = (int) (screenWidth * 0.7);
int maxHeight = (int) (screenHeight * 0.6);
 
layoutParams.width = maxWidth;
layoutParams.height = maxHeight;
 
qrcodeImg.setAdjustViewBounds(true);
qrcodeImg.setMaxWidth(maxWidth);
qrcodeImg.setMaxHeight(maxHeight);
 

 

3, real machine effect Screenshot:
First high> FIG width, the second width is> FIG high
Here can be seen the benefits of setting the background color of the ImageView, the true scope of the picture is the blue area, fitCenter zoom mode so that in case of an external constant size, internal zoom
 

 

 

 

 

Guess you like

Origin www.cnblogs.com/JD85/p/12026305.html