Android continues to explore Fresco and android exploration fresco
Let's continue with the above. In the previous blog, we already know how to use Fresco and many attributes of Fresco, but in many cases, xml files cannot meet your requirements, this requires you to dynamically change the displayed content in the Code. Today we will explore how to change the status and content of image implementation in the code.
Previously, we used the SimpleDraweeView control. When displaying an image, we directly wrote a setImageURI (uri). Fresco not only provided this method to display the image, it also provides the setController (controller) method to load images.
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);
If the image is loaded successfully or fails, the method is executed. When the image is loaded successfully, the onFinalImageSet method is executed. When the image fails to be loaded, the onFailure method is executed. If the image is set progressively, the onIntermediateImageFailed method is called back
After I have finished loading the uri, how can I achieve the effect in xml? We continue to implement 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 many methods. You can set all the methods used in xml here, and some cannot be set here. For example, I can set multiple background images, I can set multiple stacked graphs, which can be implemented here. Is it very powerful? Do you want to get the same privileges! However, it is time-consuming to create DraweeHiererchy.
GenericDraweeHierarchy hierarchy1 = imageView.getHierarchy();
This framework is more than just these things. It also has many more awesome things, such as: it provides progressive loading of images, display GIF animated images, and so on.
The first is progressive image loading. This feature fully considers that when the network is slow, the user will not be consistent, and at least the Blurred photos will be visible, this so-called incremental loading means that after a user loads an image, the image will be blurred to a clear gradient process. Of course, this process is limited to loading images from the network, images in local, cached, and other places do not need to be loaded incrementally, Which is meaningless.
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 progressiveappsconfig config1 = new simpleprogressiveappsconfig (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);
Hi, that's good. But what is this image pipeline? It has a large header and is responsible for image loading.
1. Check the memory cache. If yes, return
2. Background threads start subsequent work
3. Check whether the cache is in the undecoded memory. If any, decodes, transforms, returns, and then caches the data to the memory cache.
4. Check whether the file is cached. If yes, convert and return. Cache to undecoded cache and memory cache.
5. load from the network or local. After loading, decode, transform, and return. Stored in various caches.
To continue watching GIF images, it is actually no different from displaying images, mainly because the animation involved in dynamic images is stopped and played. If you just try it out, set setAutoPlayAnimation to true directly in the controller. If you want to manually listen, manually control it in the new ControllerListener.
When we want to download a high-definition image from the server, the image is large and the download is slow, some servers will provide a thumbnail. Fresco also supports this method, the controller provides two different methods: setLowResImageRequest and setImageRequest. you should understand how to use the method name.
I personally think that the most clever part of this framework is to save bitmap to ashmen, so that gc will not be started, and the interface will not be stuck due to gc. Fresco uses a third-level cache, the first level of cache is to save bitmap. The second level of cache is stored in the memory but not decoded. The interface is required for use. The third level of cache is to save it to a local file and the file is not decoded, decoding is required before use!
Much of the stored content is not decoded. This is why fresco uses three threads by default. One thread is used to load the uri and the other thread is used to decode the content, the last one you know what it does, and the other things you want to know go to the official website.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.