這裏沒有廣告,這裏沒有跑分,數據對比Google一下比比皆是,額···那我說什麼好(ノಠ益ಠ)ノ彡┻━┻。
對啊,女友···呸呸呸,那麼就來說一講用哪些庫好(你應該已經在知道了)。劇透一下(主要推薦Fresco),劇透一下(主要推薦Fresco),劇透一下(主要推薦Fresco)~~~請看到最後好不。javascript
補充一句:記得把初始化放到Applicant裏面,否則每次都會被清緩存,加載的時候用單例。複製代碼
聰明的你必定知道了,由於我要說Fresco,那麼爲何不用Glide了呢?挺好的啊~這·····由於某次我要把經過圖片URL拿本地緩存的時候,我竟然找不到同步獲取的方法(ノಠ益ಠ)ノ彡┻━┻,除了異步的回調以外,我竟然找不到····並且據說Fresco對GIF和webp支持更好,不,我無論,我要換女友。java
這是一個讓人又愛又恨的圖片加載,他自帶緩存,支持GIF,WebP,縮略圖···對圖片的緩存管理對5.0如下支持更是妥妥的,功能豐富,自帶神器,帕拉啪啦啪啦··· 惋惜他只支持本身的控件, 惋惜他只支持本身的控件, 惋惜他只支持本身的控件:DraweeView,繼承了ImageView,可是和ImageView沒有什麼關係,話說到底繼承幹啥?android
進入正題吧,感受我好廢話。我切換到Fresco也是下了很大的決心,由於替換ImageView是一件很痛苦的事情,特別是Fresco不支持warp_content,不過強大的功能確實很吸引,纔不是由於不能解決的bug和需求呢。ios
一、 首先推薦 FrescoImageView ,這個庫繼承了SimpleDraweeView,封裝了須要的一些接口,讓人使用起來更方便,小喵在這基礎在作一些調整和封裝,具體看下面:git
利用FrescoImageView封裝好的接口,使用起來是否是很方便啊<( ̄︶ ̄)↗github
public static void loadFrescoImage(FrescoImageView frescoImageView, String uri, int defaultImg, boolean pathIsLocal) {
loadFrescoImage(frescoImageView, uri, defaultImg, 0, false, pathIsLocal, true, null, null);
}
public static void loadFrescoImage(FrescoImageView frescoImageView, String uri, int defaultImg, boolean pathIsLocal, Point showSize) {
loadFrescoImage(frescoImageView, uri, defaultImg, 0, false, pathIsLocal, true, showSize, null);
}
public static void loadFrescoImage(FrescoImageView frescoImageView, String uri, int defaultImg, int radius, boolean pathIsLocal, Point showSize) {
loadFrescoImage(frescoImageView, uri, defaultImg, radius, false, pathIsLocal, true, showSize, null);
}
public static void loadFrescoImage(FrescoImageView frescoImageView, String uri, int defaultImg, boolean pathIsLocal, Postprocessor postprocessor) {
loadFrescoImage(frescoImageView, uri, defaultImg, 0, false, pathIsLocal, true, null, postprocessor);
}
public static void loadFrescoImage(FrescoImageView frescoImageView, String uri, int defaultImg, boolean pathIsLocal, Point point, Postprocessor postprocessor) {
loadFrescoImage(frescoImageView, uri, defaultImg, 0, false, pathIsLocal, true, point, postprocessor);
}
public static void loadFrescoImage(FrescoImageView frescoImageView, String uri, int defaultImg, int radius, boolean pathIsLocal, Point point, Postprocessor postprocessor) {
loadFrescoImage(frescoImageView, uri, defaultImg, radius, false, pathIsLocal, true, point, postprocessor);
}
public static void loadFrescoImage(FrescoImageView frescoImageView, String uri, int defaultImg, int radius, boolean pathIsLocal) {
loadFrescoImage(frescoImageView, uri, defaultImg, radius, false, pathIsLocal, true, null, null);
}
public static void loadFrescoImageCircle(FrescoImageView frescoImageView, String uri, int defaultImg, boolean pathIsLocal) {
loadFrescoImage(frescoImageView, uri, defaultImg, 0, true, pathIsLocal, true, null, null);
}
/** * @param frescoImageView * @param uri URL或者本地uri * @param defaultImg 默認資源 * @param radius 弧形角度 * @param showCircle 是否顯示圓 * @param pathIsLocal 是否加載的是本地資源 * @param gifAnima 是否執行GIF動畫 * @param showSize 是否修改顯示大小 * @param postprocessor 對圖像進行二次處理 */
public static void loadFrescoImage(FrescoImageView frescoImageView, String uri, int defaultImg,
int radius, boolean showCircle, boolean pathIsLocal, boolean gifAnima,
Point showSize, Postprocessor postprocessor) {
init(frescoImageView, radius, showCircle, gifAnima, showSize, postprocessor);
if (pathIsLocal) {
frescoImageView.loadLocalImage(uri, defaultImg);
} else {
frescoImageView.loadView(uri, defaultImg);
}
}
private static void init(FrescoImageView frescoImageView, int radius, boolean showCircle, boolean gifAnima,
Point showSize, Postprocessor postprocessor) {
frescoImageView.setAnim(gifAnima);
frescoImageView.setCornerRadius(radius);
frescoImageView.setFadeTime(300);
if (showCircle)
frescoImageView.asCircle();
if (postprocessor != null)
frescoImageView.setPostProcessor(postprocessor);
if (showSize != null) {
frescoImageView.setResize(showSize);
}
}複製代碼
三、PhotoDraweeView 功能與丁丁大名的PhotoView同樣,支持雙擊放大,單擊返回,手動放大與縮小等,無縫接入Fresco,哎喲喂,不錯喲。web
四、subsampling-scale-image-view 是否是很羨慕微博和微信加載高清長圖卻不OOM,每次都被產品還有測試說的火冒三丈,這個庫讓你高潮,它採用的是BitmapRegionDecoder的方式,分段加載顯示超長圖,拒絕OOM,並且,並且,並且支持支持支持:雙擊放大,單擊返回,手動放大等,目前只能加載本地,不怕不怕,能夠下下來用緩存啊:api
個人邏輯是,當圖片的長度大於屏幕的2倍是就使用這個加載(寬度? i dont care)緩存
final SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)
final String tmpUri = list.get(position).getUri();
final Uri uri = Uri.parse((tmpUri .startsWith("http")) ? tmpUri : (tmpUri .startsWith("file://")) ? tmpUri : "file://" + tmpUri );
if (tmpUri .startsWith("http")) {
File file = FrescoUtil.getLocalCache(uri);//Fresco的本地緩存
if (file != null && file.exists()) {
imageView.setImage(ImageSource.uri(file.getAbsolutePath()));
} else {
FrescoUtil.loadImage(tmpUri , context, 0, 0, new LoadingListener() {
@Override
public void onSuccess(Bitmap bitmap) {
File file = FrescoUtil.getLocalCache(uri);
if (file != null && file.exists()) {
imageView.setImage(ImageSource.uri(file.getAbsolutePath()));
}
}
@Override
public void onFail() {
}
});
}
} else {
imageView.setImage(ImageSource.uri(tmpUri .replace("file://", "")));
}複製代碼
ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this)
.setDownsampleEnabled(true)
.build();
Fresco.initialize(this, config);複製代碼
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
switch (newState) {
case RecyclerView.SCROLL_STATE_IDLE://中止滑動
if (Fresco.getImagePipeline().isPaused())
Fresco.getImagePipeline().resume();
break;
case RecyclerView.SCROLL_STATE_DRAGGING:
if (preScrollState == RecyclerView.SCROLL_STATE_SETTLING) {
//觸摸滑動不須要加載
Fresco.getImagePipeline().pause();
} else {
//觸摸滑動須要加載
if (Fresco.getImagePipeline().isPaused())
Fresco.getImagePipeline().resume();
}
break;
case RecyclerView.SCROLL_STATE_SETTLING://慣性滑動
Fresco.getImagePipeline().pause();
break;
}
preScrollState = newState;
}複製代碼
getWindow().setSharedElementEnterTransition(DraweeTransition.createTransitionSet(
ScalingUtils.ScaleType.CENTER_CROP, ScalingUtils.ScaleType.CENTER_CROP));
getWindow().setSharedElementReturnTransition(DraweeTransition.createTransitionSet(
ScalingUtils.ScaleType.CENTER_CROP, ScalingUtils.ScaleType.CENTER_CROP));複製代碼
不要向Fresco設置大量大尺寸的R.drawable.XX資源,由於這種相似的內存釋放不及時,多跳轉幾個頁面很不幸你就會有OOM的可能,這中問題主要是在於設置背景,通常的loading和fail的圖片都不會很大(要那麼大幹嗎,UI你是要打架嗎( ‵o′)凸),咱們選擇是把圖片保存到SD卡以後,加載本地圖片資源,這樣能夠友好的釋放背景大圖了。微信
獲取本地圖片和下載圖片的方法
/** * @param uri * @param context * @param width * @param height * @param listener */
public static void getImageLoading(String uri, Context context, int width, int height, final LoadingListener listener) {
getBitmapWithProcessor(uri, context, width, height, null, listener);
}
/** * @param uri * @param context * @param width * @param height * @param processor 處理圖片的 * @param listener */
public static void getImageLoading(final String uri, Context context, final int width, final int height,
BasePostprocessor processor, final LoadingListener listener) {
ResizeOptions resizeOptions = null;
if (width != 0 && height != 0) {
resizeOptions = new ResizeOptions(width, height);
}
ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(uri))
.setProgressiveRenderingEnabled(false) //不渲染
.setPostprocessor(processor)
.setResizeOptions(resizeOptions)
.build();
ImagePipeline imagePipeline = Fresco.getImagePipeline();
DataSource
> dataSource = imagePipeline.fetchDecodedImage(imageRequest, context); dataSource.subscribe(
new BaseBitmapDataSubscriber() { @Override protected
void onNewResultImpl(Bitmap bitmap) { } @Override protected
void onFailureImpl(DataSource
> dataSource) { listener.onFail(); } }, CallerThreadExecutor.getInstance()); } public
static boolean isLocalCached(Uri uri) { ImagePipeline imagePipeline = Fresco.getImagePipeline(); DataSource<
Boolean> dataSource = imagePipeline.isInDiskCache(uri);
if (dataSource ==
null) {
return
false; } ImageRequest imageRequest = ImageRequest.fromUri(uri); CacheKey cacheKey = DefaultCacheKeyFactory.getInstance() .getEncodedCacheKey(imageRequest, YHApplication.get()); BinaryResource resource = ImagePipelineFactory.getInstance() .getMainFileCache().getResource(cacheKey);
return resource !=
null && dataSource.getResult() !=
null && dataSource.getResult(); } public
static File getLocalCache(Uri uri) {
if (!isLocalCached(uri))
return
null; ImageRequest imageRequest = ImageRequest.fromUri(uri); CacheKey cacheKey = DefaultCacheKeyFactory.getInstance() .getEncodedCacheKey(imageRequest, YHApplication.get()); BinaryResource resource = ImagePipelineFactory.getInstance() .getMainFileCache().getResource(cacheKey); File file = ((FileBinaryResource) resource).getFile();
return file; }
複製代碼
我的Github : github.com/CarGuo