A discussion on the relationship between Glide Centercrop Fitcenter and ImageView scaletype, mutual influence

Source: Internet
Author: User

Reprint please indicate the source, thank http://blog.csdn.net/harryweasley/article/details/71526216

In the process of using glide, you must have seen the following code method:

        ImageView ImageView = (ImageView) Findviewbyid (r.id.image);
        Imageview.setscaletype (ImageView.ScaleType.FIT_START);
        Glide.with (this). Load ("Https://zhitu.isux.us/assets/img/imgSample/test-60.jpg"). Fitcenter (). to (ImageView);

ImageView have their own scaletype,glide and have their own fitcenter or centercrop, that the relationship between the two is how, whether it will affect each other, this blog to explore the problem.

Glide provides two standard options for dealing with image display problems, one is the Centercrop one is fitcenter: centercrop

Centercrop () is a cropping technique that scales the image so that it fills within the ImageView bounds and trims the extra parts. The ImageView will be completely populated, but the image may not be fully displayed. Fitcenter

Fitcenter () is a cropping technique that scales an image so that the image is measured to be equal to or less than the ImageView boundary range. The image will be fully displayed, but may not fill the entire ImageView.

Look at the beginning of this blog three code, we first look at Glide source Fitcenter method:
Com.bumptech.glide.DrawableRequestBuilder

Public drawablerequestbuilder<modeltype> Fitcenter () {
        return transform (Glide.getdrawablefitcenter ());
    } Public
drawablerequestbuilder<modeltype> transform (TRANSFORMATION<GIFBITMAPWRAPPER> Transformation) {
        super.transform (transformation);
        return this;
    }

Drawablerequestbuilder Transform call the parent class transform, continue to see the source of the parent class:
Com.bumptech.glide.GenericRequestBuilder

/** * Transform resources with the given {@link transformation}s. Replaces any existing Tran
     Sformation or * transformations. * Convert resources with a given transformation. Replace any existing transformation or transformations.
     ⭐1 * @param transformations the transformations to apply in order.
     * @return this request builder. */Public Genericrequestbuilder<modeltype, DataType, ResourceType, transcodetype> transform (TRANSFO
        Rmation<resourcetype> transformations) {//This variable is used after, please note ⭐2 istransformationset = true;
        if (transformations.length = = 1) {transformation = transformations[0];
        } else {transformation = new multitransformation<resourcetype> (transformations);
    } return this; }

We see the Fitcenter Object Glide.getdrawablefitcenter () added to the transformation, and similarly, if it is Centercrop, Then Glide.getdrawablecentercrop () is added to Transformtion, and glide will download the image from the network based on the incoming transformtion in a picture processing format.

Fitcenter Source first analysis here, and now we enter the glide source to see what the into method has done,

Com.bumptech.glide.GenericRequestBuilder

Public target<transcodetype> into (ImageView view) {
        util.assertmainthread ();
        if (view = null) {
            throw new IllegalArgumentException ("must pass in a non null View");
        }

        if (!istransformationset && view.getscaletype () = null) {
            switch (View.getscaletype ()) {case
                Center_ CROP:
                    applycentercrop ();
                    break;
                Case Fit_center: Case
                Fit_start: Case
                fit_end:
                    applyfitcenter ();
                    break;
                $CASES-omitted$
                Default:
                    //Do nothing.
            }

        } return into (Glide.buildimageviewtarget (view, Transcodeclass));
    }

First look at this judging if (!istransformationset && view.getscaletype () = null)
By the source of ImageView, each imageview has a default fitcenter Scaletype,imageview Source:

Public ImageView (Context context) {
        super (context);
        Initimageview ();
    }

    Public ImageView (context context, @Nullable AttributeSet attrs) {This
        (context, attrs, 0);
    }

    Public ImageView (context context, @Nullable AttributeSet attrs, int. defstyleattr) {This
        (context, Attrs, defstyleattr, 0);
    }

    Public ImageView (context context, @Nullable attributeset attrs, int defstyleattr,
            int defstyleres) {
        super ( Context, Attrs, defstyleattr, defstyleres);

        Initimageview ();
        ...
        }
    private void Initimageview () {
         ...
         Set Fit_center to the default scaletype
         mscaletype = scaletype.fit_center;
         ...
      }

As we can see, each imageview will default to Mscaletype = Scaletype.fit_center

Learned from the above unview.getscaletype ()! = null must be True,

The istransformationset here is not very familiar, yes, is the above ⭐2 said to use the variable,

It can be seen from this that if glide executes centercrop or fitcenter, then if (!istransformationset && view.getscaletype () = null) The code inside will not be executed.

