Cost-effective Refactoring Solution from Fresco to Glide for Android Projects

Click on the Terminal R&D Department above, then select Set as Favorite

Technical articles delivered on time every day at 8:30 AM

Reply Learning in the public account backend to get exclusive quality materials from the author

Cost-effective Refactoring Solution from Fresco to Glide for Android Projects

Previous Articles

Breaking news! Microsoft GitHub finally announces the official app

Newly released Android senior interview records at the end of 2019~
As a hacker, discovered my girlfriend’s infidelity through technical means…
Java coding standards from Google, highly recommended, both authoritative and scientific!
Cost-effective Refactoring Solution from Fresco to Glide for Android Projects

Reprinted from the author:ImWiki

Link:https://www.jianshu.com/p/c8ec2ac8a465

Introduction

Fresco is a highly invasive framework; if we use Fresco, we have to replace our ImageView with SimpleDraweeView. If we use a custom ImageView, it becomes very troublesome. Although SimpleDraweeView provides very rich features, such as rounded corners and borders, sometimes it can cause very strange layout anomalies. Before version 5.0, Fresco could effectively reduce OOM, but now it has no advantage. Therefore, the project plans to replace Fresco with Glide.
Disadvantages of Fresco
  1. The package is too large.
  2. Prone to unknown layout errors.
  3. Highly invasive, must use Fresco’s view controls.
Replacement Analysis
Since the project heavily uses SimpleDraweeView to implement layouts, and SimpleDraweeView provides rich features like rounded corners, circles, and borders, modifying the entire project’s layout code is unrealistic. Therefore, we need to create a View with the same package name, attribute names, and functionality as SimpleDraweeView to significantly reduce replacement costs. However, this SimpleDraweeView is not fully functional because complete imitation is too costly, so we need to remove setImageRequest(ImageRequest request) and getControllerBuilder() method calls and only provide general calling methods and properties.

Custom Replacement Control

I have already packaged the replacement solution ImageViewEx, which can be directly introduced into the project.
Cost-effective Refactoring Solution from Fresco to Glide for Android Projects
Custom RoundedCornerDrawable
We can actually draw the image directly in the ImageView.onDraw method, but using a custom Drawable allows for better component controllability and reusability. Since drawing is quite complex, I won’t introduce it all here; please browse my GitHub code directly.
Custom ImageViewExBase
Create an abstract base ImageView class to support rounded corners, borders, and other functions.
Custom ImageViewEx
Inherit from ImageViewExBase, this is used to replace SimpleDraweeView. In the future, we should try to avoid using SimpleDraweeView and use ImageViewEx instead.
Custom SimpleDraweeView
This class is key and also inherits from ImageViewExBase. Its functionality is the same as ImageViewEx; the only difference is that the XML attributes continue from Fresco, and the package name must be com.facebook.drawee.view, and the XML attributes must also be consistent with Fresco. It is necessary to override setImageURI(Uri uri) and use a callback method to allow developers to implement image display.
Cost-effective Refactoring Solution from Fresco to Glide for Android Projects
Set LoadHandler
Cost-effective Refactoring Solution from Fresco to Glide for Android Projects

Explanation

The provided attributes of com.facebook.drawee.view.SimpleDraweeView support all Fresco attributes, but not all functionalities are supported. After replacement, deep testing is required to avoid errors.

Open Source Project Address

https://github.com/taoweiji/ImageViewEx

Read more

Boss: Please conduct a Code Review on your code!

Memories of an Android interview in May

Research on the implementation of screen adaptation solutions by Toutiao

APK Slimming – It’s time to reduce the burden on the App

Learn and use Android Jetpack – Navigation

Mobile manufacturers have conducted intensive testing on “Hongmeng”:Is it faster than the Android system?

Impressive Java 8 Stream, is its performance impressive too?

Believe in yourself, there is nothing you cannot do, only things you haven’t thought of

What you gain here is not just technology!

Cost-effective Refactoring Solution from Fresco to Glide for Android Projects

Cost-effective Refactoring Solution from Fresco to Glide for Android Projects

If you like it, give a “LikeCost-effective Refactoring Solution from Fresco to Glide for Android Projects

Leave a Comment