Android图片缓存框架 - Fresco的BasePostprocessor图片处理 (十)

目录

1.Fresco 简介

2. Fresco 文档

3. Fresco开发步骤

4. Fresco加载图片6种方式

5. Drawees xm属性设置

6 Fresco实现圆角或圆形图片

7. DraweeHierarchy设置

8. Fresco的DraweeController

9. ControllerListener 图片加载监听

10. Fresco的ImageRequest

11 Fresco的显示GIF

12 Fresco的BasePostprocessor图片处理

12.1 BasePostprocessor图片处理

12.2 图片添加红色网格

12.3 图片水平翻转

12.4 裁剪1/4图片

13.Fresco设置和清除缓存


12 Fresco的BasePostprocessor图片处理

12.1 BasePostprocessor图片处理

下载图片完成后,希望对图片进行处理后再显示,例如可以对图片添加红色网格、水平翻转、裁剪图片1/4;

自定义Postprocessor需要继承 BasePostprocessor,只能重写一个process()函数

12.2 图片添加红色网格

        ColorPostprocessor redMeshPostprocessor = new ColorPostprocessor();
        redMeshPostprocessor.setColor(Color.RED);
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(imgUrl1))
                .setPostprocessor(redMeshPostprocessor)
                .build();
        PipelineDraweeController controller = (PipelineDraweeController)
                Fresco.newDraweeControllerBuilder()
                        .setImageRequest(request)
                        .setOldController(draweeView.getController())
                        .build();
        draweeView.setController(controller);

自定义ColorPostprocessor:

  private class ColorPostprocessor extends BasePostprocessor {
        private int color = Color.TRANSPARENT;
        public void setColor(int mcolor) {
            color = mcolor;
        }
        @Override
        public String getName() {
            return "ColorPostprocessor";
        }
        @Override
        public void process(Bitmap bitmap) {
            Log.i("====1===", "====1===");
            showToast("process1");
            for (int x = 0; x < bitmap.getWidth(); x += 2) {
                for (int y = 0; y < bitmap.getHeight(); y += 2) {
                    //设置图片遮罩层
                    bitmap.setPixel(x, y, color);
//              bitmap.setPixel(x, y, Color.RED);
                }
            }
        }
    }

 

12.3 图片水平翻转

        RotatePostprocessor rotatePostprocessor = new RotatePostprocessor();
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(imgUrl1))
                .setPostprocessor(rotatePostprocessor)
                .build();
        PipelineDraweeController controller = (PipelineDraweeController)
                Fresco.newDraweeControllerBuilder()
                        .setImageRequest(request)
                        .setOldController(draweeView.getController())
                        .build();
        draweeView.setController(controller);

自定义翻转类RotatePostprocessor :

private class RotatePostprocessor extends BasePostprocessor {
        @Override
        public String getName() {
            return "RotatePostprocessor";
        }

        @Override
        public void process(Bitmap destBitmap, Bitmap sourceBitmap) {
            super.process(destBitmap, sourceBitmap);
            try {
                for (int x = 1; x < destBitmap.getWidth(); x++) {
                    for (int y = 1; y < destBitmap.getHeight(); y++) {
                        destBitmap.setPixel(destBitmap.getWidth() - x, y, sourceBitmap.getPixel(x, y));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

 

12.4 裁剪1/4图片

        ScalePostprocessor scalePostprocessor = new ScalePostprocessor();
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(imgUrl1))
                .setPostprocessor(scalePostprocessor)
                .build();
        PipelineDraweeController controller = (PipelineDraweeController)
                Fresco.newDraweeControllerBuilder()
                        .setImageRequest(request)
                        .setOldController(draweeView.getController())
                        .build();
        draweeView.setController(controller);

自定义ScalePostprocessor :

private class ScalePostprocessor extends BasePostprocessor {
        @Override
        public String getName() {
            return "ScanlePostprocessor";
        }

        @Override
        public CloseableReference process(Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory) {
            //宽和高各缩小一半,显示图片的1/4
            CloseableReference bitmapRef = bitmapFactory.createBitmap(
                    sourceBitmap.getWidth()/2,
                    sourceBitmap.getHeight()/2);
            try {
                Bitmap destBitmap = bitmapRef.get();
                for (int x = 1; x < destBitmap.getWidth(); x += 2) {
                    for (int y = 1; y < destBitmap.getHeight(); y += 2) {
                        destBitmap.setPixel(destBitmap.getWidth() - x, y, sourceBitmap.getPixel(x, y));
                    }
                }
                return CloseableReference.cloneOrNull(bitmapRef);
            } finally {
                CloseableReference.closeSafely(bitmapRef);
            }
        }
    }

 

 

你可能感兴趣的:(Android图片缓存框架,Fresco,Android开发,Android图片缓存框架,Fresco,Fresco)