Android 利用 Glide 获取图片真正的宽高

本文介绍如何使用Glide正确获取网络图片的真实宽高,并对比了使用加载监听与Target方法的区别。通过实例展示了两种方法的不同结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

有时候需要获取网络图片的宽高来设置图片显示的大小,很多人会直接利用Glide的加载监听去拿图片的宽高,但是这样拿到的不是图片真正的宽高,而是图片显示在ImageView后的宽高。如下:

//获取图片显示在ImageView后的宽高
        Glide.with(this)
                .load(imgUrl)
                .asBitmap()//强制Glide返回一个Bitmap对象
                .listener(new RequestListener<String, Bitmap>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                        Log.d(TAG, "onException " + e.toString());
                        return false;
                    }

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

想要拿到图片真正的宽高,应该利用Glide的Target。如下:

//获取图片真正的宽高
        Glide.with(this)
                .load(imgUrl)
                .asBitmap()//强制Glide返回一个Bitmap对象
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
                        int width = bitmap.getWidth();
                        int height = bitmap.getHeight();
                        Log.d(TAG, "width " + width); //200px
                        Log.d(TAG, "height " + height); //200px
                    }
                });

完整代码

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private ImageView mIv_img;
    String imgUrl = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=523024675,1399288021&fm=117&gp=0.jpg";
    private String TAG = this.getClass().getSimpleName();

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

        //获取图片真正的宽高
        Glide.with(this)
                .load(imgUrl)
                .asBitmap()//强制Glide返回一个Bitmap对象
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
                        int width = bitmap.getWidth();
                        int height = bitmap.getHeight();
                        Log.d(TAG, "width " + width); //200px
                        Log.d(TAG, "height " + height); //200px
                    }
                });

        //获取图片显示在ImageView后的宽高
        Glide.with(this)
                .load(imgUrl)
                .asBitmap()//强制Glide返回一个Bitmap对象
                .listener(new RequestListener<String, Bitmap>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                        Log.d(TAG, "onException " + e.toString());
                        return false;
                    }

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

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值