本篇文章为大家展示了怎么在Android中利用Glide获取图片的宽高,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
//获取图片显示在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 = "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, "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>
上述内容就是怎么在Android中利用Glide获取图片的宽高,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。