SoFunction
Updated on 2025-03-08

Android uses Glide to obtain the true width and height of the picture

Preface

Sometimes you need to obtain the width and height of the network image to set the size of the image display. Many people will directly use Glide's loading monitoring to get the width and height of the image, but what you get in this way is not the real width and height of the image, but the width and height of the image displayed after the ImageView. as follows:

    //Get the width and height of the image displayed in ImageView    (this)
        .load(imgUrl)
        .asBitmap()//Forcing Glide to return a Bitmap object        .listener(new RequestListener<String, Bitmap>() {
          @Override
          public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
            (TAG, "onException " + ());
            return false;
          }

          @Override
          public boolean onResourceReady(Bitmap bitmap, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
            int width = ();
            int height = ();
            (TAG, "width2 " + width); //400px
            (TAG, "height2 " + height); //400px
            return false;
          }
        }).into(mIv_img);

If you want to get the real width and height of the picture, you should use Glide's Target. as follows:

    //Get the real width and height of the picture    (this)
        .load(imgUrl)
        .asBitmap()//Forcing Glide to return a Bitmap object        .into(new SimpleTarget<Bitmap>() {
          @Override
          public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
            int width = ();
            int height = ();
            (TAG, "width " + width); //200px
            (TAG, "height " + height); //200px
          }
        });

Complete code

public class MainActivity extends AppCompatActivity {

  private ImageView mIv_img;
  String imgUrl = "/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=523024675,1399288021&fm=117&gp=";
  private String TAG = ().getSimpleName();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    mIv_img = (ImageView) findViewById(.iv_img);

    //Get the real width and height of the picture    (this)
        .load(imgUrl)
        .asBitmap()//Forcing Glide to return a Bitmap object        .into(new SimpleTarget<Bitmap>() {
          @Override
          public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
            int width = ();
            int height = ();
            (TAG, "width " + width); //200px
            (TAG, "height " + height); //200px
          }
        });

    //Get the width and height of the image displayed in ImageView    (this)
        .load(imgUrl)
        .asBitmap()//Forcing Glide to return a Bitmap object        .listener(new RequestListener<String, Bitmap>() {
          @Override
          public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
            (TAG, "onException " + ());
            return false;
          }

          @Override
          public boolean onResourceReady(Bitmap bitmap, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
            int width = ();
            int height = ();
            (TAG, "width2 " + width); //400px
            (TAG, "height2 " + height); //400px
            return false;
          }
        }).into(mIv_img);
  }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  android:
  xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <ImageView
    android:
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerInParent="true"
    android:scaleType="centerCrop"
    android:src="@mipmap/ic_launcher"/>

</RelativeLayout>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.