Android 获取图片宽和高以及 ScaleType
2022-11-17 本文已影响0人
雨来
ScaleType
https://cloud.tencent.com/developer/article/2039262
获取图片宽和高
利用glide
Glide.with(getContext())
.asBitmap()
.load(qaBannerBean.imagePath)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
//height 就是高
int height = resource.getHeight();
Glide.with(getContext()).load(qaBannerBean.imagePath).into(ivBanner);
ivBanner.setVisibility(View.VISIBLE);
}
});
利用BitmapFactory
因为是直接把图片加载到内存容易OOM
参考:
https://www.jianshu.com/p/b49beee4a57e
等比例缩放
https://blog.csdn.net/qq_34995257/article/details/52414944
Glide.with(activity).load(yourUrl).asBitmap().into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
int imageWidth = resource.getWidth();
int imageHeight = resource.getHeight();
int height = ScreenUtils.getScreenWidth() * imageHeight / imageWidth;
ViewGroup.LayoutParams para = imageView.getLayoutParams();
para.height = height;
para.width = ScreenUtils.getScreenWidth();
imageView.setImageBitmap(resource);
}
});