1. 程式人生 > >畢加索(Picasso )妙用

畢加索(Picasso )妙用

效果圖:


 Picasso.with(context)
                .load(url)
                .placeholder(getDefaultImage(IMAGE_TYPE))
                .transform(new CropSquareTransformation())
                .noFade()
                .error(getDefaultImage(IMAGE_TYPE))
                .into(view,callback);

public class CropSquareTransformation implements Transformation {
    @Override
    public Bitmap transform(Bitmap source) {

        int targetWidth = 500;//目標最大寬度

        int targetHeight = 500;//目標最大高度

        if (source.getWidth() == 0 || source.getHeight() == 0) {
            return source;
        }

        if (source.getWidth() > source.getHeight()) {//橫向長圖
            if (source.getHeight() < targetHeight && source.getWidth() <= 400) {
                return source;
            } else {
                //如果圖片大小大於等於設定的高度,則按照設定的高度比例來縮放
                double aspectRatio = (double) source.getWidth() / (double) source.getHeight();
                int width = (int) (targetHeight * aspectRatio);
                if (width > targetWidth) { //對橫向長圖的寬度 進行二次限制
                    width = targetWidth;
                    targetHeight = (int) (width / aspectRatio);// 根據二次限制的寬度,計算最終高度
                }
                if (width != 0 && targetHeight != 0) {
                    Bitmap result = Bitmap.createScaledBitmap(source, width, targetHeight, false);
                    if (result != source) {
                        // Same bitmap is returned if sizes are the same
                        source.recycle();
                    }
                    return result;
                } else {
                    return source;
                }
            }
        } else {//豎向長圖
            //如果圖片小於設定的寬度,則返回原圖
            if (source.getWidth() < targetWidth && source.getHeight() <= 600) {
                return source;
            } else {
                //如果圖片大小大於等於設定的寬度,則按照設定的寬度比例來縮放
                double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
                int height = (int) (targetWidth * aspectRatio);
                if (height > targetHeight) {//對橫向長圖的高度進行二次限制
                    height = targetHeight;
                    targetWidth = (int) (height / aspectRatio);//根據二次限制的高度,計算最終寬度
                }
                if (height != 0 && targetWidth != 0) {
                    Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, height, 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 "desiredWidth" + " desiredHeight";
    }
}

import com.squareup.picasso.Callback;
public class PicassoCallBack implements Callback {
    @Override
    public void onSuccess() {
        LogUtil.e("PicassoCallBack onSuccess");
    }

    @Override
    public void onError() {
        LogUtil.e("PicassoCallBack onError");
    }
}