Android(安卓)Fresco图片处理库用法API英文原文文档2-1(Facebook开源Android图片库)
这是英文文档的第二部分(1):DRAWEE GUIDE
由于第二部分内容多一些,所以分为2个文章发。方便大家查看。
Using Drawees in XML
Drawees have very extensive customization facilities. The best way to customize your Drawee is to do so in the XML.
Here is an example that sets nearly all possible options:
<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" />
Height and width mandatory
Youmustdeclare bothandroid:layout_width
andandroid:layout_height
. Without both of these two, the view will not be able to lay the image out correctly.
wrap_content
Drawees do not support thewrap_content
value for thelayout_width
andlayout_height
attributes.
The reason for this is that the content's size changes. The size of your downloaded image can be different from your placeholder - and the failure and retry images, if any, can be still different.
Use ofwrap_content
would force Android to do another layout pass when your image comes in - and for the layout to change before users' eyes, creating a jarring effect.
Fixing the aspect ratio
This is the one time you should usewrap_content.
You can force a DraweeView to be laid out in a particular aspect ratio. If you want a width:height ratio of 4:3, for instance, do this:
<com.facebook.drawee.view.SimpleDraweeView android:id="@+id/my_image_view" android:layout_width="20dp" android:layout_height="wrap_content" <!-- other attributes -->
Then specify your aspect ratio in Java:
mSimpleDraweeView.setAspectRatio(1.33f);
Using Drawees in Code
Change the image
The easy to way is to call
mSimpleDraweeView.setImageURI(uri);
For more advanced requirements, use acontroller builder.
Customizing the hierarchy
For most apps, specify the parameters of their hierarchy inXMLprovides all the customization they need. In some cases, though, you may need to go further.
We create an instance of the builder and then set it to the view:
List<Drawable> backgroundsList;List<Drawable> overlaysList;GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(getResources());GenericDraweeHierarchy hierarchy = builder .setFadeDuration(300) .setPlaceholderImage(new MyCustomDrawable()) .setBackgrounds(backgroundList) .setOverlays(overlaysList) .build();mSimpleDraweeView.setHierarchy(hierarchy);
DonotcallsetHierarchy
more than once on the same view, even if the view is recycled. The hierarchy is expensive to create and is intended to be used more than once. UsesetController
orsetImageURI
to change the image shown in it.
Modifying the hierarchy in-place
Some attributes of the hierarchy can be changed while the application is running.
You would first need to get it from the View:
GenericDraweeHierarchy hierarchy = mSimpleDraweeView.getHierarchy();
Change the placeholder
Then you could modify the placeholder, either with a resource id:
hierarchy.setPlaceholderImage(R.drawable.placeholderId);
or a full-fledgedDrawable:
Drawable drawable; // create your drawablehierarchy.setPlaceholderImage(drawable);
Change the image display
You can change thescale type:
hierarchy.setActualImageScaleType(ScalingUtils.ScaleType.CENTER_INSIDE);
If you have chosen scale typefocusCrop,
you'll need to set a focus point:
hierarchy.setActualImageFocusPoint(point);
You can add a color filter to the image:
ColorFilter filter;// create your filterhierarchy.setActualImageColorFilter(filter);
Rounding
All of therounding related params, except the rounding method, can be modified. You get aRoundingParams
object from the hierarchy, modify it, and set it back again:
RoundingParams roundingParams = hierarchy.getRoundingParams();roundingParams.setCornersRadius(10);hierarchy.setRoundingParams(roundingParams);
Drawee Image Branches
Contents
- Definitions
- Actual
- Placeholder
- Failure
- Retry
- Progress Bar
- Backgrounds
- Overlays
- Pressed State Overlay
Definitions
This page outlines the different image branches that can be displayed in a Drawee, and how they are set.
Except for the actual image, all of them can be set by an XML attribute. The value in XML must be either an Android drawable or color resource.
They can also be set by a method in theGenericDraweeHierarchyBuilderclass, ifsetting programmatically. In code, the value can either be from resources or be a custom subclass ofDrawable.
Some of the items can even be changed on the fly after the hierarchy has been built. These have a method in theGenericDraweeHierarchyclass.
Several of the drawables can bescaled.
Actual
Theactualimage is the target; everything else is either an alternative or a decoration. This is specified using a URI, which can point to an image over the Internet, a local file, a resource, or a content provider.
This is a property of the controller, not the hierarchy. It therefore is not set by any of the methods used by the other Drawee components.
Instead, use thesetImageURI
method orset a controllerprogrammatically.
In addition to the scale type, the hierarchy exposes other methods only for the actual image. These are:
- the focus point (used for thefocusCropscale type only)
- a color filter
Default scale type:centerCrop
Placeholder
Theplaceholderis shown in the Drawee when it first appears on screen. After you have calledsetController
orsetImageURI
to load an image, the placeholder continues to be shown until the image has loaded.
In the case of a progressive JPEG, the placeholder only stays until your image has reached the quality threshold, whether the default, or one set by your app.
XML attribute:placeholderImage
Hierarchy builder method:setPlaceholderImage
Hierarchy method:setPlaceholderImage
Default value: a transparentColorDrawable
Default scale type:centerInside
Failure
Thefailureimage appears if there is an error loading your image. The most common cause of this is an invalid URI, or lack of connection to the network.
XML attribute:failureImage
Hierarchy builder method:setFailureImage
Default value: The placeholder image
Default scale type:centerInside
Retry
Theretryimage appears instead of the failure image if you have set your controller to enable the tap-to-retry feature.
You mustbuild your own Controllerto do this. Then add the following line
.setTapToRetryEnabled(true)
The image pipeline will then attempt to retry an image if the user taps on it. Up to four attempts are allowed before the failure image is shown instead.
XML attribute:retryImage
Hierarchy builder method:setRetryImage
Default value: The placeholder image
Default scale type:centerInside
Progress Bar
If specified, theprogress barimage is shown as an overlay over the Drawee until the final image is set.
Currently the progress bar remains the same throughout the image load; actually changing in response to progress is not yet supported.
XML attribute:progressBarImage
Hierarchy builder method:setProgressBarImage
Default value: None
Default scale type:centerInside
Backgrounds
Backgrounddrawables are drawn first, "under" the rest of the hierarchy.
Only one can be specified in XML, but in code more than one can be set. In that case, the first one in the list is drawn first, at the bottom.
Background images don't support scale-types and are scaled to the Drawee size.
XML attribute:backgroundImage
Hierarchy builder method:setBackground,
setBackgrounds
Default value: None
Default scale type: N/A
Overlays
Overlaydrawables are drawn last, "over" the rest of the hierarchy.
Only one can be specified in XML, but in code more than one can be set. In that case, the first one in the list is drawn first, at the bottom.
Overlay images don't support scale-types and are scaled to the Drawee size.
XML attribute:overlayImage
Hierarchy builder method:setOverlay,
setOverlays
Default value: None
Default scale type: N/A
Pressed State Overlay
Thepressed state overlayis a special overlay shown only when the user presses the screen area of the Drawee. For example, if the Drawee is showing a button, this overlay could have the button change color when pressed.
The pressed state overlay doesn't support scale-types.
XML attribute:pressedStateOverlayImage
Hierarchy builder method:setPressedStateOverlay
Default value: None
Default scale type: N/A
Scaling
You can specify a different scale type for each of thedifferent drawablesin your Drawee. The
Available scale types
Scale Type | Explanation |
---|---|
center | Center the image in the view, but perform no scaling. |
centerCrop | Scales the image so that both dimensions will be greater than or equal to the corresponding dimension of the parent. One of width or height will fit exactly. The image is centered within parent's bounds. |
focusCrop | Same as centerCrop, but based around a caller-specified focus point instead of the center. |
centerInside | Downscales the image so that it fits entirely inside the parent. Unlike fitCenter, no upscaling will be performed.Aspect ratio is preserved. The image is centered within parent's bounds. |
fitCenter | Scales the image so that it fits entirely inside the parent. One of width or height will fit exactly. Aspect ratio is preserved. The image is centered within the parent's bounds. |
fitStart | Scales the image so that it fits entirely inside the parent. One of width or height will fit exactly. Aspect ratio is preserved. The image is aligned to the top-left corner of the parent. |
fitEnd | Scales the image so that it fits entirely inside the parent. One of width or height will fit exactly. Aspect ratio is preserved. The image is aligned to the bottom-right corner of the parent. |
fitXY | Scales width and height independently, so that the image matches the parent exactly. Aspect ratio is not preserved. |
none | Used for Android's tile mode. |
These are mostly the same as those supported by the AndroidImageViewclass.
The one unsupported type ismatrix.
In its place, Fresco offersfocusCrop,
which will usually work better.
How to set
Actual, placeholder, retry, and failure images can all beset in XML, using attributes likefresco:actualImageScaleType
. You can also set themin codeusing theGenericDraweeHierarchyBuilderclass.
Even after your hierarchy is built, the actual image scale type can be modified on the fly usingGenericDraweeHierarchy.
However, donotuse theandroid:scaleType
attribute, nor the.setScaleType
method. These have no effect on Drawees.
focusCrop
Android, and Fresco, offer acenterCrop
scale type, which will fill the entire viewing area while preserving the aspect ratio of the image, cropping as necessary.
This is very useful, but the trouble is the cropping doesn't always happen where you need it. If, for instance, you want to crop to someone's face in the bottom right corner of the image,centerCrop
will do the wrong thing.
By specifying a focus point, you can say which part of the image should be centered in the view. If you specify the focus point to be at the top of the image, such as (0.5f, 0f), we guarantee that, no matter what, this point will be visible and centered in the view as much as possible.
Focus points are specified in a relative coordinate system. That is, (0f, 0f) is the top-left corner, and (1f, 1f) is the bottom-right corner. Relative coordinates allow focus points to be scale-invariant, which is highly useful.
A focus point of (0.5f, 0.5f) is equivalent to a scale type ofcenterCrop.
To use focus points, you must first set the right scale type in your XML:
fresco:actualImageScaleType="focusCrop"
In your Java code, you must programmatically set the correct focus point for your image:
PointF focusPoint;// your app populates the focus pointmSimpleDraweeView .getHierarchy() .setActualImageFocusPoint(focusPoint);
none#
If you are using Drawables that make use of Android's tile mode, you need to use thenone
scale type for this to work correctly.
Rounded Corners and Circles
Not every image is a rectangle. Apps frequently need images that appear with softer, rounded corners, or as circles. Drawee supports a variety of scenarios, all without the memory overhead of copying bitmaps.
What
Images can be rounded in two shapes:
- As a circle - set
roundAsCircle
to true. - As a rectangle, but with rounded corners. Set
roundedCornerRadius
to some value.
Rectangles support having each of the four corners have a different radius, but this must be specified in Java code rather than XML.
How
Images can be rounded with two different methods:
BITMAP_ONLY
- Uses a shader to draw the bitmap with rounded corners. This is the default rounding method. This works only on the actual image and theplaceholder. Other branches, like failure and retry images, are not rounded. Furthermore, this rounding method doesn't support animations.OVERLAY_COLOR
- Draws rounded corners by overlaying a solid color, specified by the caller. The Drawee's background should be static and of the same solid color. UseroundWithOverlayColor
in XML, orsetOverlayColor
in code, for this effect.
In XML
TheSimpleDraweeView
class will forward several attributes over toRoundingParams
:
<com.facebook.drawee.view.SimpleDraweeView ... fresco:roundedCornerRadius="5dp" fresco:roundBottomLeft="false" fresco:roundBottomRight="false" fresco:roundWithOverlayColor="@color/blue" fresco:roundingBorderWidth="1dp" fresco:roundingBorderColor="@color/red"
In code#
Whenconstructing a hierarchy, you can pass an instance ofRoundingParamsto yourGenericDraweeHierarchyBuilder:
RoundingParams roundingParams = RoundingParams.fromCornersRadius(7f);roundingParams.setOverlayColor(R.color.green);// alternatively use fromCornersRadii or asCirclegenericDraweeHierarchyBuilder .setRoundingParams(roundingParams);
You can also change most of the rounding parameters on the fly:
RoundingParams roundingParams = mSimpleDraweeView.getHierarchy().getRoundingParams();roundingParams.setBorder(R.color.red, 1.0);roundingParams.setRoundAsCircle(true);mSimpleDraweeView.getHierarchy().setRoundingParams(roundingParams);
The one exception to this is that theRoundingMethod
cannot be changed when changing dynamically. Attempting to do so will throw anIllegalStateException.
更多相关文章
- 用命令行编译APK(英文版)
- android国际化操作
- Android(安卓)- TextView限制文本长度,英文占1位,中文占2位
- 13. android Context Menu With Icon (英文)
- 中文路径导致的报错
- android多国语言文件夹
- Android(java)学习笔记117:英文朗诵android App编写实例
- 话语收集
- android多国语言文件夹文件汇总