最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android利用Glide獲取圖片真正的寬高的實(shí)例

 更新時(shí)間:2017年08月03日 09:05:53   作者:wildma  
本篇文章主要介紹了Android利用Glide獲取圖片真正的寬高的實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下

前言

有時(shí)候需要獲取網(wǎng)絡(luò)圖片的寬高來設(shè)置圖片顯示的大小,很多人會(huì)直接利用Glide的加載監(jiān)聽去拿圖片的寬高,但是這樣拿到的不是圖片真正的寬高,而是圖片顯示在ImageView后的寬高。如下:

    //獲取圖片顯示在ImageView后的寬高
    Glide.with(this)
        .load(imgUrl)
        .asBitmap()//強(qiáng)制Glide返回一個(gè)Bitmap對(duì)象
        .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);

想要拿到圖片真正的寬高,應(yīng)該利用Glide的Target。如下:

    //獲取圖片真正的寬高
    Glide.with(this)
        .load(imgUrl)
        .asBitmap()//強(qiáng)制Glide返回一個(gè)Bitmap對(duì)象
        .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()//強(qiáng)制Glide返回一個(gè)Bitmap對(duì)象
        .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()//強(qiáng)制Glide返回一個(gè)Bitmap對(duì)象
        .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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

大余县| 嘉峪关市| 阳东县| 伊吾县| 昌宁县| 子长县| 望江县| 囊谦县| 嵊泗县| 拜城县| 绥德县| 磴口县| 宝清县| 拉孜县| 仙游县| 高平市| 云霄县| 鄯善县| 双江| 泗洪县| 海原县| 水城县| 彩票| 伊金霍洛旗| 玉溪市| 黑水县| 筠连县| 新郑市| 扬州市| 延寿县| 东山县| 凤翔县| 德兴市| 南平市| 三穗县| 康定县| 阜康市| 株洲市| 南宁市| 石柱| 信宜市|