public static void load(Uri uri,SimpleDraweeView draweeView,BasePostprocessor processor,int width,int height, BaseControllerListener listener){ ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri) .setPostprocessor(processor) .setResizeOptions(new ResizeOptions(width,height)) //缩放,在解码前修改内存中的图片大小, 配合Downsampling可以处理所有图片,否则只能处理jpg, // 开启Downsampling:在初始化时设置.setDownsampleEnabled(true) .setProgressiveRenderingEnabled(true)//支持图片渐进式加载 .setAutoRotateEnabled(true) //如果图片是侧着,可以自动旋转 .build(); PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder() .setImageRequest(request) .setControllerListener(listener) .setOldController(draweeView.getController()) .setAutoPlayAnimations(true) //自动播放gif动画 .build(); draweeView.setController(controller); }
private DraweeController getImageController(String imagePath, DraweeController oldController) { String address = "http://image.tmdb.org/t/p/w" + "300" + imagePath; Log.d("FRESCOLOADING", address); Uri uri = Uri.parse(address); ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri) .setProgressiveRenderingEnabled(true) .setPostprocessor(new BasePostprocessor() { @Override public void process(Bitmap bitmap) { super.process(bitmap); Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() { @Override public void onGenerated(Palette palette) { int primaryDark = ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark); int primary = ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary); mBinding.collapsingToolbar.setContentScrimColor(palette.getMutedColor(primary)); mBinding.collapsingToolbar.setStatusBarScrimColor(palette.getDarkVibrantColor(primaryDark)); } }); } }) .build(); return Fresco.newDraweeControllerBuilder() .setImageRequest(request) .setOldController(oldController) .build(); }
@Override public void convert(final BaseViewHolder holder, final TngouModel.TngouBean tngouBean, final int position) { super.convert(holder, tngouBean, position); final SimpleDraweeView mImage = holder.getView(R.id.mImage); final String url = RequestServiceTngou.ImageHear + tngouBean.img; Uri uri = Uri.parse(url); Postprocessor redMeshPostprocessor = new BasePostprocessor() { @Override public String getName() { return url; } @Override public void process(Bitmap bitmap) { if (imageHeights.get(url + "height") == null) { imageHeights.put(url + "height", UtilsDynamicSize.defaultDisplayWidth / 3 * bitmap.getHeight() / bitmap.getWidth()); imageHeights.put(url + "width", UtilsDynamicSize.defaultDisplayWidth / 3); } mImage.getLayoutParams().height = imageHeights.get(url + "height"); mImage.getLayoutParams().width = imageHeights.get(url + "width"); } }; ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri) .setPostprocessor(redMeshPostprocessor) .build(); PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder() .setImageRequest(request) .setOldController(mImage.getController()) // other setters as you need .build(); mImage.setController(controller); }
@Override public void convert(final BaseViewHolder holder, final MzituModel bean, final int position) { super.convert(holder, bean, position); final SimpleDraweeView mImage = holder.getView(R.id.mImage); final String url = bean.imagePath; Uri uri = Uri.parse(url); Postprocessor redMeshPostprocessor = new BasePostprocessor() { @Override public String getName() { return url; } @Override public void process(Bitmap bitmap) { if (imageHeights.get(url + "height") == null) { imageHeights.put(url + "height", UtilsDynamicSize.defaultDisplayWidth / 3 * bitmap.getHeight() / bitmap.getWidth()); imageHeights.put(url + "width", UtilsDynamicSize.defaultDisplayWidth / 3); } mImage.getLayoutParams().height = imageHeights.get(url + "height"); mImage.getLayoutParams().width = imageHeights.get(url + "width"); } }; ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri) .setPostprocessor(redMeshPostprocessor) .build(); PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder() .setImageRequest(request) .setOldController(mImage.getController()) // other setters as you need .build(); mImage.setController(controller); }
public static void LoadImageFromURLAndCallBack(SimpleDraweeView destImageView , String URL, Context context, BaseBitmapDataSubscriber bbds , BasePostprocessor postprocessor) { int w = destImageView.getWidth(); int h =destImageView.getHeight(); if(w<1){ w = destImageView.getLayoutParams().width; } if(h<1){ h =destImageView.getLayoutParams().height; } ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(Uri.parse(URL)) .setResizeOptions(new ResizeOptions(w,h)) .setProgressiveRenderingEnabled(true); if(postprocessor!=null){ builder.setPostprocessor(postprocessor); } ImageRequest imageRequest = builder .build(); ImagePipeline imagePipeline = Fresco.getImagePipeline(); DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, context); dataSource.subscribe(bbds, CallerThreadExecutor.getInstance()); DraweeController draweeController = Fresco.newDraweeControllerBuilder() .setImageRequest(imageRequest) .setOldController(destImageView.getController()) .setAutoPlayAnimations(true) .build(); destImageView.setController(draweeController); }
@Override public void process(Bitmap dest, Bitmap src) { Canvas canvas = new Canvas(dest); Paint paint = new Paint(); canvas.drawBitmap(src, 0, 0, paint); for(BasePostprocessor processor : mProcessors) { processor.process(dest, dest); } }
public BenchmarkPostprocessorForDuplicatedBitmap( DurationCallback durationCallback, BasePostprocessor postprocessor) { super(durationCallback); mPostprocessor = postprocessor; }
public BenchmarkPostprocessorForManualBitmapHandling( DurationCallback durationCallback, BasePostprocessor postprocessor) { super(durationCallback); mPostprocessor = postprocessor; }
public BenchmarkPostprocessorForDuplicatedBitmapInPlace( DurationCallback durationCallback, BasePostprocessor postprocessor) { super(durationCallback); mPostprocessor = postprocessor; }
public static void getBitmapWithQiniu(@NonNull final String url, @NonNull final int width, @NonNull final int height, @Nullable BasePostprocessor processor, @NonNull final FrescoUtil.BitmapListener listener){ String newUrl = QiniuUtils.getSamllImage(url,width,height,QiniuUtils.isWWW,true); FrescoUtil.getBitmapWithProcessor(newUrl,context,width,height,processor,listener); }
public static void getBitmap(@NonNull final String url, @NonNull final int width, @NonNull final int height, @Nullable BasePostprocessor processor, @NonNull final FrescoUtil.BitmapListener listener){ FrescoUtil.getBitmapWithProcessor(url,context,width,height,processor,listener); }
@Override public void setImageURI(Uri uri, Object callerContext) { // http://frescolib.org/docs/modifying-image.html // this post process will do two things: 1. resize if image width is too large; 2. split if image height is too large Postprocessor postProcessor = new BasePostprocessor() { @Override public String getName() { return "SplitLongImagePostProcessor"; } @Override public CloseableReference<Bitmap> process(Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory) { CloseableReference<Bitmap> bitmapRef = null; try { // resize image if its width is too large: > windowWidth * 1.5 double ratio = 1.0; if (sourceBitmap.getWidth() >= WindowWidth * 1.5) { ratio = (double) WindowWidth / sourceBitmap.getWidth(); } bitmapRef = bitmapFactory.createBitmap((int) (sourceBitmap.getWidth() * ratio), (int) (sourceBitmap.getHeight() * ratio)); Bitmap destBitmap = bitmapRef.get(); Canvas canvas = new Canvas(destBitmap); Rect destRect = new Rect(0, 0, destBitmap.getWidth(), destBitmap.getHeight()); canvas.drawBitmap(sourceBitmap, null, destRect, paint); // split images if its height is too large: > OpenGL max Height try { int imageTotalHeight = destBitmap.getHeight(); double imageAspectRatio = destBitmap.getWidth() / (double) WindowWidth; int imageMaxAllowedHeight; if (imageAspectRatio < 1) { imageMaxAllowedHeight = (int) (ImageUtils.getMaxHeight() * imageAspectRatio) - 5; } else { imageMaxAllowedHeight = ImageUtils.getMaxHeight() - 5; } int imageCount = getTimes(imageTotalHeight, imageMaxAllowedHeight); // Log.d(TAG, "process: h = " + imageTotalHeight + " w = " + destBitmap.getWidth() + " allowed: " + imageMaxAllowedHeight + " count: " + imageCount); if (imageCount > 1) { bmps = new ArrayList<Bitmap>(); Rect bsrc = new Rect(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); destBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); InputStream isBm = new ByteArrayInputStream(baos.toByteArray()); BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(isBm, true); for (int i = 0; i < imageCount; i++) { bsrc.left = 0; bsrc.top = i * imageMaxAllowedHeight; bsrc.right = destBitmap.getWidth(); bsrc.bottom = Math.min(bsrc.top + imageMaxAllowedHeight, imageTotalHeight); Bitmap bmp = decoder.decodeRegion(bsrc, null); bmps.add(bmp); } } } catch (Exception e) { Log.e(TAG, Log.getStackTraceString(e)); } return CloseableReference.cloneOrNull(bitmapRef); } finally { CloseableReference.closeSafely(bitmapRef); } } }; ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri).setAutoRotateEnabled(true) // this will reduce image's size if it's wider than screen width // .setResizeOptions(new ResizeOptions(WindowWidth, Integer.MAX_VALUE)) .setPostprocessor(postProcessor).build(); DraweeController controller = ((PipelineDraweeControllerBuilder) getControllerBuilder()).setImageRequest(request) .setControllerListener(listener) .setCallerContext(callerContext) .setAutoPlayAnimations(true) .setOldController(getController()) .build(); setController(controller); }
private CombinePostProcessors(List<BasePostprocessor> processors) { super(); mProcessors = processors; }
public Builder() { processors = new ArrayList<BasePostprocessor>(); }
public Builder add(BasePostprocessor processor) { processors.add(processor); return this; }
/** * If the image has some ResizeOptions we put also the resized image into the cache with different key. * currently don't support downsampling / resizing for GIFs. * @param url * @param draweeView * @param processor * @param width * @param height * @param listener */ public static void loadUrl(String url, SimpleDraweeView draweeView, BasePostprocessor processor, int width, int height, BaseControllerListener listener){ url = append(url); load(Uri.parse(url),draweeView,processor,width,height,listener); }
public static void loadFile(String file, SimpleDraweeView draweeView,BasePostprocessor processor,int width,int height, BaseControllerListener listener){ load(getFileUri(file),draweeView,processor,width,height,listener); }
public static void loadFile(File file, SimpleDraweeView draweeView,BasePostprocessor processor,int width,int height, BaseControllerListener listener){ load(getFileUri(file),draweeView,processor,width,height,listener); }
public static void loadRes(int resId, SimpleDraweeView draweeView,BasePostprocessor processor,int width,int height, BaseControllerListener listener){ load(getResUri(resId),draweeView,processor,width,height,listener); }
public static void loadFile(String file, SimpleDraweeView draweeView, BasePostprocessor processor, int width, int height, BaseControllerListener listener){ load(getFileUri(file),draweeView,processor,width,height,listener); }
public static void loadFile(File file, SimpleDraweeView draweeView, BasePostprocessor processor, int width, int height, BaseControllerListener listener){ load(getFileUri(file),draweeView,processor,width,height,listener); }
public static void loadRes(int resId, SimpleDraweeView draweeView, BasePostprocessor processor, int width, int height, BaseControllerListener listener){ load(getResUri(resId),draweeView,processor,width,height,listener); }
/** * If the image has some ResizeOptions we put also the resized image into the cache with different key. * currently don't support downsampling / resizing for GIFs. * @param url * @param draweeView * @param processor * @param width * @param height * @param listener */ public static void loadUrl(String url, SimpleDraweeView draweeView,BasePostprocessor processor,int width,int height, BaseControllerListener listener){ load(Uri.parse(url),draweeView,processor,width,height,listener); }