Android uses Picasso to load network pictures and scale them equally

  • 2021-10-13 08:33:09
  • OfStack

When loading android pictures, due to the limited screen of mobile phones, when many large pictures are loaded, we require equal scaling, such as scaling the height according to a fixed width, so that the size ratio of the pictures can be scaled accordingly, but the pictures are not deformed. Obviously, it can't be implemented according to android: scaleType, because there will be many limitations, so you must write your own algorithm.

Scaling through Picasso

In fact, picasso provides such a method. Specifically, the transform method that displays Transformation.

(1) Get the width and height of network or local pictures first
(2) Obtain the desired target width
(3) Obtain the height of the target in proportion
(4) Create a new map according to the width and height of the target


Transformation transformation = new Transformation() {
    @Override
    public Bitmap transform(Bitmap source) {
        int targetWidth = mImg.getWidth();
        LogCat.i("source.getHeight()="+source.getHeight());
               LogCat.i("source.getWidth()="+source.getWidth());
               LogCat.i("targetWidth="+targetWidth);
        if(source.getWidth()==0){
          return source;
        }
        // If the picture is smaller than the set width, the original image is returned 
        if(source.getWidth()<targetWidth){
          return source;
        }else{
          // If the picture size is greater than or equal to the set width, it is scaled according to the set width ratio 
          double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
          int targetHeight = (int) (targetWidth * aspectRatio);
          if (targetHeight != 0 && targetWidth != 0) {
            Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
            if (result != source) {
              // Same bitmap is returned if sizes are the same
              source.recycle();
            }
            return result;
          } else {
            return source;
          }
        }
      }
    @Override
    public String key() {
      return "transformation" + " desiredWidth";
    }
  };

Then set transform in Picasso


Picasso.with(mContext)
        .load(imageUrl)
        .placeholder(R.mipmap.zhanwei)
        .error(R.mipmap.zhanwei)
        .transform(transformation)
        .into(viewHolder.mImageView);

Transformation This is a very powerful feature of Picasso, which allows you to image in load- > In the middle of into ImageView, this pair of pictures is transformed in series 1. For example, if you want to do Gaussian blur, add rounded corners, do gray processing, round pictures, etc., you can do it through Transformation.

Reference article: https://stackoverflow.com/questions/21889735/resize-image-to-full-width-and-variable-height-height-with-picasso

Summarize

The above is the site to introduce you Android using Picasso to load the network picture scaling method, I hope to help you, if you have any questions welcome to leave me a message, this site will reply to you in time!


Related articles: