Fresco是Facebook最新推出的一款用于Android应用中展示图片的强大图片库,可以从网络、本地存储和本地资源中加载图片。其中的Drawees可以显示占位符,直到图片加载完成。而当图片从屏幕上消失时,会自动释放内存。

功能很强大,为了大家学习方便,我将英文原文文档给大家迁移过来,供参考学习。


GitHub项目地址:https://github.com/facebook/fresco


Fresco

Fresco is a powerful system for displaying images in Android applications.

Fresco takes care of image loading and display, so you don't have to. It will load images from the network, local storage, or local resources, and display a placeholder until the image has arrived. It has two levels of cache; one in memory and another in internal storage.

In Android 4.x and lower, Fresco puts images in a special region of Android memory. This lets your application run faster - and suffer the dreadedOutOfMemoryErrormuch less often.

Fresco also supports:

  • streaming of progressive JPEGs
  • display of animated GIFs and WebPs
  • extensive customization of image loading and display
  • and much more!

Find out more at ourwebsite.

Requirements

Fresco can be included in any Android application.

Fresco supports Android 2.3 (Gingerbread) and later.

Using Fresco in your application

If you are building with Gradle, simply add the following line to thedependenciessection of yourbuild.gradlefile:

compile 'com.facebook.fresco:fresco:0.1.0+'

See ourdownloadpage for other options.

Get Started

Building Fresco from source

Install the AndroidSDKif you haven't already. Then run the Android SDK Manager and install the Android Support Library and Android Support Repository.

Download the AndroidNDK. Then add the directory containing it to your PATH environment variable.

Then just do

git clone https://github.com/facebook/fresco.gitcd fresco./gradlew build

Join the Fresco community

Please use ourissues pageto let us know of any problems.

For pull requests, please see theCONTRIBUTINGfile for information on how to help out.

License

Fresco isBSD-licensed. We also provide an additionalpatent grant.



这是英文文档的第一部分:QUICK START


QUICK START

Adding Fresco to your Project

Here's how to add Fresco to your Android project.

Android Studio or Gradle

Edit yourbuild.gradlefile. You must add the following line to thedependenciessection:

dependencies {  // your app's other dependencies  compile 'com.facebook.fresco:fresco:0.2.0+'}

Maven

Add the following to the<dependencies>section of yourpom.xmlfile:

<dependency>  <groupId>com.facebook.fresco</groupId>  <artifactId>fresco</artifactId>  <version>LATEST</version></dependency>

Eclipse ADT / Ant

Unfortunately Eclipse does not yet support the AAR file format Fresco uses. We are still looking for a workaround.

Getting started with Fresco

If you just want to download an image and display it, showing a placeholder until it comes, use aSimpleDraweeView.

For images from the network, you will need to to request Internet permission from your users. Add this line to yourAndroidManifest.xmlfile:

  <uses-permission android:name="android.permission.INTERNET"/>

Near your application startup, before your app callssetContentView(), initialize the Fresco class:

Fresco.initialize(context);

In your XML, add a custom namespace to the top-level element:

<!-- Any valid element will do here --><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:fresco="http://schemas.android.com/apk/res-auto"    android:layout_height="match_parent"    android:layout_width="match_parent">

Then add theSimpleDraweeViewto the layout:

<com.facebook.drawee.view.SimpleDraweeView    android:id="@+id/my_image_view"    android:layout_width="130dp"    android:layout_height="130dp"    fresco:placeholderImage="@drawable/my_drawable"  />

To show an image, you need only do this:

Uri uri = Uri.parse("http://frescolib.org/static/fresco-logo.png");SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);draweeView.setImageURI(uri);

and Fresco does the rest.

The placeholder is shown until the image is ready. The image will be downloaded, cached, displayed, and cleared from memory when your view goes off-screen.

Concepts

Drawees

Drawees are spaces in which images are rendered. These are made up of three components, like an Model-View-Controller framework.

DraweeView

Descended from the AndroidViewclass.

Most apps should use theSimpleDraweeViewclass. Place these in your application using XML or Java code. Set the URI to load with thesetImageURImethod, as explained in theGetting Startedpage.

You cancustomize its appearance in XML.

DraweeHierarchy

This is the hierarchy of AndroidDrawableobjects that will actually render your content. Think of it as the Model in an MVC.

If you need tocustomize your image's appearance in Java, this is the class you will deal with.

DraweeController

TheDraweeControlleris the class responsible for actually dealing with the underlying image loader - whether Fresco's own image pipeline, or another.

If you need something more than a single URI to specify the image you want to display, you will need an instance of this class.

DraweeControllerBuilder

DraweeControllersare immutable once constructed. They arebuiltusing the Builder pattern.

Listeners

One use of a builder is to specify aListenerto execute code upon the arrival, full or partial, of image data from the server.

The Image Pipeline

Behind the scenes, Fresco's image pipeline deals with the work done in getting an image. It fetches from the network, a local file, a content provider, or a local resource. It keeps a cache of compressed images on local storage, and a second cache of decompressed images in memory.

The image pipeline uses a special technique calledpinned purgeablesto keep images off the Java heap. This requires callers tocloseimages when they are done with them.

SimpleDraweeViewdoes this for you automatically, so should be your first choice. Very few apps need to use the image pipeline directly.

Supported URIs

Fresco supports images in a variety of locations.

Fresco doesnotaccept relative URIs. All URIs must be absolute and must include the scheme.

These are the URI schemes accepted:

Type Scheme Fetch method used
File on network http://,https:// HttpURLConnectionornetwork layer
File on device file:// FileInputStream
Content provider content:// ContentResolver
Asset in app asset:// AssetManager
Resource in app res:// Resources.openRawResource


Note: Only image resources can be used with the image pipeline (e.g. a PNG image). Other resource types such as Strings or XML Drawables make no sense in the context of the image pipeline and so cannot be supported by definition. One potentially confusing case is drawable declared in XML (e.g. ShapeDrawable). Important thing to note is that this isnotan image. If you want to display an XML drawable as the main image, then set it as aplaceholderand use thenulluri.


更多相关文章

  1. Android(安卓)如何批量改变字体颜色
  2. Android设置Alpha值实现图片渐变效果
  3. android之Fresco框架--Fresco基本使用
  4. 关于webview最详细讲解(包含 h5 和android 交互)
  5. 我的开源项目:Android图片剪裁库
  6. 【Android开发学习41】Android(安卓)将文字转为为图片drawText
  7. WebView加载网页基本配置
  8. Android(安卓)APP如何做开场渐变动画
  9. Android在相册中过滤GIF图片

随机推荐

  1. 在php中获取引荐来源网址(包括参数)
  2. php隔两行换色
  3. ThinkPHP3.2.3框架下where的组合查询and
  4. 关于uoloadify不能显示效果原因(thinkphp
  5. PHP 常见的数据加密技术
  6. 如何从0-X的PHP中获得一个加密的强整数?
  7. PHP 使用 OSS 批量上传图片
  8. PHP 5.0 到 7.1 常用语法糖(个人整理)
  9. PHP基础 文件流
  10. 使用wamp扩展php时出现服务未启动的解决