美文网首页
Android 获取图片宽和高以及 ScaleType

Android 获取图片宽和高以及 ScaleType

作者: 雨来 | 来源:发表于2022-11-17 11:43 被阅读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);
                        }
                    });
    
    

    相关文章

      网友评论

          本文标题:Android 获取图片宽和高以及 ScaleType

          本文链接:https://www.haomeiwen.com/subject/usyyxdtx.html