Now there are a lot of picture loading frame, common glide, Fresco, Picasso and so on. Glide because of its small size, powerful caching mechanism and other advantages, by the majority of programmers favor; Fresco Although the volume is larger, the cache mechanism is not glide powerful, but it wins in having some cool effect (progress bar, fade effect), etc., there are many people in use. Today we are going to introduce the use of fresco.
First, "Fresco's official Chinese document" is posted.
1. Configure the Environment
Using fresco requires first importing dependencies in Build.gradle:
Fresco required to rely on compile ' com.facebook.fresco:fresco:0.12.0 ' /// on the machine support WebP on API < 14, you need to add Compile ' com.facebook.fresco:animated-base-support:0.12.0 '//// support WebP (static diagram + motion diagram), need to add compile ' com.facebook.fresco:animated-webp:0.12.0 ' compile ' com.facebook.fresco:webpsupport:0.12.0 '
If you need to download pictures from the network, you need to configure network permissions in the Androidmanifest.xml file:
<android:name/>
Finally, initialize the fresco in the application of the project:
Extends application { @Override voidSuper// Initialize Fresco fresco.initialize (this ); }}
Don't forget to add the Name property for application in the Androidmanifest.xml file:
<application android:name= ". MyApplication " ... </application>
2. SimpleDraweeView2.1, XML properties
Fresco provides us with a Simpledraweeview control that we can load directly into this control. Fresco integrates a number of attributes into this control, which is simple and practical. The following are the common properties in Simpledraweeview:
Fresco:actualimagescaletype: The scaled style of the actual loaded picture Fresco:backgroundimage: The underlying image resource fresco: Fadeduration: progress bar and placeholder pictures fade away, loaded pictures gradually show time interval fresco:failureimage: Picture resource fresco displayed when loading fails : Failureimagescaletype: Scaling style for pictures loaded when loading fails fresco:overlayimage: Overlay A picture of the picture on the surface of the displayed image resource fresco: Placeholderimage: Placeholder Picture resource fresco:placeholderimagescaletype: Placeholder picture's telescopic style fresco: Progressbarautorotateinterval: progress bar picture rotation period fresco:progressbarimage: progress bar Picture resource Fresco: Progressbarimagescaletype: The scaling style of the progress bar picture Fresco:retryimage: The picture resource fresco that prompts reload : Retryimagescaletype: The stretch style of the picture that prompts reloading fresco:roundascircle: Cut the picture into a circular fresco:viewaspectratio: Picture aspect ratio
The code in our XML layout file is as follows:
<?XML version= "1.0" encoding= "Utf-8"?><RelativelayoutXmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:fresco= "Http://schemas.android.com/apk/res-auto"Android:id= "@+id/activity_main"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"><Com.facebook.drawee.view.SimpleDraweeViewAndroid:id= "@+id/id_main_sdv_sdv"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Fresco:actualimagescaletype= "Focuscrop"Fresco:fadeduration= "3000"Fresco:failureimage= "@mipmap/ic_launcher"Fresco:failureimagescaletype= "Centerinside"Fresco:placeholderimage= "@mipmap/ic_launcher"Fresco:placeholderimagescaletype= "Fitcenter"Fresco:progressbarautorotateinterval= "1000"Fresco:progressbarimage= "@mipmap/ic_launcher"Fresco:progressbarimagescaletype= "Centerinside"Fresco:retryimage= "@mipmap/ic_launcher"Fresco:retryimagescaletype= "Centercrop"Fresco:roundascircle= "false"Fresco:viewaspectratio= "1.6" /> <button android:id= "@+id/id_main _btn_load " Android:layout_width=" Match_parent " Android:layout_height= "Wrap_content" Android:layout_alignparentbottom= "true" Android:layout_margin=" 20.0dip " Android:text= "Start Load Image" /></relativelayout>
2.2. Precautions
(1) The width height of the Simpledraweeview control cannot be set to Wrap_content, only match_parent, specific values, or the aspect ratio is set using the Viewaspectratio property.
(2) If you load multiple pictures in one page, do not place Simpledraweeview directly in Scollview, we recommend using Recyclerview, ListView, or GridView. Because Simpledraweeview in ScrollView will hold memory until the activity or fragment is destroyed.
(3) When using Simpledraweeview, do not use attributes that are in ImageView and not in view.
3, Frame use 3.1, simple use
Like TextView, button and other controls go to Simpledraweeview, and then call the following code to load the network picture:
Simpledraweeview SDV = (Simpledraweeview) Findviewbyid (R.ID.ID_MAIN_SDV_SDV) Sdv.setimageuri ("http:// Image5.tuku.cn/pic/wallpaper/fengjing/menghuandaziranmeijingbizhi/009.jpg ");
3.2. Java Code Settings Properties
We can dynamically set the properties of the Simpledraweeview control in Java code through Genericdraweehierarchy. Note that if you set properties in Java code, the properties set in the XML file are not valid. An example code is as follows:
//The code sets the properties of the Simpledraweeview (overrides all the attributes of the XML setting, that is, properties that are not in the XML will be invalidated)//Note: A genericdraweehierarchy is a genericdraweehierarchy hierarchy that cannot be shared by multiple Simpledraweeview =NewGenericdraweehierarchybuilder (Getresources ()). Setfadeduration (3000new progressbardrawable ()) // show progress bar (fresco comes with a progress bar) build (); // set picture fillet roundingparams roundingparams = new Roundingparams (); Roundingparams.setroundascircle (false); // do not cut the picture into round Roundingparams.setcornersradius (200// set property Simpledraweeview for Sdv.sethierarchy (hierarchy);
Note: You cannot set the same Genericdraweehierarchy object to more than one simpledraweeview!
3.3. Set up Download events
Sdv.setcontroller (Fresco.newdraweecontrollerbuilder (). Setcontrollerlistener (New basecontrollerlistener<imageinfo>() {@OverridePublicvoidOnfinalimageset (String ID, imageinfo imageinfo, animatable animatable) {//The method that triggers when all pictures are loaded successfullyint width =Imageinfo.getwidth ();int height =Imageinfo.getheight (); Qualityinfo Qualityinfo =Imageinfo.getqualityinfo ();int quality =Qualityinfo.getquality ();Boolean isoffullquality = qualityinfo.isoffullquality (); boolean isofgoodenoughquality = Qualityinfo.isofgoodenoughquality (); } @Override public void Onintermediateimageset (String ID, imageinfo imageinfo) {// callback method when loading progressive picture } @Override Public void OnFailure (String ID, throwable throwable) { Span style= "COLOR: #008000" >// method of callback when loading a picture fails }}). Seturi (" Http://image5.tuku.cn/pic/wallpaper/fengjing/menghuandaziranmeijingbizhi/009.jpg " ). Build ());
3.4. Progressive picture
Progressive picture is a kind of support image from Blur to clear loading mode. The code is as follows:
Sdv.setcontroller (Fresco.newdraweecontrollerbuilder () . Setimagerequest ( Imagerequestbuilder.newbuilderwithsource ( uri.parse ("http://image5.tuku.cn/pic/wallpaper/fengjing/ Menghuandaziranmeijingbizhi/009.jpg ")) . setprogressiverenderingenabled (true) . Build ()) . Setoldcontroller (Sdv.getcontroller ()) . Build ());
The above is the basic use of fresco introduction, I hope everyone has help ~ ~ ~
Use of the "Android-frame" fresco