您的位置:首页 > 移动开发 > Android开发

Android-->Facebook图片加载库Fresco(爱它的理由)

2016-10-02 05:53 218 查看
在Android中,图片加载框架很多,很多,很多…都是经典,爆款.

也许你在用:

https://github.com/square/picasso

https://github.com/bumptech/glide

https://github.com/nostra13/Android-Universal-Image-Loader/

https://github.com/koush/UrlImageViewHelper

等等,

但是都阻止不了,我爱https://github.com/facebook/fresco/它的理由;

Fresco 的 Image Pipeline 负责图片的获取和管理。图片可以来自远程服务器,本地文件,或者Content Provider,本地资源。压缩后的文件缓存在本地存储中,Bitmap数据缓存在内存中。

在5.0系统以下,Image Pipeline 使用 pinned purgeables 将Bitmap数据避开Java堆内存,存在ashmem中。这要求图片不使用时,要显式地释放内存。

SimpleDraweeView自动处理了这个释放过程,所以没有特殊情况,尽量使用SimpleDraweeView,在特殊的场合,如果有需要,也可以直接控制Image Pipeline。

了解它,从官网开始: http://fresco-cn.org/docs/getting-started.html

使用方法:

//1:添加依赖
compile 'com.facebook.fresco:fresco:0.14.0'
//2.调用初始化方法,推荐在Application方法.切记此方法必须调用.
Fresco.initialize(this);


显示本地/网络/资源图片:

//显示方法只有这一个函数.不同的就是参数url...
simpleDraweeView.setImageURI(Uri.parse(url));

//网络图片: 直接使用网络url地址
String url = "http://mvimg1.meitudata.com/559e50975a5039607.jpg";//即可

//Res资源图片
String url = "res://" + getPackageName() + "/" + R.mipmap.ic_launcher;//注意要添加包名

//文章后面有所有支持的uri类型.


圆形/圆角图片:

<com.facebook.drawee.view.SimpleDraweeView
app:roundAsCircle="true" //圆形图片
app:roundedCornerRadius="10dp" //圆角
android:layout_width="40dp"
android:layout_height="40dp" />


//圆形图片
RoundingParams roundingParams = RoundingParams.asCircle();
draweeView.getHierarchy().setRoundingParams(roundingParams);


RoundingParams roundingParams = RoundingParams.fromCornersRadius(10);//圆角图片
roundingParams.setRoundAsCircle(true);//圆形图片
draweeView.getHierarchy().setRoundingParams(roundingParams);


占位图片,失败图片,加载进度

<com.facebook.drawee.view.SimpleDraweeView
app:failureImage="@mipmap/fail_image"   //失败图片
app:placeholderImage="@mipmap/placeholder_image" //占位图片
app:progressBarImage="@mipmap/progress_bar" //进度图片
app:retryImage="@mipmap/retry_image"/>  //重试图片


//对应代码设置:
final GenericDraweeHierarchy hierarchy = simpleDraweeView.getHierarchy();
hierarchy.setProgressBarImage(R.mipmap.progress_bar);
hierarchy.setFailureImage(R.mipmap.fail_image);
hierarchy.setPlaceholderImage(R.mipmap.placeholder_image);
hierarchy.setRetryImage(R.mipmap.retry_image);


请注意:

不要使用wrap_content, 尽量使用match_parent或者具体的尺寸.

原因你可以看这里:http://fresco-cn.org/docs/wrap-content.html

支持的URI

类型SCHEME示例
远程图片http://, https:// HttpURLConnection 或者参考 使用其他网络加载方案
本地文件file://FileInputStream
Content providercontent://ContentResolver
asset目录下的资源asset://AssetManager
res目录下的资源res://Resources.openRawResource
Uri中指定图片数据data:mime/type;base64,数据类型必须符合 rfc2397规定 (仅支持 UTF-8)
所有属性参考:

https://github.com/facebook/fresco/blob/master/drawee/src/main/res/values/attrs.xml

<resources>
<declare-styleable name="GenericDraweeHierarchy">

<!-- Fade duration in milliseconds. -->
<attr name="fadeDuration" format="integer"/>

<!-- Aspect ratio (width / height) of the view, not necessarily of the images. -->
<attr name="viewAspectRatio" format="float"/>

<!-- Image branches -
Scale-type values must match those in GenericDraweeHierarchyInflater.getScaleTypeFromXml.
(GenericDraweeHierarchyInflater.java).
For drawables that should not be scaled, such as those with the android:tileMode
attribute set, use the value 'none'. -->

