一、开源工具——Luban(鲁班)——仿微信朋友圈策略
GitHub的地址:https://github.com/Curzibn/Luban
导入方法:
- 依赖gradle
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.6'
compile 'top.zibin:Luban:1.0.7'
- 导入library:下载library
使用方法:
- Listener方式,调用监听即可
Luban.get(this)
.load(File) //传人要压缩的图片
.putGear(Luban.THIRD_GEAR) //设定压缩档次,默认三挡
.setCompressListener(new OnCompressListener() { //设置回调
@Override
public void onStart() {
//TODO 压缩开始前调用,可以在方法内启动 loading UI
}
@Override
public void onSuccess(File file) {
//TODO 压缩成功后调用,返回压缩后的图片文件
}
@Override
public void onError(Throwable e) {
//TODO 当压缩过去出现问题时调用
}
}).launch(); //启动压缩
- RxJava方式,自行随意控制线程
Luban.get(this)
.load(file)
.putGear(Luban.THIRD_GEAR)
.asObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
}
})
.onErrorResumeNext(new Func1<Throwable, Observable<? extends File>>() {
@Override
public Observable<? extends File> call(Throwable throwable) {
return Observable.empty();
}
})
.subscribe(new Action1<File>() {
@Override
public void call(File file) {
//TODO 压缩成功后调用,返回压缩后的图片文件
}
});
- 与glide相配合使用:在onSuccess的方法里用:
Glide.with(Context context).load(File file).into(ImageView imageview);
二、用代码方式
//压缩图片尺寸
public Bitmap compressBySize(String pathName, int targetWidth,
int targetHeight) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;// 不去真的解析图片,只是获取图片的头部信息,包含宽高等;
Bitmap bitmap = BitmapFactory.decodeFile(pathName, opts);
// 得到图片的宽度、高度;
float imgWidth = opts.outWidth;
float imgHeight = opts.outHeight;
// 分别计算图片宽度、高度与目标宽度、高度的比例;取大于等于该比例的最小整数;
int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);
int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);
opts.inSampleSize = 1;
if (widthRatio > 1 || widthRatio > 1) {
if (widthRatio > heightRatio) {
opts.inSampleSize = widthRatio;
} else {
opts.inSampleSize = heightRatio;
}
}
//设置好缩放比例后,加载图片进内容;
opts.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(pathName, opts);
return bitmap;
}
GitHub的地址:https://github.com/Curzibn/Luban
compile 'io.reactivex:rxjava:1.1.6'
compile 'top.zibin:Luban:1.0.7'
.load(File) //传人要压缩的图片
.putGear(Luban.THIRD_GEAR) //设定压缩档次,默认三挡
.setCompressListener(new OnCompressListener() { //设置回调
@Override
public void onStart() {
//TODO 压缩开始前调用,可以在方法内启动 loading UI
}
@Override
public void onSuccess(File file) {
//TODO 压缩成功后调用,返回压缩后的图片文件
}
@Override
public void onError(Throwable e) {
//TODO 当压缩过去出现问题时调用
}
}).launch(); //启动压缩
.load(file)
.putGear(Luban.THIRD_GEAR)
.asObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
}
})
.onErrorResumeNext(new Func1<Throwable, Observable<? extends File>>() {
@Override
public Observable<? extends File> call(Throwable throwable) {
return Observable.empty();
}
})
.subscribe(new Action1<File>() {
@Override
public void call(File file) {
//TODO 压缩成功后调用,返回压缩后的图片文件
}
});
public Bitmap compressBySize(String pathName, int targetWidth,
int targetHeight) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;// 不去真的解析图片,只是获取图片的头部信息,包含宽高等;
Bitmap bitmap = BitmapFactory.decodeFile(pathName, opts);
// 得到图片的宽度、高度;
float imgWidth = opts.outWidth;
float imgHeight = opts.outHeight;
// 分别计算图片宽度、高度与目标宽度、高度的比例;取大于等于该比例的最小整数;
int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);
int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);
opts.inSampleSize = 1;
if (widthRatio > 1 || widthRatio > 1) {
if (widthRatio > heightRatio) {
opts.inSampleSize = widthRatio;
} else {
opts.inSampleSize = heightRatio;
}
}
//设置好缩放比例后,加载图片进内容;
opts.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(pathName, opts);
return bitmap;
}