使用 Fresco加载图片
概念:
ImagePipeline ——负责从网络、本地图片、Content Provider(内容提供者)或者本地资源那里获取图片,压缩保存在本地存储中和在内存中保存为压缩的图片
Drawee——处理图片的渲染,由3部分组成:
(1)DraweeView——显示图片的View,继承ImageView;大部分时间将使用SimpleDraweeView
(2)DraweeHierarchy——Fresco提供了很多定制功能,如:添加placeholderImage,retryImage,failureImage,backgroundImage;当它们需要显示追踪所有的这些图片并在它们
(3)DraweeController——负责处理图片加载器;如果不想使用ImagePipeline,Fresco允许自定义Image loader
开始使用:
1. 添加 Fresco到 app/build.gradle 中:
dependencies {
compile 'com.facebook.fresco:fresco:0.6.1'
}
2.如果需要从网络中获取图片,在 AndroidManifest.xml中增加Internet权限:
<uses-permission android:name="android.permission.INTERNET"/>
3.初始化 Fresco. 在使用Fresco的activity中,Fresco 必须在 setContentView()之前初始化
Fresco.initialize(context);
调用Fresco.initialize一次即可完成初始化,多次调用初始化无意义,所以最好在 Application 里面做这件事。
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
在 AndroidManifest.xml 中指定你的 Application 类
<manifest
...
>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:label="@string/app_name"
android:name=".MyApplication"
>
...
</application>
...
</manifest>
4.在xml布局文件中, 加入命名空间:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/my_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
fresco:placeholderImage="@drawable/img_frame_background" />
</LinearLayout>
Note: If you want to use any Fresco defined properties, you'll need to add a custom namespace definition:
xmlns:fresco="http://schemas.android.com/apk/res-auto"
5.设置图片URI:
Uri imageUri = Uri.parse("https://i.imgur.com/tGbaZCY.jpg");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.sdvImage);
draweeView.setImageURI(imageUri);
/****************************************************************/
在XML中使用Drawees
Drawees 具有极大的可定制性。
下面的例子给出了可以配置的各种选项:
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/my_image_view"
android:layout_width="20dp"
android:layout_height="20dp"
fresco:fadeDuration="300"
fresco:actualImageScaleType="focusCrop"
fresco:placeholderImage="@color/wait_color"
fresco:placeholderImageScaleType="fitCenter"
fresco:failureImage="@drawable/error"
fresco:failureImageScaleType="centerInside"
fresco:retryImage="@drawable/retrying"
fresco:retryImageScaleType="centerCrop"
fresco:progressBarImage="@drawable/progress_bar"
fresco:progressBarImageScaleType="centerInside"
fresco:progressBarAutoRotateInterval="1000"
fresco:backgroundImage="@color/blue"
fresco:overlayImage="@drawable/watermark"
fresco:pressedStateOverlayImage="@color/red"
fresco:roundAsCircle="false"
fresco:roundedCornerRadius="1dp"
fresco:roundTopLeft="true"
fresco:roundTopRight="false"
fresco:roundBottomLeft="false"
fresco:roundBottomRight="true"
fresco:roundWithOverlayColor="@color/corner_color"
fresco:roundingBorderWidth="2dp"
fresco:roundingBorderColor="@color/border_color"
/>
参照:
[1]Fresco文档:http://fresco-cn.org/docs/using-drawees-xml.html
[2]Displaying Images with the Fresco Library
使用 Fresco加载图片的更多相关文章
- Fresco加载显示gif图片
第一步:首先要导包 Android Studio 或者 Gradle dependencies { compile 'com.facebook.fresco:fresco:0.6.0+' } 第二步: ...
- Android项目实战(三十):Fresco加载gif图片并播放
前言: 项目中图文混合使用的太多太多了,但是绝大部分都是静态图片. 然而项目开发中有这么一个需求:显示一个出一个简短的动画(一般都不超过3秒)演示 比如说:一个功能提供很多步骤来教用户做广播体操,那么 ...
- Glide加载图片问题记录
Glide加载图片相比于Picasso而言性能较好,又比Fresco轻巧,而且又支持加载gif动图,是Google 推荐.专注平滑的滚动.简单易用.可扩展的一款图片加载框架.但是使用时还是会遇到一些问 ...
- CSS3学习总结——实现瀑布流布局与无限加载图片相册
首先给大家看一下瀑布流布局与无限加载图片相册效果图: 一.pic1.html页面代码如下: <!DOCTYPE html> <html> <head> <me ...
- JQuery实现无刷新下拉加载图片
最近做的一个项目需要做页面无刷新下拉加载图片,调研了一番,大多都采用检测滚动条达到底部,然后利用ajax加载下一页数据对页面数据进行添加,根据这一逻辑,自己写了一个,具体代码如下: JQu ...
- android 网络加载图片,对图片资源进行优化,并且实现内存双缓存 + 磁盘缓存
经常会用到 网络文件 比如查看大图片数据 资源优化的问题,当然用开源的项目 Android-Universal-Image-Loader 或者 ignition 都是个很好的选择. 在这里把原来 ...
- ImageLoader加载图片
先导universal-image-loader-1.9.3包 在application配置 android:name=".MyApplication" intent权限 1 pa ...
- chrome (failed) net::ERR_INCOMPLETE_CHUNKED_ENCODING ashx 加载图片
chrome (failed) net::ERR_INCOMPLETE_CHUNKED_ENCODING ashx文件加载图片的方法,发现在chrome浏览器里面出了异常: (failed) ne ...
- 在QtCreator 2.1.0 下使用opencv231库加载图片并显示
在.pro中库连接如上图,具体规则正在学习,注意debug下连接*d.lib.release下链接.lib.没有d的. 如果出现imread不可以加载图片,cvloadImage却可以,则是上面说的连 ...
随机推荐
- MySQL(四) —— 操作数据表中的记录
插入记录 INSERT [INTO] tbl_name [(col_name,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),... //法二: ...
- Walkthrough: Arranging Controls on Windows Forms Using Snaplines
https://msdn.microsoft.com/en-us/library/t5b5kc41(v=vs.110).aspx Spacing and Aligning Controls Using ...
- 各种操作中心Operation Center一览
Operation Center在中国可能有很多种名称,例如指挥中心.运维室.总控中心等等,国外可能也有很多名称,不管名称如何,任何一个上规模得数据总心或者运维单位一般都有一个这样得中心,来负责所管理 ...
- JS如何将CST格式的日期转换为制定格式String
<html> <body> <script type="text/javascript"> var d = new Date() dateFor ...
- Java开发中经典的小实例-(while(参数){})
import java.util.Scanner;public class Test_while { public static void main(String[] args) { ...
- zoj 1648 判断线段是否相交
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=648 Circuit Board Time Limit: 2 Second ...
- python进程、线程、协程(转载)
python 线程与进程简介 进程与线程的历史 我们都知道计算机是由硬件和软件组成的.硬件中的CPU是计算机的核心,它承担计算机的所有任务. 操作系统是运行在硬件之上的软件,是计算机的管理者,它负责资 ...
- HTML笔记(七)head相关元素<base> & <meta>
<head>元素是所有头部元素的容器. 可添加的标签有:<title>.<base>.<link>.<meta>.<script> ...
- HttpClient的CircularRedirectException异常原因及解决办法
HttpClient的CircularRedirectException异常原因及解决办法 这两天在使用我自己爬虫抓取网页的时候总是出现 org.apache.http.client.ClientPr ...
- servlet文件上传
1.获取文件上传路径 String path=request.getServletContext.getRePath("/WEB-INF/resources"); 2.获得工厂 3 ...