通过glide获取图片显示后的真正宽高

2021-01-14 06:15:04

参考地址 Android利用Glide获取图片真正的宽高的实例

原来我都是通过缩放比例。来计算图片的展示后的真实宽高。

今天看到用glide也能巧妙的获得宽高,省区了再计算这一步。

下一次遇到可以直接拿glide尝试下。


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//获取图片显示在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。如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
//获取图片真正的宽高
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  android:id="@+id/activity_main"
  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>


  • 2019-04-30 11:23:36

    elasticsearch和analysis-ik的安装使用

    全文搜索和中文分词主要介绍了两组全文搜索加中文分词方案; TNTSearch+jieba-php这套组合对于博客这类的小项目基本够用了;

  • 2019-04-30 11:42:24

    php7+laravel+coreseek(sphinx)中文搜索初步实现(Linux)

    官网www.coreseek.cn已不能下载,所以需从网上找资源, 注意的一点是,笔者安装coreseek-3.2.14版本后,使用时提示client版本高于server版本的错误, php的sphinx扩展,为使用者,为client;coreseek是系统服务,为server

  • 2019-04-30 13:55:13

    浅谈mysql fulltext全文索引优缺点

    为什么会注意到mysql的fulltext? nima, 还是上次innodb转成tokudb引擎的事,这次alter修改表引擎的时候,提示percona tokudb是不支持fulltext索引的.

  • 2019-04-30 18:56:52

    elasticsearch文档操作

    使用了Elasticsearch提供的一整套强大的REST API,本文继续来看通过这一套API如何完成文档的基本操作。

  • 2019-05-05 14:04:11

    PHP使用CURL模拟POST/GET/PUT/DELETE方式提交数据

    最近因为工作需要,调用网盘接口来上传文件,我用了CURL库, 当然在用CURL库之前必须要在php中启用 cURL 设置 可以通过使用php_info()函数来得到cURL信息,如果看不到cURL信息的话,那么需要设置PHP并开启这个库。在Windows平台下,需要改一改php.ini文件的设置,找到 php_curl.dll,并取消前面的分号注释就行了。