Setting the actual image
The easy way is to call
For more advanced requirements, use a controller builder.
Customizing the hierarchy
For most apps, specify the parameters of their hierarchy in XML provides 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:
1 2 3 4 5 6 7 8 9 10 11
Drawable background; List<Drawable> overlaysList; GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(getResources()); GenericDraweeHierarchy hierarchy = builder .setFadeDuration(300) .setPlaceholderImage(new MyCustomDrawable()) .setBackground(background) .setOverlays(overlaysList) .build(); mSimpleDraweeView.setHierarchy(hierarchy);
setHierarchy more than once on the same view is usually unnecessary, even if the view is recycled. The hierarchy is expensive to create and in most cases you can just modify the existing hierarchy instead of creating a new one. Keep in mind that each instance of a
DraweeView needs its own instance of a
DraweeHierarchy. Same instance of a
DraweeHierarchy is not to be used by multiple views.
To change the actual image displayed by the view, use
Modifying the hierarchy in-place
Some attributes of the hierarchy can be changed without having to build a new hierarchy.
You would first need to get it from the View:
GenericDraweeHierarchy hierarchy = mSimpleDraweeView.getHierarchy();
Changing the placeholder
Then you could modify the placeholder, either with a resource id:
or a full-fledged Drawable:
Drawable placeholderImage = ...; hierarchy.setPlaceholderImage(placeholderImage);
The other image branches (failure image, retry image and progress bar) can be modified in a similar way too.
Drawable failureImage = ...; hierarchy.setFailureImage(failureImage, ScaleType.CENTER);
Changing the actual image display
You can change the scale type:
If you have chosen scale type
focusCrop, you’ll need to set a focus point:
You can add a color filter to the image:
1 2 3
ColorFilter filter; // create your filter hierarchy.setActualImageColorFilter(filter);
All of the rounding related params can be modified dynamically. You get a
RoundingParams object from the hierarchy, modify it, and set it back again:
1 2 3
RoundingParams roundingParams = hierarchy.getRoundingParams(); roundingParams.setCornersRadius(10); hierarchy.setRoundingParams(roundingParams);