When loading android pictures, due to the limitation of the mobile screen, when many large pictures are loaded, we require equal scale, for example, according to the fixed width, equal scale height, so that the size scale of the picture gets the corresponding scale, but the picture does not change shape. Obviously, it can't be implemented according to android:scaleType, because there are many limitations, so you have to write your own algorithm.
Scale with Glide
In fact, glide provides such a method. Specifically, the setResource method that inherits Transformation is displayed.
(1) First get the width and height of the network or local picture
(2) Get the required target width
(3) Get the height of the target in proportion
(4) Create a new graph based on the width and height of the target
/** * =========================================== * Version: 1.0 * Description: set image scaling * <p>glide Process pictures</p> * =========================================== */ public class TransformationUtils extends ImageViewTarget<Bitmap> { private ImageView target; public TransformationUtils(ImageView target) { super(target); this.target = target; } @Override protected void setResource(Bitmap resource) { view.setImageBitmap(resource); //Get the width and height of the original image int width = resource.getWidth(); int height = resource.getHeight(); //Obtain imageView Breadth int imageViewWidth = target.getWidth(); //Calculate scale float sy = (float) (imageViewWidth * 0.1) / (float) (width * 0.1); //Calculate the height of the image after being magnified at the same scale int imageViewHeight = (int) (height * sy); ViewGroup.LayoutParams params = target.getLayoutParams(); params.height = imageViewHeight; target.setLayoutParams(params); } }
Then set transform in Glide
Glide.with(this) .load(newActiviteLeftBannerUrl) .asBitmap() .placeholder(R.drawable.placeholder) .into(new TransformationUtils(target));
Transformation this is a very powerful function of Glide, which allows you to make a series of transformations in the middle of load image - > into ImageView. For example, if you want to make a picture Gaussian blur, add fillets, do gray processing, circle picture, etc., you can do it through transformation.