通过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>


  • 2017-07-05 09:48:51

    CSS 元素垂直居中的 6种方法

    利用CSS进行元素的水平居中,比较简单,行级元素设置其父元素的text-align center,块级元素设置其本身的left 和 right margins为auto即可。本文收集了六种利用css进行元素的垂直居中的方法,每一种适用于不同的情况,在实际的使用过程中选择某一种方法即可。

  • 2017-07-06 10:02:18

    大白话讲解Promise(一)

    去年6月份, ES2015正式发布(也就是ES6,ES6是它的乳名),其中Promise被列为正式规范。作为ES6中最重要的特性之一,我们有必要掌握并理解透彻。本文将由浅到深,讲解Promise的基本概念与使用方法。

  • 2017-07-11 21:54:14

    MYSQL5.7版本sql_mode=only_full_group_by问题

    一旦开启 only_full_group_by ,感觉,group by 将变成和 distinct 一样,只能获取受到其影响的字段信息,无法和其他未受其影响的字段共存,这样,group by 的功能将变得十分狭窄了

  • 2017-07-14 13:51:58

    NodeJS连接MySQL出现Cannot enqueue Handshake after invoking quit.

    原因在于node连接上mysql后如果因网络原因丢失连接或者用户手工关闭连接后,原有的连接挂掉,需要重新连接;如下代码,每次访问结束都关闭,每次开始访问前重连接下,代码中没有监听连接的fatal错误,copy需谨慎

  • 2017-07-14 13:53:02

    nodejs解决mysql和连接池(pool)自动断开问题

    最近在做一个个人项目,数据库尝试使用了mongodb、sqlite和mysql。分享一下关于mysql的连接池用法。项目部署于appfog,项目中我使用连接池链接数据库,本地测试一切正常。上线以后,经过几次请求两个数据接口总是报503。一直不明就里,今天经过一番排查终于顺利解决了。