package com.cmb.zh.common.util;
import java.io.File;
import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;
import com.cmb.zh.session.common.ProfilePhotoManager;
import com.cmb.zhsdk.magi.task.ICancelable;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
public class PicassoImgLoadManagerImpl implements ImgLoadManager {
public static class PicassoImgLoadReqImpl implements ImgLoadReq {
private final RequestCreator mCreator;
PicassoImgLoadReqImpl(RequestCreator picassoCreator) {
mCreator = picassoCreator;
}
@Override
public ImgLoadReq placeholder(int placeholderResId) {
mCreator.placeholder(placeholderResId);
return this;
}
@Override
public ImgLoadReq error(int errorResId) {
mCreator.error(errorResId);
return this;
}
/**
* 设置tag(多个加载请求可设置相同tag),用于开启或结束图片加载
*
* @param tag
* @return
*/
@Override
public ImgLoadReq tag(Object tag) {
mCreator.tag(tag);
return this;
}
/**
* 自适应控件宽高设置图片(wrap_content 无法计算)
*
* @return
*/
@Override
public ImgLoadReq fit() {
mCreator.fit();
return this;
}
@Override
public ImgLoadReq resize(int width, int height) {
mCreator.resize(width, height);
return this;
}
@Override
public ImgLoadReq centerInside() {
mCreator.centerInside();
return this;
}
@Override
public ImgLoadReq centerCrop() {
mCreator.centerCrop();
return this;
}
@Override
public ImgLoadReq rotate(float degrees) {
mCreator.rotate(degrees);
return this;
}
@Override
public ICancelable into(ImageView imgView) {
mCreator.into(imgView);
return null;
}
@Override
public ICancelable into(LoadListener listener) {
return null;
}
}
private final Picasso picasso;
public PicassoImgLoadManagerImpl(Context context) {
picasso = new Picasso.Builder(context).build();
}
/**
* 加载图片
*
* @param path
* @return
*/
public ImgLoadReq load(String path) {
return new PicassoImgLoadReqImpl(picasso.load(path));
}
/**
* 加载图片
*
* @param path
* @return
*/
public ImgLoadReq load(Uri path) {
return new PicassoImgLoadReqImpl(picasso.load(path));
}
/**
* 加载本地图片
*
* @return
*/
public ImgLoadReq loadFile(String path) {
return new PicassoImgLoadReqImpl(picasso.load(new File(path)));
}
/**
* 加载图片
*
* @param resourceId 需要加载的资源id
* @return
*/
public ImgLoadReq load(int resourceId) {
return new PicassoImgLoadReqImpl(picasso.load(resourceId));
}
/**
* 开启加载
*
* @param tag
*/
public void resumeLoadByTag(Object tag) {
picasso.resumeTag(tag);
}
/**
* 停止加载
*
* @param tag
*/
public void pauseLoadByTag(Object tag) {
picasso.pauseTag(tag);
}
// /**
// * 裁剪图片
// *
// * @param mContext
// * 上下文
// * @param path
// * 图片路径
// * @param mImageView
// * 控件
// */
// public static void loadImageViewCrop(Context mContext, String path,
// ImageView mImageView) {
// Picasso.with(mContext).load(path).transform(new CropImageView())
// .into(mImageView);
// }
//
// /**
// * 自定义图片裁剪
// */
// public static class CropImageView implements Transformation {
//
// @Override
// public Bitmap transform(Bitmap source) {
// int size = Math.min(source.getWidth(), source.getHeight());
// int x = (source.getWidth() - size) / 2;
// int y = (source.getHeight() - size) / 2;
//
// Bitmap newBitmap = Bitmap.createBitmap(source, x, y, size, size);
//
// if (newBitmap != null) {
// // 内存回收
// source.recycle();
// }
// return newBitmap;
// }
//
// @Override
// public String key() {
//
// return "lgl";
// }
// }
}
import java.io.File;
import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;
import com.cmb.zh.session.common.ProfilePhotoManager;
import com.cmb.zhsdk.magi.task.ICancelable;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
public class PicassoImgLoadManagerImpl implements ImgLoadManager {
public static class PicassoImgLoadReqImpl implements ImgLoadReq {
private final RequestCreator mCreator;
PicassoImgLoadReqImpl(RequestCreator picassoCreator) {
mCreator = picassoCreator;
}
@Override
public ImgLoadReq placeholder(int placeholderResId) {
mCreator.placeholder(placeholderResId);
return this;
}
@Override
public ImgLoadReq error(int errorResId) {
mCreator.error(errorResId);
return this;
}
/**
* 设置tag(多个加载请求可设置相同tag),用于开启或结束图片加载
*
* @param tag
* @return
*/
@Override
public ImgLoadReq tag(Object tag) {
mCreator.tag(tag);
return this;
}
/**
* 自适应控件宽高设置图片(wrap_content 无法计算)
*
* @return
*/
@Override
public ImgLoadReq fit() {
mCreator.fit();
return this;
}
@Override
public ImgLoadReq resize(int width, int height) {
mCreator.resize(width, height);
return this;
}
@Override
public ImgLoadReq centerInside() {
mCreator.centerInside();
return this;
}
@Override
public ImgLoadReq centerCrop() {
mCreator.centerCrop();
return this;
}
@Override
public ImgLoadReq rotate(float degrees) {
mCreator.rotate(degrees);
return this;
}
@Override
public ICancelable into(ImageView imgView) {
mCreator.into(imgView);
return null;
}
@Override
public ICancelable into(LoadListener listener) {
return null;
}
}
private final Picasso picasso;
public PicassoImgLoadManagerImpl(Context context) {
picasso = new Picasso.Builder(context).build();
}
/**
* 加载图片
*
* @param path
* @return
*/
public ImgLoadReq load(String path) {
return new PicassoImgLoadReqImpl(picasso.load(path));
}
/**
* 加载图片
*
* @param path
* @return
*/
public ImgLoadReq load(Uri path) {
return new PicassoImgLoadReqImpl(picasso.load(path));
}
/**
* 加载本地图片
*
* @return
*/
public ImgLoadReq loadFile(String path) {
return new PicassoImgLoadReqImpl(picasso.load(new File(path)));
}
/**
* 加载图片
*
* @param resourceId 需要加载的资源id
* @return
*/
public ImgLoadReq load(int resourceId) {
return new PicassoImgLoadReqImpl(picasso.load(resourceId));
}
/**
* 开启加载
*
* @param tag
*/
public void resumeLoadByTag(Object tag) {
picasso.resumeTag(tag);
}
/**
* 停止加载
*
* @param tag
*/
public void pauseLoadByTag(Object tag) {
picasso.pauseTag(tag);
}
// /**
// * 裁剪图片
// *
// * @param mContext
// * 上下文
// * @param path
// * 图片路径
// * @param mImageView
// * 控件
// */
// public static void loadImageViewCrop(Context mContext, String path,
// ImageView mImageView) {
// Picasso.with(mContext).load(path).transform(new CropImageView())
// .into(mImageView);
// }
//
// /**
// * 自定义图片裁剪
// */
// public static class CropImageView implements Transformation {
//
// @Override
// public Bitmap transform(Bitmap source) {
// int size = Math.min(source.getWidth(), source.getHeight());
// int x = (source.getWidth() - size) / 2;
// int y = (source.getHeight() - size) / 2;
//
// Bitmap newBitmap = Bitmap.createBitmap(source, x, y, size, size);
//
// if (newBitmap != null) {
// // 内存回收
// source.recycle();
// }
// return newBitmap;
// }
//
// @Override
// public String key() {
//
// return "lgl";
// }
// }
}