<!-- A drawable or color to be be used as a placeholder. -->
<attr name="placeholderImage" format="reference"/>
<!-- Scale type of the placeholder image. Ignored if placeholderImage is not specified. -->
<attr name="placeholderImageScaleType">
<enum name="none" value="-1" />
<enum name="fitXY" value="0" />
<enum name="fitStart" value="1" />
<enum name="fitCenter" value="2" />
<enum name="fitEnd" value="3" />
<enum name="center" value="4" />
<enum name="centerInside" value="5" />
<enum name="centerCrop" value="6" />
<enum name="focusCrop" value="7" />
</attr>

<!-- A drawable to be be used as a retry image. -->
<attr name="retryImage" format="reference"/>
<!-- Scale type of the retry image. Ignored if retryImage is not specified. -->
<attr name="retryImageScaleType">
<enum name="none" value="-1" />
<enum name="fitXY" value="0" />
<enum name="fitStart" value="1" />
<enum name="fitCenter" value="2" />
<enum name="fitEnd" value="3" />
<enum name="center" value="4" />
<enum name="centerInside" value="5" />
<enum name="centerCrop" value="6" />
<enum name="focusCrop" value="7" />
</attr>

<!-- A drawable to be be used as a failure image. -->
<attr name="failureImage" format="reference"/>
<!-- Scale type of the failure image. Ignored if failureImage is not specified. -->
<attr name="failureImageScaleType">
<enum name="none" value="-1" />
<enum name="fitXY" value="0" />
<enum name="fitStart" value="1" />
<enum name="fitCenter" value="2" />
<enum name="fitEnd" value="3" />
<enum name="center" value="4" />
<enum name="centerInside" value="5" />
<enum name="centerCrop" value="6" />
<enum name="focusCrop" value="7" />
</attr>

<!-- A drawable to be be used as a progress bar. -->
<attr name="progressBarImage" format="reference"/>
<!-- Scale type of the progress bar. Ignored if progressBarImage is not specified. -->
<attr name="progressBarImageScaleType">
<enum name="none" value="-1" />
<enum name="fitXY" value="0" />
<enum name="fitStart" value="1" />
<enum name="fitCenter" value="2" />
<enum name="fitEnd" value="3" />
<enum name="center" value="4" />
<enum name="centerInside" value="5" />
<enum name="centerCrop" value="6" />
<enum name="focusCrop" value="7" />
</attr>
<!-- Progress bar Auto Rotate interval in milliseconds -->
<attr name="progressBarAutoRotateInterval" format="integer"/>

<!-- Scale type of the actual image. -->
<attr name="actualImageScaleType">
<enum name="none" value="-1" />
<enum name="fitXY" value="0" />
<enum name="fitStart" value="1" />
<enum name="fitCenter" value="2" />
<enum name="fitEnd" value="3" />
<enum name="center" value="4" />
<enum name="centerInside" value="5" />
<enum name="centerCrop" value="6" />
<enum name="focusCrop" value="7" />
</attr>

<!-- A drawable or color to be used as a background. -->
<attr name="backgroundImage" format="reference"/>

<!-- A drawable or color to be used as an overlay. -->
<attr name="overlayImage" format="reference"/>

<!-- A drawable or color to be used as a pressed-state-overlay -->
<attr name="pressedStateOverlayImage" format="reference"/>

<!-- Rounding params -
Declares attributes for rounding shape, mode and border. -->

<!-- Round as circle. -->
<attr name="roundAsCircle" format="boolean"/>
<!-- Rounded corner radius. Ignored if roundAsCircle is used. -->
<attr name="roundedCornerRadius" format="dimension"/>
<!-- Round the top-left corner. Ignored if roundAsCircle is used. -->
<attr name="roundTopLeft" format="boolean"/>
<!-- Round the top-right corner. Ignored if roundAsCircle is used. -->
<attr name="roundTopRight" format="boolean"/>
<!-- Round the bottom-right corner. Ignored if roundAsCircle is used. -->
<attr name="roundBottomRight" format="boolean"/>
<!-- Round the bottom-left corner. Ignored if roundAsCircle is used. -->
<attr name="roundBottomLeft" format="boolean"/>
<!-- Round by overlying color. -->
<attr name="roundWithOverlayColor" format="color"/>
<!-- Rounding border width-->
<attr name="roundingBorderWidth" format="dimension"/>
<!-- Rounding border color -->
<attr name="roundingBorderColor" format="color"/>
<!-- Rounding border padding -->
<attr name="roundingBorderPadding" format="dimension" />

</declare-styleable>

<declare-styleable name="SimpleDraweeView">

<!-- An image uri . -->
<attr name="actualImageUri" format="string"/>

</declare-styleable>

</resources>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android facebook fresco