📜 ⬆️ ⬇️

Malevich

It so happened that I love small, nimble libraries, which are easy to expand on the one hand, and easy to use on the other.

But once I was faced with the fact that all the libraries for loading images that I looked at either work with images not efficiently enough, or they are difficult to remake for a specific task. I honestly decided to donate to one of the libraries, which at first glance looked good. But, alas, with regret I noticed that similar attempts to optimize this library have been unsuccessful since 2014.

Since I am a lazy person by nature - I decided to go the beaten path - I opened an example from Google Loading Large Bitmaps Efficiently and decided to cut out all unnecessary things from it - to get an easily expandable library on the one hand, and the most efficient library on the other hand.
')
Since who else but these dudes know better how to load bitmaps?

So it turned out Malevich:

image

Spoiler:

malevich.load(mImageUrl).into(mImageView); 


Cutting out all the unnecessary and correcting the bugs (there were a hell of a few), I started to add the right one)

Probably not difficult to guess what library I was inspired by simplifying syntax.

But let's actually to the library.
There are two modes of use.
Mode for loon:

 // init Malevich malevich = new Malevich.Builder(this).build(); // use malevich.load(mImageUrl).into(mImageView); // mImageUrl - Bitmap  BitmapDrawable  Resource id  HttpUrl     

And the regime for those over 30:

Memory and disk caching params

 ImageCache.ImageCacheParams cacheParams = new ImageCache.ImageCacheParams(this, "dir"); cacheParams.memoryCacheEnabled = true; //Enable memory cache cacheParams.setMemCacheSizePercent(0.4f); //Percent of available memory for cache cacheParams.compressQuality = 90; // Compress quality cacheParams.compressFormat = Bitmap.CompressFormat.PNG; // Compress format cacheParams.diskCacheEnabled = true; // Use disk cache cacheParams.diskCacheSize = 10485760; // Disk cache size 

Malevich Builder

 malevich = new Malevich.Builder(this) .debug(true) // write log .maxSize(1024) // max size of image in px .LoadingImage(R.drawable.some) // preloader image or recource .CacheParams(casheParams) // custom cache .build(); 

Loading image

Transform image with prebuild utils or custom method:

 malevich.load(url).width(mItemHeight).height(mItemHeight).imageDecodedListener(new Malevich.ImageDecodedListener() { @Override public Bitmap onImageDecoded(String data, int reqWidth, int reqHeight, Bitmap bitmap) { // Get squared bitmap and transform it to circle return Malevich.Utils.getSquaredCircleBitmap(bitmap,reqWidth); } }).into(imageView); 


But in general, the goal of the project was not to make a picasso or glide on the Google engine. I added only the most necessary utilities that are constantly used in the hard life of a freelancer - give a square from a picture, give a circle, etc. Since I wanted to keep light expandability and not turn into some kind of God forgive the mural from you know who.

Yes, the subject of my special pride is that the library contains less than a dozen classes and 0 (Zero) dependencies.

Well, all the formalities:
- customizable, gentlest lru cache in memory
- disconnected disk cache
- overlapping loading events, errors
- flow control
- Nyashnye utilities, convenient builder
- the only library effectively re-using memory according to the best recommendations from Google)

In general, if you ever have to load tons of pictures in a cleverly twisted vorkflou with all sorts of custom pieces - think of Malevich.

Github link: github.com/recoilme/malevich

I will be glad to the distributors!

Source: https://habr.com/ru/post/262189/


All Articles