本篇文章为大家展示了怎么在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 = "/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>
上述内容就是怎么在Android中利用Glide获取图片的宽高,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
当前文章:怎么在Android中利用Glide获取图片的宽高-创新互联
文章源于:https://www.cdcxhl.com/article20/hghjo.html
成都网站建设公司_创新互联,为您提供电子商务、网站营销、企业网站制作、外贸网站建设、网站制作、App设计
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联