Picasso处理图片,按照设置的宽度比例来缩放

来源:互联网 发布:数据存储解决方案 nas 编辑:程序博客网 时间:2024/05/20 21:18
   Picasso.with(UIUtils.getContext())                    .load(serviceInShopPageListBean.getServiceDownUrl())                    .transform(transformation)                    .into(imageview);
    Transformation transformation = new Transformation() {        @Override        public Bitmap transform(Bitmap source) {            int targetWidth = imageview.getWidth();            if (source.getWidth() == 0) {                return source;            }            //如果图片大小大于等于设置的宽度,则按照设置的宽度比例来缩放            double aspectRatio = (double) source.getHeight() / (double) source.getWidth();            int targetHeight = (int) (targetWidth * aspectRatio);            if (targetHeight != 0 && targetWidth != 0) {                Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);                if (result != source) {                    // Same bitmap is returned if sizes are the same                    source.recycle();                }                return result;            } else {                return source;            }        }        @Override        public String key() {            return "transformation" + " desiredWidth";        }    };
阅读全文
1 0
原创粉丝点击