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

  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);

你可能感兴趣的:(Android图片加载)