So if glide does not execute Centercrop and fitcenter,if (!istransformationset && view.getscaletype ()! = null) The code inside will be executed and we will see that He did two things according to the incoming ImageView ScaleType, performing Applycentercrop () or Applyfitcenter (), we went to the source to see:

Com.bumptech.glide.GenericRequestBuilder

void Applycentercrop () {
        //to being implemented by subclasses when possible.
    }

    void Applyfitcenter () {
        //to being implemented by subclasses when possible.
    }

We see is an empty method, the implementation is implemented in the subclass, then we go to the sub-class Drawablerequestbuilder see, the source code is as follows:
Com.bumptech.glide.DrawableRequestBuilder

@Override
    void Applyfitcenter () {
        fitcenter ();
    }

    @Override
    void Applycentercrop () {
        centercrop ();
    }

See here there is no epiphany, from here we can come to a conclusion if we do not give glide manually add Centercrop and Fitcenter methods, Glide will perform centercrop or Fitcenter methods according to the scaletype of the incoming imageview. (I know you will say, ScaleType not only center_crop,fit_center,fit_start,fit_end, this I know Ha, will continue to say later.) )

Do you think this is the role of ImageView ScaleType in glide, wrong, this scaletype at the end of the picture set to ImageView also played a role.
In the glide get to the picture resources, will execute ImageView setimagedrawable will set the picture in, the source code is as follows:

Com.bumptech.glide.request.target.GlideDrawableImageViewTarget

/**
     * Sets the drawable on the view using
     * {@link android.widget.imageview#setimagedrawable ( android.graphics.drawable.Drawable)}.
     *
     * @param resource The {@link android.graphics.drawable.Drawable} to display in the view.
     *
    /@Override protected void Setresource (glidedrawable Resource) {
        view.setimagedrawable (Resource);
    }

A conclusion can be summed up: The ScaleType of ImageView plays at least once in the process of glide loading pictures to ImageView.

After several tests, I came to the conclusion that if ImageView's scaletype is not centercrop and glide did not call the Centercrop method, then the image format obtained by glide from the server is Fitcenter format, Finally, the format of the ImageView picture is set to the ImageView of the scaletype to be determined.

I don't know if you notice the sign of ⭐1, a note over there illustrates a problem, if glide simultaneously executes the centercrop and Fitcenter methods, then who is behind and downloads the picture in whose format.
To make an analogy, the code looks like this:

Glide.with (this). Load ("Https://zhitu.isux.us/assets/img/imgSample/test-60.jpg"). Fitcenter (). Centercrop (). into ( ImageView);

The images downloaded from the network are downloaded in centercrop format.

This blog concludes with an analysis of the 8 ScaleType of ImageView:

CENTER The image is placed in the view, but no scaling is performed.
Center_crop Scales the image (preserving the aspect ratio of the image) so that the size (width and height) of the image is equal to or greater than the corresponding size of the view (minus the fill). So it will be full of imageview, but may show incomplete pictures
Center_inside Shrink the image evenly (keeping the image's aspect ratio) so that the size (width and height) of the image is equal to or less than the corresponding size of the view (minus the fill).
Fit_center Maintain the aspect ratio of the original image to calculate a scale, but also make sure that the original image is fully placed in the target view, at least one axis (x or y) will fit exactly. The result is centered on the target view. Default Properties
Fit_end Maintain the aspect ratio of the original image to calculate a scale, but also make sure that the original image is fully placed in the target view, at least one axis (x or y) will fit exactly. End aligns the results to the bottom right edge of the target view.
Fit_start Maintain the aspect ratio of the original image to calculate a scale, but also make sure that the original image is fully placed in the target view, at least one axis (x or y) will fit exactly. Start aligns the results to the top-left edge of the target view.
Fit_xy scale x and y independently so that the original image matches the target exactly. This may change the aspect ratio of the original image.
MATRIX Scaled using the image matrix when drawing.
Finally, I'll summarize this blog post again:

Glide downloaded from the network to the picture, will be in two formats to download, one is Centercrop, one is fitcenter, once downloaded, set to ImageView, will also be based on ImageView ScaleType to establish the picture location.
If glide has not manually called Centercrop and fitcenter, then glide the image format downloaded from the network, determined by the ImageView ScaleType, if ScaleType is Center_crop, So glide download the picture with Centercrop, if ScaleType is fit_center,fit_start,fit_end, then glide download the picture in fitcenter format. However, according to my test results found that if ScaleType is Center,center_inside,matrix,glide is also in the fitcenter format to download pictures.

Reference article:
Https://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Https://futurestud.io/tutorials/glide-image-resizing-scaling
Http://www.jianshu.com/p/96fc561eada1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.