概述
//图片缓存
SimpleDraweeView photoIV;String imgUrl;
View itemView;
//初始化配置设置默认图等
photoIV.setHierarchy(createVideoCoverBuilder(itemView.getResources()));
public GenericDraweeHierarchy createVideoCoverBuilder(Resources resources) { GenericDraweeHierarchyBuilder builder = GenericDraweeHierarchyBuilder.newInstance(resources); builder.setPlaceholderImage(resources.getDrawable(drawable.shape_default_image)); builder.setFailureImage(resources.getDrawable(drawable.shape_default_image)); return builder.build(); }
Uri uri = Uri.parse(imgUrl); ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri) .setResizeOptions(new ResizeOptions(50, 50)) .build(); DraweeController controller = Fresco.newDraweeControllerBuilder() .setOldController(holder.photoIV.getController()) .setImageRequest(request) .build();photoIV.setController(controller);
//以下两个方法与其他网上不同,避免内存溢出//判断是否缓存了图片,并进行加载(不做处理)public void loadImage(Context context, String uri, ImageSize size, final ImageLoadingListener listener) { if (listener == null) { return; } if (TextUtils.isEmpty(uri)) { listener.onFailure(); return; } ImageRequestBuilder builder = ImageRequestBuilder .newBuilderWithSource(Uri.parse(uri)) .setProgressiveRenderingEnabled(false); if (size != null) { builder.setResizeOptions(new ResizeOptions(size.getWidth(), size.getHeight())); } ImageRequest imageRequest = builder.build(); ImagePipeline imagePipeline = Fresco.getImagePipeline(); final DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, context); final DataSubscriber<CloseableReference<CloseableImage>> dataSubscriber = new BaseDataSubscriber<CloseableReference<CloseableImage>>() { @Override public void onNewResultImpl( DataSource<CloseableReference<CloseableImage>> dataSource) { if (!dataSource.isFinished()) { return; } CloseableReference<CloseableImage> closeableReference = dataSource .getResult(); if (closeableReference == null) { return; } try { Bitmap bitmap = null; if (closeableReference.get() instanceof CloseableBitmap) { CloseableBitmap closeableBitmap = (CloseableBitmap) closeableReference.get(); bitmap = closeableBitmap.getUnderlyingBitmap(); } //Bitmap bitmap = closeableBitmap.getUnderlyingBitmap(); if (bitmap != null && !bitmap.isRecycled()) { //Bitmap copybitmap = SoftReference<Bitmap> copyBitmap = new SoftReference<Bitmap>(Bitmap.createBitmap(bitmap)); // Bitmap copybitmap = ImageBlur.createRGBABitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(),1); listener.onSuccess(copyBitmap.get()); } } finally { closeableReference.close(); dataSource.close(); } } @Override public void onFailureImpl(DataSource dataSource) { listener.onFailure(); dataSource.close(); } }; dataSource.subscribe(dataSubscriber, CallerThreadExecutor.getInstance()); }//(做处理压缩重新设置宽高)模糊化需要@Override public void getImageThumbnail(Context context, String uri, ImageSize size, final ImageLoadingListener listener) { if (listener == null) { return; } if (TextUtils.isEmpty(uri)) { listener.onFailure(); return; } ImageRequestBuilder builder = ImageRequestBuilder .newBuilderWithSource(Uri.parse(uri)) .setProgressiveRenderingEnabled(false); if (size != null) { builder.setResizeOptions(new ResizeOptions(size.getWidth(), size.getHeight())); } ImageRequest imageRequest = builder.build(); ImagePipeline imagePipeline = Fresco.getImagePipeline(); final DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, context); dataSource.subscribe(new BaseDataSubscriber<CloseableReference<CloseableImage>>() { @Override public void onNewResultImpl( DataSource<CloseableReference<CloseableImage>> dataSource) { if (!dataSource.isFinished()) { return; } CloseableReference<CloseableImage> closeableReference = dataSource .getResult(); if (closeableReference != null) { try { Bitmap bitmap = null; if (closeableReference.get() instanceof CloseableBitmap) { CloseableBitmap closeableBitmap = (CloseableBitmap) closeableReference.get(); bitmap = closeableBitmap.getUnderlyingBitmap(); } //Bitmap bitmap = closeableBitmap.getUnderlyingBitmap(); if (bitmap != null && !bitmap.isRecycled()) { // Bitmap thumbbitmap = ThumbnailUtils.extractThumbnail(bitmap, 100, 100); SoftReference<Bitmap> copyBitmap = new SoftReference<Bitmap>(ImageUtil.createThumbnail(bitmap, 100, 100)); listener.onSuccess(copyBitmap.get()); } } finally { closeableReference.close(); dataSource.close(); } } } @Override public void onFailureImpl(DataSource dataSource) { listener.onFailure(); dataSource.close(); } }, CallerThreadExecutor.getInstance()); }//回调接口public interface ImageLoadingListener { void onSuccess(Bitmap var1); void onFailure(); }//加载不做处理的loadImage(context,imageView,null,new ImageLoadingListener() { @Override public void onSuccess(final Bitmap bitmap) { // ImageUtil.blurBitMap(bitmap, 10); // ad_cover.setImageBitmap(bitmap); handler.post(new Runnable() { @Override public void run() { ad_cover.setImageBitmap(bitmap); } }); } @Override public void onFailure() { } });//加载压缩过的(模糊化处理)getImageThumbnail(context,imageView,null,new ImageLoadingListener() {@Overridepublic void onSuccess(final Bitmap bitmap) {blurBitMap(bitmap, 10);//模糊化处理(压缩后处理效率高)
// ad_cover.setImageBitmap(bitmap);handler.post(new Runnable() {@Overridepublic void run() {
ad_cover.setImageBitmap(bitmap);}
});
}
@Overridepublic void onFailure() {
} });
最后
以上就是复杂奇异果为你收集整理的Fresco图片加载框架一些简单使用的全部内容,希望文章能够帮你解决Fresco图片加载框架一些简单使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复