本篇文章為大家展示了怎么在Android中利用Glide獲取圖片的寬高,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

//獲取圖片顯示在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, "width3 " + 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 = "/file/tupian/20230213/error.html";
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, "width3 " + 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>
上述內(nèi)容就是怎么在Android中利用Glide獲取圖片的寬高,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當前文章:怎么在Android中利用Glide獲取圖片的寬高-創(chuàng)新互聯(lián)
文章源于:http://chinadenli.net/article20/hghjo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供電子商務、網(wǎng)站營銷、企業(yè)網(wǎng)站制作、外貿(mào)網(wǎng)站建設、網(wǎng)站制作、App設計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容