美文网首页Android深入Android架构师自定义控件
Glide 根据手机屏幕宽度,等比缩放ImageView

Glide 根据手机屏幕宽度,等比缩放ImageView

作者: 一个冬季 | 来源:发表于2019-08-28 17:04 被阅读0次
参考文章

Glide坑遇记:宽度铺满高度自适应 & GIF加载之坑
Glide - 图片重设大小 和 缩放

需求

公司需要根据手机屏幕的宽度,等比缩放图片的高度,如果图片的宽度 > 手机的宽度,是要等比缩放宽高。如果图片的宽度 <= 手机屏幕的宽度,不进行缩放

更新

2020-05-18

/**
 * @date 创建时间:2020/5/18 0018
 * @auther gaoxiaoxiong
 * @Descriptiion 根据传入的宽度,放大高度
 **/
static class PhotoViewImageViewTarget extends CustomTarget<Bitmap> {
        PhotoView photoView;
        int phoneWidth;
        public PhotoViewImageViewTarget(PhotoView photoView, int phoneWidth) {
            this.photoView = photoView;
            this.phoneWidth = phoneWidth;
        }

        @Override
        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
            if (resource!=null){
                //获取原图的宽高
                int resourceWidth = resource.getWidth();
                int resourceHeight = resource.getHeight();
                FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) photoView.getLayoutParams();
                float scaleWidth = 0.0f;
                scaleWidth = (float) phoneWidth / (float) resourceWidth;
                if (resourceWidth > 1000){
                    layoutParams.width = (int) (resourceWidth * scaleWidth);
                    layoutParams.height = (int) (resourceHeight * scaleWidth);
                }else {
                    if (resourceWidth > phoneWidth){
                        layoutParams.width = (int) (resourceWidth * scaleWidth);
                        layoutParams.height = (int) (resourceHeight * scaleWidth);
                    }else {
                        layoutParams.width = resourceWidth;
                        layoutParams.height = resourceHeight;
                    }
                }
                int height = resource.getHeight();//原始高度
                layoutParams.height = height;
                photoView.setLayoutParams(layoutParams);
                photoView.setImageBitmap(resource);
            }
        }

        @Override
        public void onLoadCleared(@Nullable Drawable placeholder) {

        }
    }
======以下为老版本
使用方法
   ScaleGeometricRatioTransform scaleGeometricRatioTransform = new ScaleGeometricRatioTransform(ScreentUtil.getInstance().getScreenWidth(mActivity),mActivity);
    Glide.with(SPMobileApplication.getInstance())
                            .load("图片地址")
                            .asBitmap()
                            .skipMemoryCache(true)
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .transform(scaleGeometricRatioTransform)
                            .listener(new RequestListener<String, Bitmap>() {
                                        @Override
                                        public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                                            return false;
                                        }

                                        @Override
                                        public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                                            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageView.getLayoutParams();
                                            layoutParams.width = resource.getWidth();
                                            layoutParams.height = resource.getHeight();
                                            imageView.setLayoutParams(layoutParams);
                                            return false;
                                        }
                                    })

                            .into(imageView);

ScreentUtil.getInstance().getScreenWidth(mActivity);是获取屏幕的宽度

代码实现
/**
 * @author : gaoxiaoxiong
 * @date :2019/8/28 0028
 * @description:根据手机屏幕,等比缩放ImageView,ImageView是横向拉满的
 **/
public class ScaleGeometricRatioTransform implements Transformation<Bitmap> {
    private int phoneWidth = 0;//显示的宽度
    private Context mContext;
    BitmapPool mBitmapPool = null;
    private WeakReference<Context> contextWeakReference;

    public ScaleGeometricRatioTransform(int phoneWidth, Context context) {
        contextWeakReference = new WeakReference<Context>(context);
        this.phoneWidth = phoneWidth;
        this.mContext = contextWeakReference.get();
        mBitmapPool = Glide.get(mContext).getBitmapPool();
    }

    @Override
    public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
        Bitmap source = resource.get();
        int resoreWidth = source.getWidth();
        int resoreHeight = source.getHeight();
        float scaleWidth = 0.0f, scaleHeight = 0.0f;
        scaleWidth = (float) phoneWidth / (float) resoreWidth;
        Matrix matrix = new Matrix();
        if (resoreWidth > 1000){
            matrix.postScale(scaleWidth, scaleWidth);
        }else {
            if (resoreWidth > phoneWidth) {
                matrix.postScale(scaleWidth, scaleWidth);
            }else {
                matrix.postScale(1.0f, 1.0f);
            }
        }
        Bitmap newbm = Bitmap.createBitmap(source, 0, 0, resoreWidth, resoreHeight, matrix, true);
        return BitmapResource.obtain(newbm, this.mBitmapPool);
    }

    @Override
    public String getId() {
        return this.getClass().getName();
    }
}



<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/iv_child_view_floorimageview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

小结

1、 Bitmap.createBitmap(source, 0, 0, resoreWidth, resoreHeight, matrix, true);
该方法是放入原始的宽高,后面的matrix者是对原始宽高进行缩放的操作
2、当你在Transformation进行图片放大缩小的操作,不会影响到ImageView的宽高,所以你要最后获取转换图片的宽高大小,进行动态的设置
3、如果不设置transform,直接写listener,从resource里面获取的是被压缩过的图片

相关文章

网友评论

    本文标题:Glide 根据手机屏幕宽度,等比缩放ImageView

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