Fresco
监听下载事件
定义一个ControllerListener,继承自BaseControllerListener。
ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
@Override
public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable animatable) {
super.onFinalImageSet(id, imageInfo, animatable);
if (animatable instanceof AnimatedDrawable2) {
AnimatedDrawable2 animatedDrawable = (AnimatedDrawable2) animatable;
//获取动图的长度
long loopDurationMs = animatedDrawable.getLoopDurationMs();
//设置循环次数
animatedDrawable.setAnimationBackend(new LoopCountModifyingBackend(animatedDrawable.getAnimationBackend(), 1));
}
}
// 如果允许呈现渐进式JPEG,同时图片也是渐进式图片,onIntermediateImageSet会在每个扫描被解码后回调。
@Override
public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
Log.d(TAG, "Intermediate image received");
}
@Override
public void onFailure(String id, Throwable throwable) {
super.onFailure(id, throwable);
}
};
Uri uri = Uri.fromFile(localFile);
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setControllerListener(controllerListener)
.setUri(uri)
.build();
mSimpleDraweeView.setController(controller);
Android 无法绘制长或宽大于2048像素的图片。这是由OpenGL渲染系统限制的,如果它超过了这个界限,Fresco会对它进行Resize。
修改图片
//修改图片
BasePostprocessor postProcessor = new BasePostprocessor() {
@Override
public String getName() {
return "CustomPostProcessor";
}
@Override
public void process(Bitmap bitmap) {
for (int x = 0; x < bitmap.getWidth(); x += 2) {
for (int y = 0; y < bitmap.getHeight(); y += 2) {
bitmap.setPixel(x, y, Color.RED);
}
}
}
@Override
public void process(Bitmap destBitmap, Bitmap sourceBitmap) {
for (int x = 0; x < destBitmap.getWidth(); x++) {
for (int y = 0; y < destBitmap.getHeight(); y++) {
destBitmap.setPixel(destBitmap.getWidth() - x, y, sourceBitmap.getPixel(x, y));
}
}
}
@Override
public CloseableReference<Bitmap> process(Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory) {
CloseableReference<Bitmap> bitmapRef = bitmapFactory.createBitmap(
sourceBitmap.getWidth() / 2,
sourceBitmap.getHeight() / 2);
try {
Bitmap destBitmap = bitmapRef.get();
for (int x = 0; x < destBitmap.getWidth(); x += 2) {
for (int y = 0; y < destBitmap.getHeight(); y += 2) {
destBitmap.setPixel(x, y, sourceBitmap.getPixel(x, y));
}
}
return CloseableReference.cloneOrNull(bitmapRef);
} finally {
CloseableReference.closeSafely(bitmapRef);
}
}
};
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
.setPostprocessor(postProcessor)
.build();
PipelineDraweeController newController = (PipelineDraweeController)
Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.setOldController(mSimpleDraweeView.getController())
// other setters as you need
.build();
mSimpleDraweeView.setController(newController);
网友评论