Android Next 公共組件庫 Tasks, Views, Widgets, Http, Utils
項目介紹
這個庫是我在日常開發過程中積累下來的一些可復用組件,有一些是原創的,有一些是修改開源項目的,有一些則完全是從Android源碼/Apache-Commons源碼中復制過來的,大部分都在我的工作項目和個人項目中有實際使用案例,具體就不一一說明了
最新版本
已經部署到Maven Central,可以直接使用
- 1.0.5 2014.12.22
- 1.0.4 2014.09.15
開始使用
Gradle集成方法:
// core 核心庫, 格式:jar和aar compile 'com.mcxiaoke.next:core:1.0.+' // http HTTP組件, 格式:jar和aar compile 'com.mcxiaoke.next:http:1.0.+' // ui UI組件, 格式:aar compile 'com.mcxiaoke.next:ui:1.0.+' // extra-abc 依賴support-v7 AppCompat 格式:aar compile 'com.mcxiaoke.next:extra-abc:1.0.+'
模塊結構
分為以下幾個模塊:
core 核心組件
包含異步任務組件,緩存組件,基礎Activity和Service,還有一些工具類,按Java包介紹如下:
-
com.mcxiaoke.next.annotation 兩個簡單的Annotation,標注是否線程安全,純標注用
-
com.mcxiaoke.next.app 基礎類,包含:
- NextBaseActivity 基礎Activity,添加了一些ActionBar相關的封裝方法
- NextBaseFragment 基礎Fragment,添加了ActionBar和Activity相關的一些封裝方法
- MultiIntentService 類似于IntentService,但是可以多個異步任務并發執行,可以在所有任務執行完成后自動stopSelf(),具體請看源碼
-
com.mcxiaoke.next.cache 簡單緩存類,包含內存緩存MemoryCache和磁盤緩存DiscCache,使用非常簡單,可定制
-
com.mcxiaoke.next.collection 幾個常用的集合類,包含:NoDuplicatesArrayList, NoDuplicatesCopyOnWriteArrayList, NoDuplicatesLinkedList和WeakFastHashMap
-
com.mcxiaoke.next.common 包含NextMessage,類似于Android系統的Message類,但是使用更方便,能支持更多數據類型
-
com.mcxiaoke.next.db 包含兩個簡單的數據庫相關的工具類
-
com.mcxiaoke.next.geo 包含LastLocationFinder,用于快速獲取上次定位位置
-
com.mcxiaoke.next.io 包含CountingInputStream, CountingOutputStream, StringBuilderWriter, BoundedInputStream等IO數據流相關的封裝類,方便使用
-
com.mcxiaoke.next.task 包含異步任務執行模塊相關的類,詳細的使用見后面的說明
- TaskQueue 核心類,對外接口,支持單例使用
- TaskCallback 回調接口
-
com.mcxiaoke.next.utils 包含了很多使用簡單但又非常有用的小工具類:
- AndroidUtils Android系統相關的一些工具類,包括文件路徑處理,Toast顯示,屏幕方向,組件啟用禁用,獲取App簽名信息等
- AssertUtils Assert類,Null檢查,對象檢查,數組檢查等
- BitmapUtils Bitmap縮放,旋轉,圓角,陰影,裁剪等方法
- CryptoUtils 加密算法相關的工具方法,支持MD5/SHA1/SHA256/AES/HEX等
- IOUtils IO操作工具類,包含常用的文件復制/字符串/數組/列表/數據流讀寫方法
- MimeUtils MIME工具類,支持根據文件擴展名獲取MIME類型
- NetworkUtils 網絡工具類,支持獲取網絡類型,設置代理等
- ProxyUtils WebView代理設置工具類,這個可能已經失效了
- ReflectionUtils Java反射相關的工具類
- StringUtils 字符串工具類,支持常用的字符串合并/分割/比較/轉換/判斷等操作
- ViewUtils View相關的幾個工具方法,例如getScreenRawSize/getActionBarHeightInDp/getStatusBarHeightInDp/getResourceValue等
- ZipUtils 支持ZIP文件壓縮/解壓縮
http HTTP組件
包含一個經過簡單封裝的HTTP操作模塊,簡化常用的網絡請求操作
- NextClient 網絡組件的核心類
- NextParams HTTP參數封裝和處理
- NextRequest HTTP Request
- NextResponse HTTP Response
詳細的使用方法見后面的介紹
ui UI組件
一些常用的UI控件,可簡化日常開發,包含:
-
AlertDialogFragment和ProgressDialogFragment 封裝好的DialogFragmen,接口簡單,同時有4.0版本和使用support-v4的版本
-
EndlessListView 封裝的ListView,添加了支持加載更多數據的接口和FooterView展示
-
CheckableFrameLayout Checkable布局系列,包含幾種常用布局的Checkable封裝,附帶一個很有用的CheckableImageView
-
NoPressStateFrameLayout NoPress布局系統,包含幾種常用布局的NoPressState封裝
-
SquaredFrameLayout Squared布局系列,包含幾個常用布局的Squared封裝,正方形布局
-
typeface typeface包里包含一些支持字體設置的常用控件,如Button/TextView/CheckBox等
-
AspectRatioImageView 定制的ImageView,縮放時會保持長寬比
-
CircularImageView 圓形ImageView,一般用作頭像等顯示,不建議使用,github有更好的項目
-
FixedRatioImageView 強制高寬比的ImageView,可選以水平或垂直方向為基準,另外還有一個強制保持正方形的SquaredImageView
-
AdvancedShareActionProvider 高級版的ShareActionProvider,支持自定義分享菜單列表項,自定圖標和分享內容等,包含4.0的版本和使用AppCompat的版本,具體可以看源碼和示例
-
ArrayAdapterCompat 增強版的ArrayAdapter,支持2.3以上版本,增加很多實用方法
samples 示例
包含幾個常用模塊的示例,主要是TaskQueue/NextClient/AdvancedShareActionProvider的示例
使用說明
異步任務模塊
// you can use TaskCallable or just Callable final TaskCallable<String> callable=new TaskCallable<String>("name") { @Override public String call() throws Exception { final String url="https://github.com/mcxiaoke/Android-Next/raw/master/README.md"; final NextResponse response=NextClient.get(url); return response.string(); } }; // task callback final TaskCallback<String> callback=new SimpleTaskCallback<String>() { @Override public void onTaskStarted(final String tag, final Bundle extras) { // task started, main thread } @Override public void onTaskSuccess(final String value, final Bundle extras) { // task success, main thread mTextView.setText(value); } @Override public void onTaskFailure(final Throwable ex, final Bundle extras) { // task failure, main thread } }; // add task, execute concurrently TaskQueue.getDefault().add(callable,callback,this); // add task, execute serially TaskQueue.getDefault().addSerially(callable, callback, this); // set custom task executor TaskQueue.getDefault().setExecutor(executor); // set yes/no check activity/fragment lifecycle TaskQueue.getDefault().setEnableCallerAliveCheck(true); // save task tag for cancel the task final String tag=TaskQueue.getDefault().add(callable,callback,this); TaskQueue.getDefault().cancel(tag); // cancel the task by caller TaskQueue.getDefault().cancelAll(this); // cancel all task TaskQueue.getDefault().cancelAll();
HTTP模塊
final String url = "https://github.com/mcxiaoke/Android-Next/raw/master/README.md"; // simple use // NextResponse response = NextClient.get(url); // advanced use final NextClient client = new NextClient(); final NextRequest request = NextRequest.newBuilder() .url(url) .encoding("UTF-8") .method("POST") .header("X-UDID", "cxgdg4543gd64tgdgs2tgdgst4") .param("image", new File("IMG_20141222.jpg"), "image/jpeg") .param("param1", "value1") // http progress callback, for monitor upload/download file progress .callback(new ProgressCallback() { @Override public void onProgress(final long currentSize, final long totalSize) { Log.v(TAG, "http progress: " + currentSize * 100 / totalSize); } }).build(); final NextResponse response = client.execute(request); // get response meta-data Log.v(TAG, "http response successful: " + response.successful()); Log.v(TAG, "http response statusCode: " + response.code()); Log.v(TAG, "http response statusMessage: " + response.message()); Log.v(TAG, "http response contentLength: " + response.contentLength()); Log.v(TAG, "http response contentType: " + response.contentType()); // get 301/302/30x location header Log.v(TAG, "http response location: " + response.location()); Log.v(TAG, "http response Server:" + response.header("Server")); Log.v(TAG, "http response Connection: " + response.header("Connection")); // get body as string Log.v(TAG, "http response content: " + response.string()); // get body as bytes final byte[] bytes = response.bytes(); final Bitmap bitmap1 = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); // get body as stream final InputStream stream = response.stream(); final Bitmap bitmap2 = BitmapFactory.decodeStream(stream); // get body as reader final InputStreamReader reader = response.reader(Charsets.UTF_8);
緩存模塊
// create memory cache, internally using map final IMemoryCache<String, Object> memoryCache = CacheFactory.createCache(); // create memory cache, internally using lru cache // final IMemoryCache<String,Object> memoryCache=CacheFactory.createLruCache(100); memoryCache.put("strKey", "value"); memoryCache.put("intKey", 123); memoryCache.put("boolKey", false); memoryCache.put("objKey", new TaskQueue()); final String value = (String) memoryCache.get("strKey"); final Context context = mockContext(); // default disc cache, use /data/data/package-name/cache/.disc/ dir final DiscCache discCache = new DiscCache(context); // use custom /data/data/package-name/cache/json-cache/ dir //final DiscCache discCache=new DiscCache(context,"json-cache"); // use custom /sdcard/Android/data/package-name/cache/json-cache/ dir //final DiscCache discCache=new DiscCache(context,"json-cache",DiscCache.MODE_EXTERNAL); discCache.setCacheDir("dirName"); discCache.setCharset("UTF-8"); discCache.setFileNameGenerator(nameGenerator); final byte[] bytes = new byte[100]; discCache.put("bytes", bytes); discCache.put("stream", new ByteArrayInputStream(bytes)); discCache.put("text", "some text for cache"); final byte[] bytesValue = discCache.getBytes("bytes"); final File file = discCache.getFile("stream"); final String stringValue = discCache.get("text"); discCache.remove("cacheKey"); discCache.clear(); discCache.delete(new FileFilter() { @Override public boolean accept(final File pathname) { return false; } });