Android Fresco圖片處理庫用法API英文原文文檔1(Facebook開源Android圖片庫),

來源:互聯網
上載者:User

Android Fresco圖片處理庫用法API英文原文文檔1(Facebook開源Android圖片庫),

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 dreaded OutOfMemoryError much 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 our website.

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 the dependencies section of yourbuild.gradle file:

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

See our download page for other options.

Get StartedBuilding Fresco from source

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

Download the Android NDK. 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 our issues page to let us know of any problems.

For pull requests, please see the CONTRIBUTING file for information on how to help out.

License

Fresco is BSD-licensed. We also provide an additional patent grant.



這是英文文檔的第一部分:QUICK START


QUICK STARTAdding Fresco to your Project

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

Android Studio or Gradle

Edit your build.gradle file. You must add the following line to the dependencies section:

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

Add the following to the <dependencies> section of your pom.xml file:

<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 a SimpleDraweeView.

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

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

Near your application startup, before your app calls setContentView(), 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 the SimpleDraweeView to 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 Android View class.

Most apps should use the SimpleDraweeView class. Place these in your application using XML or Java code. Set the URI to load with the setImageURI method, as explained in the Getting Started page.

You can customize its appearance in XML.

DraweeHierarchy

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

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

DraweeController

The DraweeController is 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

DraweeControllers are immutable once constructed. They are built using the Builder pattern.

Listeners

One use of a builder is to specify a Listener to 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 called pinned purgeables to keep images off the Java heap. This requires callers to close images when they are done with them.

SimpleDraweeView does 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 does not accept 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:// HttpURLConnection or network 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 is not an image. If you want to display an XML drawable as the main image, then set it as a placeholder and use the null uri.


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.