We went on to say in the previous blog that we already know how fresco is used and how many properties it has. However, very often XML files do not meet your requirements. This requires you to dynamically change the content of the display in the code, and today we'll explore how to change the state and content of the image implementation in the code.
We've already used the Simpledraweeview control before. When the picture is displayed, a Setimageuri (URI) is written directly, and fresco not only provides this method to display the picture, it also provides the Setcontroller (Controller) method to load the picture
Draweecontroller controller = Fresco.newdraweecontrollerbuilder () . Seturi (URI) . Build (); Imageview.setcontroller (Controller);
Of course, if you want to listen to the loading process, add a Controllerlisten
Controllerlistener listener = new Basecontrollerlistener () { @Override public void Onfinalimageset (String ID, Object Imageinfo, animatable animatable) { super.onfinalimageset (ID, imageinfo, animatable); } @Override public void OnFailure (String ID, throwable throwable) { super.onfailure (ID, throwable); } @Override public void onintermediateimagefailed (String ID, throwable throwable) { super.onintermediateimagefailed (ID, throwable); } }; Draweecontroller controller = Fresco.newdraweecontrollerbuilder () . Seturi (URI) . Setcontrollerlistener ( Listener) . Build (); Imageview.setcontroller (Controller);
The picture loading succeeds or fails, will run inside the method, in which the picture loading succeeds will run the Onfinalimageset method, the picture loading fails will run the OnFailure method, assumes the picture to set the progressive type, Onintermediateimagefailed will be recalled.
After saying how to load the URI. How does the effect in XML now work? We continue to implement the effects of XML in Java code
Genericdraweehierarchy hierarchy = new Genericdraweehierarchybuilder (getresources ()) . Setfadeduration (300) . SetBackground (getdrawable (r.drawable.ic_launcher)) . Setplaceholderimage (Getdrawable (R.drawable.ic_ Launcher) . Setfailureimage (getdrawable (r.drawable.ic_launcher)) . Build (); Imageview.sethierarchy (hierarchy);
There are a lot of methods that you can use in XML to set up here, some of which can not be set in XML, for example, I can set multiple background images, I can set more than one overlay. Here can help you to achieve, is not very strong AH. Do you want to get the privilege? However, Draweehiererchy is relatively time-consuming to create, so multiple uses
Genericdraweehierarchy hierarchy1 = Imageview.gethierarchy ();
This framework is not the only thing. It also has a lot more to do with things like: it provides progressive loading of images. Show gif motion drawing and so on
The first is the progressive image loading. This aspect of the function fully consider the network is relatively slow, the user is not consistent in the same, at least to see the blurred photos, the so-called progressive loading that the user from the image loaded, the image will be blurred to a clear gradient process, of course, the process is limited to loading pictures from the network, Images in local or cached places do not need to be loaded in a progressive style. It doesn't make sense
Progressivejpegconfig config = new Progressivejpegconfig () { @Override public int Getnextscannumbertodecode ( int i) { return 0; } @Override public qualityinfo getqualityinfo (int i) { return null; } }; Imagepipelineconfig Imagepipelineconfig = Imagepipelineconfig.newbuilder (this) . Setprogressivejpegconfig ( Config) . build (); Fresco.initialize (Getapplicationcontext (), imagepipelineconfig);
Of course you can also use Progressivejpegconfig config1= new Simpleprogressivejpegconfig (list,2);
<pre name= "code" class= "Java" >flog.setminimumlogginglevel (flog.verbose); set<requestlistener> listeners = new hashset<> (); Listeners.add (New Requestlogginglistener ()); Imagepipelineconfig config = Imagepipelineconfig.newbuilder (this) . Setrequestlisteners (listeners) . Build ( ); Fresco.initialize (this, config); Setcontentview (r.layout.activity_main); Mprogressivejpegview = (Simpledraweeview) Findviewbyid (R.id.my_image_view); Uri uri = uri.parse ("http://pooyak.com/p/progjpeg/jpegload.cgi?o=1"); Imagerequest request = Imagerequestbuilder.newbuilderwithsource (URI) . setprogressiverenderingenabled (True) . Build (); Draweecontroller controller = Fresco.newdraweecontrollerbuilder () . Setimagerequest (Request) . Build (); Mprogressivejpegview.setcontroller (Controller);
Imagerequest request = Imagerequestbuilder . Newbuilderwithsource (URI) . setprogressiverenderingenabled ( true) . Build (); Pipelinedraweecontroller controller = (Pipelinedraweecontroller) fresco.newdraweecontrollerbuilder () . Setimagerequest (Request). Setoldcontroller (Imageview.getcontroller ()) . Build (); Imageview.setcontroller (Controller);
hey yo, good oh, but this image pipeline this is what ah? It's a bigger story. Take charge of the picture loading work
1. Check the memory cache, if any, to return
2. Background Threads start working
3. Check if the memory cache is not decoded.
If there is, decode, transform, return. It is then cached in the memory cache.
4. Check if it is in the file cache. Suppose to have, transform, return. Cached in the uncompressed cache and in the memory cache.
5. Load from the network or locally.
After loading, decode, transform. Return. to each cache.
Continue to look at the GIF picture, in fact with the display picture is not bad. The main is the motion picture involved in the animation stop and play, assuming only a simple trial, then directly inside the controller set Setautoplayanimation to True, Let's say you want to manually listen to the new one Controllerlistener. Manual control
When we want to download a high-definition picture from the server side. The picture is bigger, the download is very slow, some server will provide a thumbnail image. The same fresco also supports this approach. There are two different methods available in the controller setlowresimagerequest and Setimagerequest, see the method name you should be clear how to use
Personally, the most ingenious part of the framework is to save the bitmap to Ashmen and not start the GC so that the interface does not get stuck due to GC. Fresco use level three cache, the first level of cache is to save bitmap, the second level cache in memory, but no decoding, the use of the interface, the third level of the cache is saved in the local file, the same file is not decoded. The use of the time to decode the first!
The above-mentioned save a lot of content is not decoded, which is also fresco default use of 3 threads, a thread used to load the URI, a thread used to decode. The last one you know what it does. The rest of what you want to know, find it on your website.
Android continues to explore fresco