通过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-11-12 02:56:39

    使用.htaccess重定向后无法显示图片,CSS失效,该如何处理

    现在我需要把这个域名泛解析到blog目录(*.mydomain.org),同时保持另外两个目录的解析不变。尝试对最后一段作以下修改后(前面的内容不变),出现问题:另两个目录中的网站内的图片无法显示,CSS全部失效。

  • 2019-11-14 11:21:34

    vue中的this指向问题

    ※ 对于普通函数(包括匿名函数),this指的是直接的调用者,在非严格模式下,如果没有直接调用者,this指的是window。showMessage1()里setTimeout使用了匿名函数,this指向 window。 ※ 箭头函数是没有自己的this,在它内部使用的this是由它定义的宿主对象决定。showMessage2()里定义的箭头函数宿主对象为vue实例,所以它里面使用的this指向vue实例。

  • 2019-11-18 23:18:49

    spring boot中读取配置信息一

    首先我们都知道一个常识,那就是每个人都有自己的年龄,比如我们现在的业务需求是查询所有年龄大于20的人的相关信息,如果我们选择通过配置文件来配置这个值为20的常量的话,我们该如何配置和如何从配置文件中获取这个值呢?,application.yml的内容如下(注意 “age:“ 和 “20“ 之间需要一个空格,yml的语法 ):

  • 2019-11-19 01:20:18

    java8 forEach、filter、map

    filter()、findAny()、orElse()配合使用,可以根据条件获取某个元素,如果没有返回指定的值。

  • 2019-11-19 01:24:01

    使用JAVA8 filter对List多条件筛选

    记录项目开发的过程中遇到的一些问题及解决方法,由于公司操作数据库都是统一使用工具生成的存在一些多表查询模糊查询,这些操作只能在集合方面下手了,比如发送邮件记录方面查询,对用户的名字及邮件模糊检索 年龄匹配查询。