Android Tiny集成圖片壓縮框架的使用
為了簡(jiǎn)化對(duì)圖片壓縮的調(diào)用,提供最簡(jiǎn)潔與合理的api壓縮邏輯,對(duì)于壓縮為Bitmap根據(jù)屏幕分辨率動(dòng)態(tài)適配最佳大小,對(duì)于壓縮為File優(yōu)化底層libjpeg的壓縮,整個(gè)圖片壓縮過(guò)程全在壓縮線程池中異步壓縮,結(jié)束后分發(fā)回UI線程。
支持的壓縮類型
Tiny圖片壓縮框架支持的壓縮數(shù)據(jù)源類型:
1、Bytes
2、File
3、Bitmap
4、Stream
5、Resource
6、Uri(network、file、content)
Tiny支持單個(gè)數(shù)據(jù)源壓縮以及批量壓縮,支持的壓縮類型:
1、數(shù)據(jù)源—>壓縮為Bitmap
2、數(shù)據(jù)源—>壓縮為File
3、數(shù)據(jù)源—>壓縮為File并返回壓縮后的Bitmap
4、批量數(shù)據(jù)源—>批量壓縮為Bitmap
5、批量數(shù)據(jù)源—>批量壓縮為File
6、批量數(shù)據(jù)源—>批量壓縮為File并返回壓縮后Bitmap
壓縮參數(shù)
Tiny.BitmapCompressOptions
Bitmap壓縮參數(shù)可配置三個(gè):
1、width
2、height
3、Bitmap.Config
如果不配置,Tiny內(nèi)部會(huì)根據(jù)屏幕動(dòng)態(tài)適配以及默認(rèn)使用ARGB_8888
Tiny.FileCompressOptions
File壓縮參數(shù)可配置四個(gè):
1、quality-壓縮質(zhì)量,默認(rèn)為76
2、isKeepSampling-是否保持原數(shù)據(jù)源圖片的寬高
3、fileSize-壓縮后文件大小
4、outfile-壓縮后文件存儲(chǔ)路徑
如果不配置,Tiny內(nèi)部會(huì)根據(jù)默認(rèn)壓縮質(zhì)量進(jìn)行壓縮,壓縮后文件默認(rèn)存儲(chǔ)在:ExternalStorage/Android/data/${packageName}/tiny/目錄下
Tiny項(xiàng)目地址: https://github.com/tianyingzhong/Tiny
Tiny與微信朋友圈的壓縮率比較
下面是使用Tiny圖片壓縮庫(kù)進(jìn)行壓縮的效果對(duì)比示例:
圖片信息 | Tiny | |
---|---|---|
6.66MB (3500x2156) | 151KB (1280x788) | 135KB (1280x789) |
4.28MB (4160x3120) | 219KB (1280x960) | 195KB (1280x960) |
2.60MB (4032x3024) | 193KB (1280x960)) | 173KB (1280x960) |
372KB (500x500) | 38.67KB (500x500) | 34.05KB (500x500) |
236KB (960x1280) | 127KB (960x1280) | 118KB (960x1280) |
壓縮為Bitmap
Tiny.BitmapCompressOptions options = new Tiny.BitmapCompressOptions(); Tiny.getInstance().source("").asBitmap().withOptions(options).compress(new BitmapCallback() { @Override public void callback(boolean isSuccess, Bitmap bitmap) { //return the compressed bitmap object } });
壓縮為File
Tiny.FileCompressOptions options = new Tiny.FileCompressOptions(); Tiny.getInstance().source("").asFile().withOptions(options).compress(new FileCallback() { @Override public void callback(boolean isSuccess, String outfile) { //return the compressed file path } });
壓縮為File并返回Bitmap
Tiny.FileCompressOptions options = new Tiny.FileCompressOptions(); Tiny.getInstance().source("").asFile().withOptions(options).compress(new FileWithBitmapCallback() { @Override public void callback(boolean isSuccess, Bitmap bitmap, String outfile) { //return the compressed file path and bitmap object } });
批量壓縮為Bitmap
Tiny.BitmapCompressOptions options = new Tiny.BitmapCompressOptions(); Tiny.getInstance().source("").batchAsBitmap().withOptions(options).batchCompress(new BitmapBatchCallback() { @Override public void callback(boolean isSuccess, Bitmap[] bitmaps) { //return the batch compressed bitmap object } });
批量壓縮為File
Tiny.FileCompressOptions options = new Tiny.FileCompressOptions(); Tiny.getInstance().source("").batchAsFile().withOptions(options).batchCompress(new FileBatchCallback() { @Override public void callback(boolean isSuccess, String[] outfile) { //return the batch compressed file path } });
批量壓縮為File并返回Bitmap
Tiny.FileCompressOptions options = new Tiny.FileCompressOptions(); Tiny.getInstance().source("").batchAsFile().withOptions(options).batchCompress(new FileWithBitmapBatchCallback() { @Override public void callback(boolean isSuccess, Bitmap[] bitmaps, String[] outfile) { //return the batch compressed file path and bitmap object } });
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程基礎(chǔ)之簡(jiǎn)單Button事件響應(yīng)綜合提示控件Toast應(yīng)用示例
這篇文章主要介紹了Android編程基礎(chǔ)之簡(jiǎn)單Button事件響應(yīng)綜合提示控件Toast應(yīng)用,結(jié)合實(shí)例形式分析了Button事件響應(yīng)與Toast提醒的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸(2)
這篇文章主要為大家詳細(xì)介紹了android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸的第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Android IPC機(jī)制Messenger實(shí)例詳解
這篇文章主要介紹了 Android IPC機(jī)制Messenger實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07