重新调整imageview的drawable的大小

做项目的过程中,由于原来使用下载可以直接返回bitmap,这样可以直接通过bitmap得到宽高,然后进行缩放处理,但是下载逻辑修改,不返回bitmap让我们叫bitmap传递给他,然后某人在框架中帮我们设置,但是此时就会有一个问题,bitmap得不到,设置之后的图片的大小就无从考量,使得我们需要通过imageview得到它身上的图像,然后做调整

好了,不说废话了,需要涉及到三个方面

  1. 图像的缩放

       bw 图像的宽, bh 图像的高 baseSize 预期的尺寸

         思路以后再补充

     2.将drawable转换成bitmap,然后重新显示

 代码如下:

  

public class ImageUtils {
    /**
     * 对bitmap进行处理
     */
    public static Bitmap scaleBitmap(Bitmap bitmap, int baseSize) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        if (baseSize == 0) {
            baseSize = AppConstant.IMG_BASE_SIZE;
        }

        if (width == height && width == baseSize) {
            // bitmap的尺寸就是预期的尺寸就直接返回不需要缩放
            LogUtils.log("不需要缩放");
            return bitmap;
        }
        // 1.图片是否超过限定值
        // 2.图片宽或高是否大于限定值
        int[] imageSize = calcImageSize(width, height, baseSize);
        // 这里就不能使用这个方式了,需要放大
        return Bitmap.createScaledBitmap(bitmap, imageSize[0], imageSize[1], true);
    }

    /**
     * 对bitmap进行处理
     */
    public static void scaleDrawable(ImageView imageView) {
        // 80*80的图片
        /*Bitmap bitmap1 = getBitMapFromIv(imageView);
        // bw = 480,bh = 80
        Bitmap bitmap2 = getBitMapFromIv2(imageView);
        // bw = 480,bh = 80
        ImageSize imageSize = getDrawableSize(imageView);
        // bw = 80,bh = 80
        scaleAu4(imageView, imageSize.getWidth(), imageSize.getHeight(), DisplayUtils.dip2px(UiUtils.getAppContext(), 120));
        LogUtils.log("aaaaaaaaaaaaaaaaaaaa");*/

        Drawable drawable = getDrawableFromIv(imageView);
        ImageSize imageSize = getSize(drawable);
        int[] images = calcImageSize(imageSize.getWidth(), imageSize.getHeight(), DisplayUtils.dip2px(UiUtils.getAppContext(), 120));
        // 将drawable 转换成bitma
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
        Bitmap bitmap = Bitmap.createBitmap(images[0], images[1], config);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, images[0], images[1]);
        drawable.draw(canvas);
        imageView.setImageBitmap(bitmap);
    }

    public static Drawable getDrawableFromIv(ImageView imageView) {
        return imageView.getDrawable();
    }

    public static Bitmap getBitMapFromIv(ImageView imageView) {
        imageView.setDrawingCacheEnabled(true);
        return imageView.getDrawingCache(true);
    }

    public static Bitmap getBitMapFromIv2(ImageView imageView) {
        imageView.setDrawingCacheEnabled(true);
        return imageView.getDrawingCache();
    }

    public static ImageSize getImageSize(ImageView imageView) {
        ImageSize imageSize = getDrawableSize(imageView);
        if (imageSize == null || imageSize.getWidth() == 0 || imageSize.getHeight() == 0) {
            imageSize = getBitMapSize(imageView);
        }
        return imageSize;
    }

    public static ImageSize getBitMapSize(ImageView imageView) {
        // 这里会有多种方式
        // 方式A
        Bitmap bitmap = imageView.getDrawingCache(true);
        if (bitmap != null) {
            return getSize(bitmap);
        }
        return null;
    }

    public static ImageSize getDrawableSize(ImageView imageView) {
        Drawable drawable = getDrawableFromIv(imageView);
        if (drawable != null) {
            return getSize(drawable);
        }
        return null;
    }

    public static ImageSize getSize(Drawable drawable) {
        if (drawable != null) {
            ImageSize imageSize = new ImageSize();
            imageSize.setWidth(drawable.getIntrinsicWidth());
            imageSize.setHeight(drawable.getIntrinsicHeight());
            return imageSize;
        }
        return null;
    }

    public static ImageSize getSize(Bitmap bitmap) {
        if (bitmap != null) {
            ImageSize imageSize = new ImageSize();
            imageSize.setWidth(bitmap.getWidth());
            imageSize.setHeight(bitmap.getHeight());
            return imageSize;
        }
        return null;
    }

    /**
     * 是否是需要绽放
     *
     * @param bitmap
     * @param baseSize
     * @return
     */
    public static boolean needScale(Bitmap bitmap, int baseSize) {
        if (bitmap != null && baseSize > 0) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            if (width == height && width == baseSize) {
                // bitmap的尺寸就是预期的尺寸就直接返回不需要缩放
                LogUtils.log("不需要缩放");
                return false;
            }
        }
        return true;
    }

    /**
     * 按比例缩放图片
     *
     * @param width    图片的宽
     * @param height   图片的高
     * @param baseSize 期望的大小(宽和高任何一边都不会超过这个值)
     * @return
     */
    public static int[] calcImageSize(int width, int height, int baseSize) {
        int[] size = new int[2];
        if (width > baseSize || height > baseSize) {
            if (width > baseSize) {
                int rot = width / baseSize;
                // 宽处理,高也要跟着处理
                width = baseSize;
                height /= rot;
            } else if (height > baseSize) {
                int rot = height / baseSize;
                // 宽处理,高也要跟着处理
                height = baseSize;
                width /= rot;
            }
        } else if (width < baseSize || height < baseSize) {
            // 这里是放大的逻辑需要多处理一步
            if (width < baseSize) {
                int rot = baseSize - width;
                // 宽处理,高也要跟着处理
                width = baseSize;
                int tmpHeight = height + rot;
                // 3.宽或高的任何一边都不能超过最大限制
                if (tmpHeight > baseSize) {
                    // 把超出的部分给去除
                    int tmpBaseSize = height - baseSize;
                    height += tmpBaseSize;
                    width -= tmpBaseSize;
                } else {
                    height += rot;
                }
            } else if (height < baseSize) {
                int rot = baseSize - height;
                // 宽处理,高也要跟着处理
                height = baseSize;
                int tmpWidth = width + rot;
                if (tmpWidth > baseSize) {
                    // 把超出的部分给去除
                    int tmpBaseSize = height - baseSize;
                    width += tmpBaseSize;
                    height -= tmpBaseSize;
                } else {
                    width += rot;
                }
            }
        }
        size[0] = width;
        size[1] = height;
        return size;
    }

    private Drawable zoomDrawable(Drawable drawable, int w, int h) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap oldbmp = drawableToBitmap(drawable);
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) w / width);
        float scaleHeight = ((float) h / height);
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
                matrix, true);
        return new BitmapDrawable(null, newbmp);
    }

    private Bitmap drawableToBitmap(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        Bitmap bitmap = Bitmap.createBitmap(width, height, config);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        return bitmap;
    }
}

对drawable的缩放只需要传入imageview即可



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值