使用 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却可以,则是上面说的连 ...
随机推荐
- ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'mysql'
mysql> use mysqlERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'mysql' ...
- 权威发布:长链非编码RNA命名规则
转自:http://blog.sina.com.cn/s/blog_8088f3700101pab7.html 权威发布:长链非编码RNA命名规则 对于人类基因命名标准的制定而言,雨果基因命名委员会( ...
- MYSQL写入数据时报错ERROR 1366 (HY000): Incorrect string value: '\xE8\x8B\xB1\xE5\xAF\xB8...' for c 插入中文不能插入
先把原先你创建的这个表删除,然后 CREATE TABLE IF NOT EXISTS tdb_goods( goods_id SMALLINT UNSIGNED PRIMARY KEY AUTO_I ...
- Struts BaseAction工具类,封装Session,Request,Application,ModelDriven
package com.ssh.shop.action; import java.io.InputStream; import java.lang.reflect.ParameterizedType; ...
- hdu 5023 A Corrupt Mayor's Performance Art 线段树
A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100 ...
- GitHub上不错的Android开源项目(二)
收集相关系列资料,自己用作参考,练习和实践.小伙伴们,总有一天,你也能写出 Niubility 的 Android App :-) 系列文章如下: GitHub上不错的Android开源项目(一):h ...
- 如何设置table中<tr>和<td>的高度
//-----------------自定义表格table的行和列的宽和高----------------------// 先设置一个样式 如下: <style type="text/ ...
- Javascript中typeof instanceof constructor的区别
typeof typeof,是一个运算符,运算中需要一个操作数,运算的结果就是这个操作数的类型,运算的结果是一个字符串.他有一定的局限性,对于对象类型的值,只能得到一个object结果,却不能精确得到 ...
- 原生js实现的效果
原生js实现tooltip提示框的效果 在js的世界里面,每一个小的特效都那么微不足道,却又那么的令人向往与好奇.前端工程师的任务特别高大上,因为他们的一个小小的设计就会激发别人的求知欲.比如说我 ...
- openwrt: Makefile 框架分析
openwrt: Makefile 框架分析 原文链接:blog.chinaunix.net/uid-26675482-id-4704952.html 本篇的主要目的是想通过分析Makefile,了解 ...