About ScaleType, the article on the online introduction of this enumeration object a lot of, but basically just introduced its effect. When I was doing a scalable mobile imageview, in order to achieve the zoom and drag of the picture, I need to record the original matrix of the picture, in the process of using it found that the original matrix and ScaleType have a direct relationship, Different scaletype will directly affect the value of the matrix and affect the effect of the custom control. In order to better understand the relationship between the two, I went to read the ImageView source code, in this record after the personal understanding of the collation.
First of all, a brief introduction to the different scaletype, in fact, look at the name to know, scale (proportional) type (type), this object is used to adjust the scale of the picture type. The different scaletype affect the different scales of the image's length and width.
Matrix is drawn with matrices (The matrix region starting from the upper-left corner) Fitxy The image is not scaled up/down to the view size display (make sure the picture is fully displayed and full of view) Fitstart enlarges/shrinks the image to the width of the view, which is displayed in the upper part of the view (the picture is displayed in full) fitcenter enlarge/Shrink the picture to the width of the view, centered (the picture will be displayed in full) fitend Enlarge/Shrink the image to the width of the view, displayed in the lower part of the view (the picture will be displayed in full)
Center by the original size of the picture is displayed, when the picture is wider than the width of the view, then the center portion of the image is captured, when the picture is less than the width of the view, the picture is centered
Centercrop proportionally enlarges/shrinks the size of the image so that the image is higher than the view, making the picture wider or larger than the view width
Centerinside the contents of the picture to the full center, so that the picture is scaled down or the original size (picture than view hours) so that the picture width is equal to or less than the width of the view (picture will be full display) We know that you can use the Getimagematrix method to get the matrix object of a picture in ImageView, in fact, the matrix object that scaletype affects. In ImageView source code, Configurebounds is used to deal with the relationship between Matrix and ScaleType. Then read the source code to explain why the above display results. Configurebounds Source code is as follows:
Private voidconfigurebounds () {if(Mdrawable = =NULL|| !mhaveframe) { return; } intDwidth =Mdrawablewidth; intDheight =Mdrawableheight; intVwidth = GetWidth ()-Mpaddingleft-Mpaddingright; intVheight = GetHeight ()-Mpaddingtop-Mpaddingbottom; BooleanFits = (Dwidth < 0 | | vwidth = = dwidth) &&(Dheight< 0 | | Vheight = =dheight); if(dwidth <= 0 | | dheight <= 0 | | Scaletype.fit_xy = =Mscaletype) { /*If The drawable has no intrinsic size, or we are told to Scaletofit and then we just fill our entire view. */Mdrawable.setbounds (0, 0, Vwidth, vheight); Mdrawmatrix=NULL; } Else { //We need to does the scaling ourself, so has the drawable//Use the its native size.Mdrawable.setbounds (0, 0, Dwidth, dheight); if(Scaletype.matrix = =Mscaletype) { //Use the specified matrix As-is. if(Mmatrix.isidentity ()) {Mdrawmatrix=NULL; } Else{Mdrawmatrix=Mmatrix; } } Else if(Fits) {//the bitmap fits exactly, no transform needed.Mdrawmatrix =NULL; } Else if(Scaletype.center = =Mscaletype) { //Center Bitmap In view, no scaling.Mdrawmatrix =Mmatrix; Mdrawmatrix.settranslate ((int) ((vwidth-dwidth) * 0.5f + 0.5f), (int) ((vheight-dheight) * 0.5f + 0.5f)); } Else if(Scaletype.center_crop = =Mscaletype) {Mdrawmatrix=Mmatrix; floatScale ; floatDX = 0, dy = 0; if(Dwidth * vheight > Vwidth *dheight) { Scale= (float) Vheight/(float) Dheight; DX= (Vwidth-dwidth * scale) * 0.5f; } Else{ Scale= (float) Vwidth/(float) Dwidth; Dy= (Vheight-dheight * scale) * 0.5f; } mdrawmatrix.setscale (scale, scale); Mdrawmatrix.posttranslate ((int) (dx + 0.5f), (int) (dy + 0.5f)); } Else if(Scaletype.center_inside = =Mscaletype) {Mdrawmatrix=Mmatrix; floatScale ; floatDX; floatdy; if(Dwidth <= vwidth && dheight <=vheight) { Scale= 1.0f; } Else{ Scale= Math.min ((float) Vwidth/(float) Dwidth, (float) Vheight/(float) dheight); } DX= (int) ((Vwidth-dwidth * scale) * 0.5f + 0.5f); Dy= (int) ((Vheight-dheight * scale) * 0.5f + 0.5f); Mdrawmatrix.setscale (scale, scale); Mdrawmatrix.posttranslate (dx, dy); } Else { //Generate the required transform.Mtempsrc.set (0, 0, Dwidth, dheight); Mtempdst.set (0, 0, Vwidth, vheight); Mdrawmatrix=Mmatrix; Mdrawmatrix.setrecttorect (MTEMPSRC, MTEMPDST, Scaletypetoscaletofit (Mscaletype)); } } }
Segmented reading:
int dwidth = mdrawablewidth; int dheight = mdrawableheight; int vwidth = getwidth ()-Mpaddingleft- mpaddingright; int vheight = getheight ()-mpaddingtop- mpaddingbottom; Boolean fits = (Dwidth < 0 | | vwidth = = dwidth) && < 0 | | vheight = dheight);
1. In this step, the width and height of the picture object (that is, the Drawable object, which ImageView actually shows the Drawable object) is obtained, and is stored in dwidth and dheight. At the same time, the width and height of the display area of the ImageView control are obtained and stored in Vwidth and vheight.
if (dwidth <= 0 | | dheight <= 0 | | Scaletype.fit_xy == Mscaletype) { /* Span style= "color: #008000;" > If the drawable has no intrinsic size, or we ' re told to Scaletofit and then we just fill our entire view. */ Mdrawable.setbounds (0, 0 Vwidth, vheight); Mdrawmatrix = null ; else {
2. When the size of the picture is unknown or scaletype is fit_xy, the boundary of the drawable is set to the size of the control, and the picture matrix object is set to null. The SetBounds method is used to set the size of the drawable drawing area, which is a rectangular object. In ImageView's OnDraw method, the image represented by Drawable is eventually drawn to the area. Here, the picture is drawn to the imageview size of the area, that is, the so-called picture full display, filled with ImageView. Note that the matrix object is not manipulated here, but is set directly to NULL, so when ImageView's ScaleType is Fit_xy, Getimagematrix gets to an initial matrix object, The Getimagematrix method is as follows.
Public Matrix Getimagematrix () { ifnull) { returnnew Matrix (Matrix.identity_matrix); } return Mdrawmatrix; }
Else { // We need to does the scaling ourself, so has the drawable // use it n ative size. Mdrawable.setbounds (0, 0, dwidth, dheight);
3. When the ScaleType is not fit_xy and the picture width is not 0, the drawing area of the drawable is set to the actual size of the picture. Note: The actual size here does not refer to the file size, but rather refers to the source file size and the screen resolution after the calculation results.
if(Scaletype.matrix = =Mscaletype) { //Use the specified matrix As-is. if(Mmatrix.isidentity ()) {Mdrawmatrix=NULL; } Else{Mdrawmatrix=Mmatrix; } } Else if(Fits) {//the bitmap fits exactly, no transform needed.Mdrawmatrix =NULL; }
4. Next to determine whether ScaleType is a matrix, if the matrix type, the current drawing of the matrix will be assigned to Mmatrix,mmatrix is assigned by the Setimagematrix method. Then there is an if (fits) judgment, if fits is true, the picture size is equal to imageview size, and no scaling is required.
Else if (Scaletype.center = = Mscaletype) {// CENTER bitmap in view, no scaling. Mdrawmatrix = Mmatrix; Mdrawmatrix.settranslate ((int) ((vwidth-dwidth) * 0.5f + 0.5f), (int) (( Vheight-dheight) * 0.5f + 0.5f)); }
5. When the ScaleType type is center, the actual operation is to move the picture right and down, moving the values for the ImageView width, height and picture width, height difference of half, so that the effect of the center display. Note that center does not scale and only shifts, so the display size of the picture does not change, and when the picture is larger than the control, only the center part is displayed, which is larger than the part of the control.
Else if(Scaletype.center_crop = =Mscaletype) {Mdrawmatrix=Mmatrix; floatScale ; floatDX = 0, dy = 0; if(Dwidth * vheight > Vwidth *dheight) { Scale= (float) Vheight/(float) Dheight; DX= (Vwidth-dwidth * scale) * 0.5f; } Else{ Scale= (float) Vwidth/(float) Dwidth; Dy= (Vheight-dheight * scale) * 0.5f; } mdrawmatrix.setscale (scale, scale); Mdrawmatrix.posttranslate ((int) (dx + 0.5f), (int) (dy + 0.5f)); }
6. In Center_crop, Dwidth * vheight > Vwidth * dheight was first judged, what is the meaning of this judgment? In fact, it is easier to understand: Dwidth/vwidth>dheight/vheight, that is, whether the width of the picture is closer to the width of the control or the height of the picture compared to the height of the control, eventually will take a large difference, enlarge it to the corresponding value of the control. The other item will exceed the size of the control. After that, it is shifted so that an item that exceeds the size of the control is centered. Note: When the picture is larger than the control, it is scaled beyond the smaller items. The purpose of crop is to transform the width of the image so that one of the items is equal to the value of the control and the other is greater than the value of the control.
Else if(Scaletype.center_inside = =Mscaletype) {Mdrawmatrix=Mmatrix; floatScale ; floatDX; floatdy; if(Dwidth <= vwidth && dheight <=vheight) { Scale= 1.0f; } Else{ Scale= Math.min ((float) Vwidth/(float) Dwidth, (float) Vheight/(float) dheight); } DX= (int) ((Vwidth-dwidth * scale) * 0.5f + 0.5f); Dy= (int) ((Vheight-dheight * scale) * 0.5f + 0.5f); Mdrawmatrix.setscale (scale, scale); Mdrawmatrix.posttranslate (dx, dy); }
7. When Center_inside, if the picture height and width are smaller than the height of the control, the scale is offset only, and the offset is the same as in other cases. Otherwise, the scale will be scaled based on a larger gap between the picture and the control's width and height, with the result that one item is the same as the control and the other is less than the value of the control. This is just the opposite of Center_crop.
Else { // Generate the required transform. Mtempsrc.set (0, 0, Dwidth, dheight); Mtempdst.set (0, 0, Vwidth, vheight); = Mmatrix; Mdrawmatrix.setrecttorect (MTEMPSRC, MTEMPDST, Scaletypetoscaletofit (Mscaletype)); }
8. The last three kinds of fit_center, Fit_start, fit_end are called Setrecttorect to get the scaling offset matrix. Setrecttorect returns a matrix that represents the transformation matrix from rectangle mtempsrc to MTEMDST, and determines the scaling options based on the third parameter matrix.scaletofit.
The Matrix.scaletofit defines four options:
CENTER: Maintains the aspect ratio of the rectangle before the coordinate transformation, and fills the transformed rectangle to the fullest extent. At least one side overlaps with the target rectangle.
END: Maintains the aspect ratio of the rectangle before the coordinate transformation, and fills the transformed rectangle to the fullest extent. At least one side overlaps with the target rectangle. End provides the right bottom alignment.
FILL: The aspect ratio of the rectangle may be transformed to ensure that the transformation is consistent with the target matrix length and width.
START: Maintains the length-to-width ratio of the rectangle before the coordinate transformation, and fills the transformed rectangle to the fullest extent. At least one side overlaps with the target rectangle. Start provides top-left alignment.
ScaleType's Fit_center, Fit_start, and Fit_end correspond to the CENTER, END, and START respectively.
The bottom line is that scaletype essentially affects the Mdrawmatrix object in ImageView, which is used to transform the Drawable object into a matrix when it is drawn.
Android:scaletype related to Matrix