When loading Android pictures, due to the limitation of the mobile phone screen, when many large pictures are loaded, we require equal-scale scaling, such as scaling the height according to a fixed width and equal-scale scaling, so that the size ratio of the picture is scaled accordingly, but the picture is not deformed. Obviously, it cannot be implemented according to android:scaleType, because there will be many restrictions, so you must write the algorithm yourself.
Scaling via Glide
In fact, glide provides such a method. Specifically, it displays the setResource method that inherits Transformation.
(1) Get the width and height of the network or local image first
(2) Obtain the required target width
(3) Get the height of the target in proportion
(4) Create a new picture according to the width and height of the target
/** * ======================================================== * Version: 1.0 * Description: Set image equal-scale zoom * <p>glide handles pictures.</p> * ======================================================== */ public class TransformationUtils extends ImageViewTarget<Bitmap> { private ImageView target; public TransformationUtils(ImageView target) { super(target); = target; } @Override protected void setResource(Bitmap resource) { (resource); //Get the width and height of the original image int width = (); int height = (); //Get the width of the imageView int imageViewWidth = (); //Calculate the scaling ratio float sy = (float) (imageViewWidth * 0.1) / (float) (width * 0.1); // Calculate the image after amplification of the proportion of the int imageViewHeight = (int) (height * sy); params = (); = imageViewHeight; (params); } }
Then set transform in Glide
(this) .load(newActiviteLeftBannerUrl) .asBitmap() .placeholder() .into(new TransformationUtils(target));
Transformation This is a very powerful feature of Glide. It allows you to make a series of transformations in the middle of load image -> into ImageView. For example, if you want to make Gaussian blur pictures, add rounded corners, grey processing, circular pictures, etc., you can all do it through Transformation.
Summarize
The above is the implementation method of using Glide to load network pictures and scales on Android. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!