国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android利用Glide獲取圖片真正的寬高的實例

2019-10-22 18:31:00
字體:
來源:轉載
供稿:網友

前言

有時候需要獲取網絡圖片的寬高來設置圖片顯示的大小,很多人會直接利用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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。

 

注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜春市| 陆河县| 青田县| 中江县| 鹿邑县| 光山县| 东宁县| 上饶市| 治县。| 朝阳县| 武宁县| 磐安县| 池州市| 保德县| 津南区| 资中县| 南皮县| 本溪| 苗栗县| 隆化县| 三穗县| 渑池县| 林西县| 乐安县| 湖南省| 米易县| 屏东县| 孟村| 蒙自县| 祁连县| 山东| 普兰县| 类乌齐县| 隆子县| 略阳县| 东丽区| 龙里县| 綦江县| 天等县| 萨迦县| 綦江县|