Android 使用Picasso加载网络图片等比例缩放

来源:互联网 发布:打鸟汽枪650淘宝 编辑:程序博客网 时间:2024/05/16 02:00
  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";        }    };
Picasso.load(serviceInShopPageListBean.getServiceDownUrl()).transform(transformation).into(imageview);