Probably the fastest image compression framework on the Android platform.
Rely on
dependencies { ... The compile 'com. Android. Support: appcompat - v7:25.3.1' compile 'com. Ghnor: flora: 1.0.0-1'}Copy the code
- Asynchronous compression:
Flora.with().load(source..).compress(new Callback<>());Copy the code
- Synchronous compression:
Flora.with().load(R.drawable.test2).compressSync();Copy the code
Controlled compression tasks
Flora.with(Activity)
Flora.with(FragmentActivity)
Flora.with(Fragment)
Flora.with(SupportFragment)
//The above four categories automatically terminate the compression task at the end of the page life cycle.
//End a series of tasks with a TAG bit.
Flora.with(TAG)
//Forcibly end a task.
Flora.cancel(TAG);
//If this parameter is not used, the task is not controlled. It is strongly recommended not to use this method.
Flora.with() Copy the code
Flora.with()
//Configure the algorithms of inSample and quality. A set of luban-based compression algorithms are built in
.calculation(new Calculation() {
@Override
public int calculateInSampleSize(int srcWidth.int srcHeight) {
return super.calculateInSampleSize(srcWidth, srcHeight);
}
@Override
public int calculateQuality(int srcWidth.int srcHeight.int targetWidth.int targetHeight) {
return super.calculateQuality(srcWidth, srcHeight, targetWidth, targetHeight); }})//After compression of the picture to do personalized processing, such as: add watermark
.addDecoration(new Decoration() {
@Override
public Bitmap onDraw(Bitmap bitmap) {
return super.onDraw(bitmap); }})//Configure the Bitmap color format
.bitmapConfig(Bitmap.Config.RGB_565)
//Maximum file size: This parameter is not recommended. The current method is to repeatedly write files. It takes time to determine the file size
.maxFileSize(1.0)
//Maximum number of compression tasks that can be performed at the same time
.compressTaskNum(1)
//Safe memory: If this parameter is set to 2, the memory required for the compression task is less than half of the available memory
.safeMemory(2)
//The compressed image is stored in the disk directory
.diskDirectory(File)
.load(source..)
.compress();Copy the code
other
- Compression speed
Internal use of thread pool scheme to compress tasks, while the necessary memory check.
Under the premise of not OOM, the maximum speed of compression is improved. The common size of 9 pictures is 20M+ and can be processed in 2S.
Of course, the machine performance, the system at the time of memory are affected by this, my test machine is [magic blue Note]…
- Effect of compression
Since the compression strategy is integrated with Luban, the final comparison of image compression size before and after can refer to Luban.
Based on this, I optimized the requirements of long graphs common in social products.
MIT License
Copyright (c) 2017 ghnor
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.