├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── othershe │ │ └── combinebitmaptest │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── othershe │ │ │ └── combinebitmaptest │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable-xxhdpi │ │ └── cat.jpg │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── othershe │ └── combinebitmaptest │ └── ExampleUnitTest.java ├── build.gradle ├── combinebitmap ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── othershe │ │ └── combinebitmap │ │ ├── CombineBitmap.java │ │ ├── cache │ │ ├── DiskLruCacheHelper.java │ │ └── LruCacheHelper.java │ │ ├── helper │ │ ├── BitmapLoader.java │ │ ├── Builder.java │ │ ├── CombineHelper.java │ │ ├── CompressHelper.java │ │ ├── ProgressHandler.java │ │ ├── ThreadPool.java │ │ └── Utils.java │ │ ├── layout │ │ ├── DingLayoutManager.java │ │ ├── ILayoutManager.java │ │ └── WechatLayoutManager.java │ │ ├── listener │ │ ├── OnHandlerListener.java │ │ ├── OnProgressListener.java │ │ └── OnSubItemClickListener.java │ │ └── region │ │ ├── DingRegionManager.java │ │ ├── IRegionManager.java │ │ └── WechatRegionManager.java │ └── res │ ├── drawable │ └── defalut_placeholder.png │ └── values │ └── strings.xml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── images ├── d1.PNG ├── d2.PNG ├── d3.PNG ├── w1.PNG ├── w2.PNG ├── w3.PNG ├── w4.PNG ├── w5.PNG ├── w6.PNG ├── w7.PNG └── w8.PNG └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files // 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/workspace.xml 38 | *.iws 39 | .idea/ 40 | 41 | # Keystore files 42 | *.jks 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CombineBitmap 2 | 3 | ### 效果预览 4 | 5 | |![](images/d1.PNG)|![](images/d2.PNG)|![](images/d3.PNG)|| 6 | |---|---|---|---| 7 | |![](images/w1.PNG)|![](images/w2.PNG)|![](images/w3.PNG)|![](images/w4.PNG)| 8 | |![](images/w5.PNG)|![](images/w6.PNG)|![](images/w7.PNG)|![](images/w8.PNG)| 9 | 10 | ### 功能 11 | * 生成类似钉钉、微信 群聊组合头像Bitmap 12 | * 可使用图片资源id、bitmap或者使用url从网络加载,传入对应数组即可 13 | * 网络加载时支持线程池 14 | * 支持磁盘缓存、内存缓存。(**记得申请磁盘缓存需要的文件存储权限**) 15 | * 对图片资源进行采样率压缩 16 | * 支持子图像的点击事件 17 | * ...... 18 | ### 基本用法 19 | **Step 1. 添加JitPack仓库** 20 | 在项目根目录下的 `build.gradle` 中添加仓库: 21 | ``` gradle 22 | allprojects { 23 | repositories { 24 | ... 25 | maven { url "https://jitpack.io" } 26 | } 27 | } 28 | ``` 29 | **Step 2. 添加项目依赖** 30 | ``` gradle 31 | dependencies { 32 | implementation 'com.github.Othershe:CombineBitmap:1.0.5' 33 | } 34 | ``` 35 | **Step 3. 配置** 36 | ``` java 37 | CombineBitmap.init(context) 38 | .setLayoutManager() // 必选, 设置图片的组合形式,支持WechatLayoutManager、DingLayoutManager 39 | .setSize() // 必选,组合后Bitmap的尺寸,单位dp 40 | .setGap() // 单个图片之间的距离,单位dp,默认0dp 41 | .setGapColor() // 单个图片间距的颜色,默认白色 42 | .setPlaceholder() // 单个图片加载失败的默认显示图片 43 | .setUrls() // 要加载的图片url数组 44 | .setBitmaps() // 要加载的图片bitmap数组 45 | .setResourceIds() // 要加载的图片资源id数组 46 | .setImageView() // 直接设置要显示图片的ImageView 47 | // 设置“子图片”的点击事件,需使用setImageView(),index和图片资源数组的索引对应 48 | .setOnSubItemClickListener(new OnSubItemClickListener() { 49 | @Override 50 | public void onSubItemClick(int index) { 51 | 52 | } 53 | }) 54 | // 加载进度的回调函数,如果不使用setImageView()方法,可在onComplete()完成最终图片的显示 55 | .setProgressListener(new ProgressListener() { 56 | @Override 57 | public void onStart() { 58 | 59 | } 60 | 61 | @Override 62 | public void onComplete(Bitmap bitmap) { 63 | 64 | } 65 | }) 66 | .build(); 67 | ``` 68 | 由于生成的组合Bitmap是矩形的,要实现钉钉的圆形显示效果,这里用到了一个可圆形显示的ImageView控件:[NiceImageView](https://github.com/Othershe/NiceImageView) 69 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "com.othershe.combinebitmaptest" 7 | minSdkVersion 14 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:27.1.1' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.2' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | 29 | implementation 'pub.devrel:easypermissions:0.4.2' 30 | implementation project(':combinebitmap') 31 | implementation 'com.github.Othershe:NiceImageView:1.0.0' 32 | } 33 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/othershe/combinebitmaptest/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmaptest; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.othershe.combinebitmaptest", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/othershe/combinebitmaptest/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmaptest; 2 | 3 | import android.Manifest; 4 | import android.graphics.Bitmap; 5 | import android.graphics.BitmapFactory; 6 | import android.graphics.Color; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.os.Bundle; 9 | import android.util.Log; 10 | import android.widget.ImageView; 11 | 12 | import com.othershe.combinebitmap.CombineBitmap; 13 | import com.othershe.combinebitmap.layout.DingLayoutManager; 14 | import com.othershe.combinebitmap.layout.WechatLayoutManager; 15 | import com.othershe.combinebitmap.listener.OnProgressListener; 16 | import com.othershe.combinebitmap.listener.OnSubItemClickListener; 17 | import com.othershe.library.NiceImageView; 18 | 19 | import java.util.List; 20 | 21 | import pub.devrel.easypermissions.AfterPermissionGranted; 22 | import pub.devrel.easypermissions.AppSettingsDialog; 23 | import pub.devrel.easypermissions.EasyPermissions; 24 | 25 | public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks { 26 | 27 | private String[] IMG_URL_ARR = { 28 | "http://img.hb.aicdn.com/eca438704a81dd1fa83347cb8ec1a49ec16d2802c846-laesx2_fw658", 29 | "http://img.hb.aicdn.com/729970b85e6f56b0d029dcc30be04b484e6cf82d18df2-XwtPUZ_fw658", 30 | "http://img.hb.aicdn.com/85579fa12b182a3abee62bd3fceae0047767857fe6d4-99Wtzp_fw658", 31 | "http://img.hb.aicdn.com/2814e43d98ed41e8b3393b0ff8f08f98398d1f6e28a9b-xfGDIC_fw658", 32 | "http://img.hb.aicdn.com/a1f189d4a420ef1927317ebfacc2ae055ff9f212148fb-iEyFWS_fw658", 33 | "http://img.hb.aicdn.com/69b52afdca0ae780ee44c6f14a371eee68ece4ec8a8ce-4vaO0k_fw658", 34 | "http://img.hb.aicdn.com/9925b5f679964d769c91ad407e46a4ae9d47be8155e9a-seH7yY_fw658", 35 | "http://img.hb.aicdn.com/e22ee5730f152c236c69e2242b9d9114852be2bd8629-EKEnFD_fw658", 36 | "http://img.hb.aicdn.com/73f2fbeb01cd3fcb2b4dccbbb7973aa1a82c420b21079-5yj6fx_fw658", 37 | }; 38 | 39 | NiceImageView imageView1; 40 | NiceImageView imageView2; 41 | NiceImageView imageView3; 42 | ImageView imageView4; 43 | ImageView imageView5; 44 | ImageView imageView6; 45 | ImageView imageView7; 46 | ImageView imageView8; 47 | ImageView imageView9; 48 | ImageView imageView10; 49 | ImageView imageView11; 50 | 51 | 52 | private int[] getResourceIds(int count) { 53 | int[] res = new int[count]; 54 | for (int i = 0; i < count; i++) { 55 | res[i] = R.drawable.cat; 56 | } 57 | return res; 58 | } 59 | 60 | private String[] getUrls(int count) { 61 | String[] urls = new String[count]; 62 | System.arraycopy(IMG_URL_ARR, 0, urls, 0, count); 63 | return urls; 64 | } 65 | 66 | private Bitmap[] getBitmaps(int count) { 67 | Bitmap[] bitmaps = new Bitmap[count]; 68 | for (int i = 0; i < count; i++) { 69 | bitmaps[i] = BitmapFactory.decodeResource(getResources(), R.drawable.cat); 70 | } 71 | return bitmaps; 72 | } 73 | 74 | @Override 75 | protected void onCreate(Bundle savedInstanceState) { 76 | super.onCreate(savedInstanceState); 77 | setContentView(R.layout.activity_main); 78 | 79 | imageView1 = findViewById(R.id.nice_iv1); 80 | imageView2 = findViewById(R.id.nice_iv2); 81 | imageView3 = findViewById(R.id.nice_iv3); 82 | imageView4 = findViewById(R.id.iv4); 83 | imageView5 = findViewById(R.id.iv5); 84 | imageView6 = findViewById(R.id.iv6); 85 | imageView7 = findViewById(R.id.iv7); 86 | imageView8 = findViewById(R.id.iv8); 87 | imageView9 = findViewById(R.id.iv9); 88 | imageView10 = findViewById(R.id.iv10); 89 | imageView11 = findViewById(R.id.iv11); 90 | requestStoragePermission(); 91 | } 92 | 93 | @AfterPermissionGranted(1000) 94 | private void requestStoragePermission() { 95 | String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE}; 96 | if (EasyPermissions.hasPermissions(this, perms)) { 97 | loadDingBitmap(imageView1, 2); 98 | 99 | loadDingBitmap(imageView2, 3); 100 | 101 | loadDingBitmap(imageView3, 4); 102 | 103 | loadWechatBitmap(imageView4, 2); 104 | 105 | loadWechatBitmap(imageView5, 3); 106 | 107 | loadWechatBitmap(imageView6, 4); 108 | 109 | loadWechatBitmap(imageView7, 5); 110 | 111 | loadWechatBitmap(imageView8, 6); 112 | 113 | loadWechatBitmap(imageView9, 7); 114 | 115 | loadWechatBitmap(imageView10, 8); 116 | 117 | loadWechatBitmap(imageView11, 9); 118 | 119 | } else { 120 | EasyPermissions.requestPermissions(this, "need storage permission", 1000, perms); 121 | } 122 | } 123 | 124 | @Override 125 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 126 | EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); 127 | } 128 | 129 | @Override 130 | public void onPermissionsGranted(int requestCode, List perms) { 131 | 132 | } 133 | 134 | @Override 135 | public void onPermissionsDenied(int requestCode, List perms) { 136 | if (1000 == requestCode) { 137 | if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) { 138 | new AppSettingsDialog.Builder(this) 139 | .setTitle("tip") 140 | .setRationale("need storage permission") 141 | .build() 142 | .show(); 143 | } 144 | } 145 | } 146 | 147 | private void loadWechatBitmap(ImageView imageView, int count) { 148 | CombineBitmap.init(MainActivity.this) 149 | .setLayoutManager(new WechatLayoutManager()) 150 | .setSize(180) 151 | .setGap(3) 152 | .setGapColor(Color.parseColor("#E8E8E8")) 153 | .setUrls(getUrls(count)) 154 | .setImageView(imageView) 155 | .setOnSubItemClickListener(new OnSubItemClickListener() { 156 | @Override 157 | public void onSubItemClick(int index) { 158 | Log.e("SubItemIndex", "--->" + index); 159 | } 160 | }) 161 | .build(); 162 | } 163 | 164 | private void loadDingBitmap(final ImageView imageView, int count) { 165 | CombineBitmap.init(MainActivity.this) 166 | .setLayoutManager(new DingLayoutManager()) 167 | .setSize(180) 168 | .setGap(2) 169 | .setUrls(getUrls(count)) 170 | .setOnProgressListener(new OnProgressListener() { 171 | @Override 172 | public void onStart() { 173 | 174 | } 175 | 176 | @Override 177 | public void onComplete(Bitmap bitmap) { 178 | imageView.setImageBitmap(bitmap); 179 | } 180 | }) 181 | .build(); 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/app/src/main/res/drawable-xxhdpi/cat.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | 15 | 21 | 22 | 28 | 29 | 35 | 36 | 37 | 42 | 43 | 48 | 49 | 54 | 55 | 60 | 61 | 66 | 67 | 72 | 73 | 78 | 79 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | CombineBitmap 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/othershe/combinebitmaptest/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmaptest; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.1.3' 11 | 12 | 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | } 17 | 18 | allprojects { 19 | repositories { 20 | google() 21 | jcenter() 22 | maven { url 'https://jitpack.io' } 23 | } 24 | } 25 | 26 | task clean(type: Delete) { 27 | delete rootProject.buildDir 28 | } 29 | -------------------------------------------------------------------------------- /combinebitmap/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /combinebitmap/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 27 5 | 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 14 10 | targetSdkVersion 27 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 16 | } 17 | 18 | buildTypes { 19 | release { 20 | postprocessing { 21 | removeUnusedCode false 22 | removeUnusedResources false 23 | obfuscate false 24 | optimizeCode false 25 | proguardFile 'proguard-rules.pro' 26 | } 27 | } 28 | } 29 | 30 | } 31 | 32 | dependencies { 33 | implementation fileTree(dir: 'libs', include: ['*.jar']) 34 | 35 | implementation 'com.android.support:appcompat-v7:27.1.1' 36 | 37 | implementation 'com.jakewharton:disklrucache:2.0.2' 38 | } 39 | -------------------------------------------------------------------------------- /combinebitmap/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /combinebitmap/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/CombineBitmap.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap; 2 | 3 | import android.content.Context; 4 | 5 | import com.othershe.combinebitmap.helper.Builder; 6 | 7 | public class CombineBitmap { 8 | public static Builder init(Context context) { 9 | return new Builder(context); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/cache/DiskLruCacheHelper.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.cache; 2 | 3 | import android.content.Context; 4 | import android.os.Build; 5 | import android.os.Environment; 6 | import android.os.StatFs; 7 | 8 | import com.jakewharton.disklrucache.DiskLruCache; 9 | 10 | import java.io.File; 11 | import java.io.IOException; 12 | 13 | public class DiskLruCacheHelper { 14 | private long DISK_CACHE_SIZE = 1024 * 1024 * 50; 15 | private String CACHE_DIR = "bitmap_cache"; 16 | 17 | public DiskLruCache mDiskLruCache; 18 | 19 | public DiskLruCacheHelper(Context context) { 20 | File diskCacheDir = getDiskCacheDir(context); 21 | if (!diskCacheDir.exists()) { 22 | diskCacheDir.mkdirs(); 23 | } 24 | if (getUsableSpace(diskCacheDir) > DISK_CACHE_SIZE) { 25 | try { 26 | mDiskLruCache = DiskLruCache.open(diskCacheDir, 1, 1, DISK_CACHE_SIZE); 27 | } catch (IOException e) { 28 | e.printStackTrace(); 29 | } 30 | } 31 | } 32 | 33 | private File getDiskCacheDir(Context context) { 34 | boolean externalStorageAvailable = Environment 35 | .getExternalStorageState().equals(Environment.MEDIA_MOUNTED); 36 | final String cachePath; 37 | if (externalStorageAvailable) { 38 | cachePath = context.getExternalCacheDir().getPath(); 39 | } else { 40 | cachePath = context.getCacheDir().getPath(); 41 | } 42 | 43 | return new File(cachePath + File.separator + CACHE_DIR); 44 | } 45 | 46 | private long getUsableSpace(File path) { 47 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { 48 | return path.getUsableSpace(); 49 | } 50 | final StatFs stats = new StatFs(path.getPath()); 51 | return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks(); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/cache/LruCacheHelper.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.cache; 2 | 3 | import android.graphics.Bitmap; 4 | import android.support.v4.util.LruCache; 5 | 6 | 7 | public class LruCacheHelper { 8 | private LruCache mMemoryCache; 9 | 10 | public LruCacheHelper() { 11 | int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); 12 | int cacheSize = maxMemory / 8; 13 | mMemoryCache = new LruCache(cacheSize) { 14 | @Override 15 | protected int sizeOf(String key, Bitmap bitmap) { 16 | return bitmap.getRowBytes() * bitmap.getHeight() / 1024; 17 | } 18 | }; 19 | } 20 | 21 | public void addBitmapToMemoryCache(String key, Bitmap bitmap) { 22 | if (getBitmapFromMemCache(key) == null) { 23 | mMemoryCache.put(key, bitmap); 24 | } 25 | } 26 | 27 | public Bitmap getBitmapFromMemCache(String key) { 28 | return mMemoryCache.get(key); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/helper/BitmapLoader.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.helper; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.os.Handler; 6 | import android.util.Log; 7 | 8 | import com.jakewharton.disklrucache.DiskLruCache; 9 | import com.othershe.combinebitmap.cache.DiskLruCacheHelper; 10 | import com.othershe.combinebitmap.cache.LruCacheHelper; 11 | 12 | import java.io.BufferedInputStream; 13 | import java.io.BufferedOutputStream; 14 | import java.io.FileDescriptor; 15 | import java.io.FileInputStream; 16 | import java.io.IOException; 17 | import java.io.OutputStream; 18 | import java.net.HttpURLConnection; 19 | import java.net.URL; 20 | import java.util.ArrayList; 21 | import java.util.HashMap; 22 | import java.util.List; 23 | import java.util.Map; 24 | 25 | public class BitmapLoader { 26 | private static String TAG = BitmapLoader.class.getSimpleName(); 27 | private static final int BUFFER_SIZE = 8 * 1024; 28 | 29 | private LruCacheHelper lruCacheHelper; 30 | private DiskLruCache mDiskLruCache; 31 | private CompressHelper compressHelper; 32 | 33 | private volatile static BitmapLoader manager; 34 | 35 | public static BitmapLoader getInstance(Context context) { 36 | if (manager == null) { 37 | synchronized (BitmapLoader.class) { 38 | if (manager == null) { 39 | manager = new BitmapLoader(context); 40 | } 41 | } 42 | } 43 | return manager; 44 | } 45 | 46 | // 存储线程池中的任务 47 | private Map doingTasks; 48 | // 存储暂时不能进入线程池的任务 49 | private Map> undoTasks; 50 | 51 | 52 | private BitmapLoader(Context context) { 53 | mDiskLruCache = new DiskLruCacheHelper(context).mDiskLruCache; 54 | lruCacheHelper = new LruCacheHelper(); 55 | compressHelper = CompressHelper.getInstance(); 56 | 57 | doingTasks = new HashMap<>(); 58 | undoTasks = new HashMap<>(); 59 | } 60 | 61 | 62 | public void asyncLoad(final int index, final String url, final int reqWidth, final int reqHeight, final Handler handler) { 63 | Runnable task = new Runnable() { 64 | @Override 65 | public void run() { 66 | Bitmap bitmap = loadBitmap(url, reqWidth, reqHeight); 67 | if (bitmap != null) { 68 | handler.obtainMessage(1, index, -1, bitmap).sendToTarget(); 69 | } else { 70 | handler.obtainMessage(2, index, -1, null).sendToTarget(); 71 | } 72 | } 73 | }; 74 | 75 | if (collectUndoTasks(url, task)) { 76 | return; 77 | } 78 | 79 | ThreadPool.getInstance().execute(task); 80 | } 81 | 82 | private Bitmap loadBitmap(String url, int reqWidth, int reqHeight) { 83 | 84 | // 尝试从内存缓存中读取 85 | String key = Utils.hashKeyFormUrl(url); 86 | Bitmap bitmap = lruCacheHelper.getBitmapFromMemCache(key); 87 | if (bitmap != null) { 88 | Log.e(TAG, "load from memory:" + url); 89 | return bitmap; 90 | } 91 | 92 | try { 93 | // 尝试从磁盘缓存中读取 94 | bitmap = loadBitmapFromDiskCache(url, reqWidth, reqHeight); 95 | if (bitmap != null) { 96 | Log.e(TAG, "load from disk:" + url); 97 | return bitmap; 98 | } 99 | // 尝试下载 100 | bitmap = loadBitmapFromHttp(url, reqWidth, reqHeight); 101 | if (bitmap != null) { 102 | Log.e(TAG, "load from http:" + url); 103 | return bitmap; 104 | } 105 | } catch (IOException e) { 106 | e.printStackTrace(); 107 | } 108 | 109 | return null; 110 | } 111 | 112 | private Bitmap loadBitmapFromHttp(String url, int reqWidth, int reqHeight) 113 | throws IOException { 114 | String key = Utils.hashKeyFormUrl(url); 115 | DiskLruCache.Editor editor = mDiskLruCache.edit(key); 116 | if (editor != null) { 117 | OutputStream outputStream = editor.newOutputStream(0); 118 | if (downloadUrlToStream(url, outputStream)) { 119 | editor.commit(); 120 | } else { 121 | editor.abort(); 122 | } 123 | mDiskLruCache.flush(); 124 | 125 | executeUndoTasks(url); 126 | } 127 | return loadBitmapFromDiskCache(url, reqWidth, reqHeight); 128 | } 129 | 130 | private boolean downloadUrlToStream(String urlString, 131 | OutputStream outputStream) { 132 | HttpURLConnection urlConnection = null; 133 | BufferedOutputStream out = null; 134 | BufferedInputStream in = null; 135 | 136 | try { 137 | final URL url = new URL(urlString); 138 | urlConnection = (HttpURLConnection) url.openConnection(); 139 | in = new BufferedInputStream(urlConnection.getInputStream(), BUFFER_SIZE); 140 | out = new BufferedOutputStream(outputStream, BUFFER_SIZE); 141 | 142 | int b; 143 | while ((b = in.read()) != -1) { 144 | out.write(b); 145 | } 146 | return true; 147 | } catch (IOException e) { 148 | Log.e(TAG, "downloadBitmap failed." + e); 149 | } finally { 150 | if (urlConnection != null) { 151 | urlConnection.disconnect(); 152 | } 153 | Utils.close(out); 154 | Utils.close(in); 155 | } 156 | return false; 157 | } 158 | 159 | private Bitmap loadBitmapFromDiskCache(String url, int reqWidth, 160 | int reqHeight) throws IOException { 161 | Bitmap bitmap = null; 162 | String key = Utils.hashKeyFormUrl(url); 163 | DiskLruCache.Snapshot snapShot = mDiskLruCache.get(key); 164 | if (snapShot != null) { 165 | FileInputStream fileInputStream = (FileInputStream) snapShot.getInputStream(0); 166 | FileDescriptor fileDescriptor = fileInputStream.getFD(); 167 | bitmap = compressHelper.compressDescriptor(fileDescriptor, reqWidth, reqHeight); 168 | if (bitmap != null) { 169 | lruCacheHelper.addBitmapToMemoryCache(key, bitmap); 170 | } 171 | } 172 | 173 | return bitmap; 174 | } 175 | 176 | private boolean collectUndoTasks(String url, Runnable task) { 177 | String key = Utils.hashKeyFormUrl(url); 178 | 179 | if (lruCacheHelper.getBitmapFromMemCache(key) != null) { 180 | return false; 181 | } 182 | 183 | DiskLruCache.Snapshot snapShot = null; 184 | try { 185 | snapShot = mDiskLruCache.get(key); 186 | } catch (IOException e) { 187 | e.printStackTrace(); 188 | } 189 | 190 | if (snapShot != null) { 191 | return false; 192 | } 193 | 194 | // 如果当前url下载操作过程的磁盘缓存的Editor未结束,又来了一个新的url,则不能正常生成新Editor 195 | // 则将新url对应的任务先保存起来 196 | if (doingTasks.containsKey(key)) { 197 | if (undoTasks.containsKey(key)) { 198 | List tasks = undoTasks.get(key); 199 | tasks.add(task); 200 | undoTasks.put(key, tasks); 201 | } else { 202 | List tasks = new ArrayList<>(); 203 | tasks.add(task); 204 | undoTasks.put(key, tasks); 205 | } 206 | return true; 207 | } 208 | 209 | doingTasks.put(key, task); 210 | return false; 211 | } 212 | 213 | private void executeUndoTasks(String url) { 214 | String key = Utils.hashKeyFormUrl(url); 215 | // 检查undoTasks中是否有要执行的任务 216 | if (undoTasks.containsKey(key)) { 217 | for (Runnable task : undoTasks.get(key)) { 218 | ThreadPool.getInstance().execute(task); 219 | } 220 | undoTasks.remove(key); 221 | } 222 | // 从doingTasks中移除已经执行完的任务 223 | doingTasks.remove(key); 224 | } 225 | } 226 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/helper/Builder.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.helper; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.content.Context; 5 | import android.graphics.Bitmap; 6 | import android.graphics.Point; 7 | import android.graphics.Region; 8 | import android.support.annotation.ColorInt; 9 | import android.view.MotionEvent; 10 | import android.view.View; 11 | import android.widget.ImageView; 12 | 13 | import com.othershe.combinebitmap.R; 14 | import com.othershe.combinebitmap.layout.DingLayoutManager; 15 | import com.othershe.combinebitmap.layout.ILayoutManager; 16 | import com.othershe.combinebitmap.layout.WechatLayoutManager; 17 | import com.othershe.combinebitmap.listener.OnProgressListener; 18 | import com.othershe.combinebitmap.listener.OnSubItemClickListener; 19 | import com.othershe.combinebitmap.region.DingRegionManager; 20 | import com.othershe.combinebitmap.region.IRegionManager; 21 | import com.othershe.combinebitmap.region.WechatRegionManager; 22 | 23 | public class Builder { 24 | public Context context; 25 | public ImageView imageView; 26 | public int size; // 最终生成bitmap的尺寸 27 | public int gap; // 每个小bitmap之间的距离 28 | public int gapColor; // 间距的颜色 29 | public int placeholder = R.drawable.defalut_placeholder; // 获取图片失败时的默认图片 30 | public int count; // 要加载的资源数量 31 | public int subSize; // 单个bitmap的尺寸 32 | 33 | public ILayoutManager layoutManager; // bitmap的组合样式 34 | 35 | public Region[] regions; 36 | public OnSubItemClickListener subItemClickListener; // 单个bitmap点击事件回调 37 | 38 | public OnProgressListener progressListener; // 最终的组合bitmap回调接口 39 | 40 | public Bitmap[] bitmaps; 41 | public int[] resourceIds; 42 | public String[] urls; 43 | 44 | 45 | public Builder(Context context) { 46 | this.context = context; 47 | } 48 | 49 | public Builder setImageView(ImageView imageView) { 50 | this.imageView = imageView; 51 | return this; 52 | } 53 | 54 | public Builder setSize(int size) { 55 | this.size = Utils.dp2px(context, size); 56 | return this; 57 | } 58 | 59 | public Builder setGap(int gap) { 60 | this.gap = Utils.dp2px(context, gap); 61 | return this; 62 | } 63 | 64 | public Builder setGapColor(@ColorInt int gapColor) { 65 | this.gapColor = gapColor; 66 | return this; 67 | } 68 | 69 | public Builder setPlaceholder(int placeholder) { 70 | this.placeholder = placeholder; 71 | return this; 72 | } 73 | 74 | public Builder setLayoutManager(ILayoutManager layoutManager) { 75 | this.layoutManager = layoutManager; 76 | return this; 77 | } 78 | 79 | public Builder setOnProgressListener(OnProgressListener progressListener) { 80 | this.progressListener = progressListener; 81 | return this; 82 | } 83 | 84 | public Builder setOnSubItemClickListener(OnSubItemClickListener subItemClickListener) { 85 | this.subItemClickListener = subItemClickListener; 86 | return this; 87 | } 88 | 89 | public Builder setBitmaps(Bitmap... bitmaps) { 90 | this.bitmaps = bitmaps; 91 | this.count = bitmaps.length; 92 | return this; 93 | } 94 | 95 | public Builder setUrls(String... urls) { 96 | this.urls = urls; 97 | this.count = urls.length; 98 | return this; 99 | } 100 | 101 | public Builder setResourceIds(int... resourceIds) { 102 | this.resourceIds = resourceIds; 103 | this.count = resourceIds.length; 104 | return this; 105 | } 106 | 107 | public void build() { 108 | subSize = getSubSize(size, gap, layoutManager, count); 109 | initRegions(); 110 | CombineHelper.init().load(this); 111 | } 112 | 113 | /** 114 | * 根据最终生成bitmap的尺寸,计算单个bitmap尺寸 115 | * 116 | * @param size 117 | * @param gap 118 | * @param layoutManager 119 | * @param count 120 | * @return 121 | */ 122 | private int getSubSize(int size, int gap, ILayoutManager layoutManager, int count) { 123 | int subSize = 0; 124 | if (layoutManager instanceof DingLayoutManager) { 125 | subSize = size; 126 | } else if (layoutManager instanceof WechatLayoutManager) { 127 | if (count < 2) { 128 | subSize = size; 129 | } else if (count < 5) { 130 | subSize = (size - 3 * gap) / 2; 131 | } else if (count < 10) { 132 | subSize = (size - 4 * gap) / 3; 133 | } 134 | } else { 135 | throw new IllegalArgumentException("Must use DingLayoutManager or WechatRegionManager!"); 136 | } 137 | return subSize; 138 | } 139 | 140 | /** 141 | * 初始化RegionManager 142 | */ 143 | private void initRegions() { 144 | if (subItemClickListener == null || imageView == null) { 145 | return; 146 | } 147 | 148 | IRegionManager regionManager; 149 | 150 | if (layoutManager instanceof DingLayoutManager) { 151 | regionManager = new DingRegionManager(); 152 | } else if (layoutManager instanceof WechatLayoutManager) { 153 | regionManager = new WechatRegionManager(); 154 | } else { 155 | throw new IllegalArgumentException("Must use DingLayoutManager or WechatRegionManager!"); 156 | } 157 | 158 | regions = regionManager.calculateRegion(size, subSize, gap, count); 159 | 160 | imageView.setOnTouchListener(new View.OnTouchListener() { 161 | int initIndex = -1; 162 | int currentIndex = -1; 163 | Point point = new Point(); 164 | 165 | @Override 166 | public boolean onTouch(View v, MotionEvent event) { 167 | point.set((int) event.getX(), (int) event.getY()); 168 | switch (event.getAction()) { 169 | case MotionEvent.ACTION_DOWN: 170 | initIndex = getRegionIndex(point.x, point.y); 171 | currentIndex = initIndex; 172 | break; 173 | case MotionEvent.ACTION_MOVE: 174 | currentIndex = getRegionIndex(point.x, point.y); 175 | break; 176 | case MotionEvent.ACTION_UP: 177 | currentIndex = getRegionIndex(point.x, point.y); 178 | if (currentIndex != -1 && currentIndex == initIndex) { 179 | subItemClickListener.onSubItemClick(currentIndex); 180 | } 181 | break; 182 | case MotionEvent.ACTION_CANCEL: 183 | initIndex = currentIndex = -1; 184 | break; 185 | } 186 | return true; 187 | } 188 | }); 189 | } 190 | 191 | /** 192 | * 根据触摸点计算对应的Region索引 193 | * 194 | * @param x 195 | * @param y 196 | * @return 197 | */ 198 | private int getRegionIndex(int x, int y) { 199 | for (int i = 0; i < regions.length; i++) { 200 | if (regions[i].contains(x, y)) { 201 | return i; 202 | } 203 | } 204 | return -1; 205 | } 206 | 207 | } 208 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/helper/CombineHelper.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.helper; 2 | 3 | import android.graphics.Bitmap; 4 | 5 | import com.othershe.combinebitmap.listener.OnHandlerListener; 6 | 7 | 8 | public class CombineHelper { 9 | public static CombineHelper init() { 10 | return CombineHelper.SingletonHolder.instance; 11 | } 12 | 13 | 14 | private CombineHelper(){ 15 | 16 | } 17 | 18 | private static class SingletonHolder { 19 | private static final CombineHelper instance = new CombineHelper(); 20 | } 21 | 22 | /** 23 | * 通过url加载 24 | * 25 | * @param builder 26 | */ 27 | private void loadByUrls(final Builder builder) { 28 | int subSize = builder.subSize; 29 | Bitmap defaultBitmap = null; 30 | if (builder.placeholder != 0) { 31 | defaultBitmap = CompressHelper.getInstance() 32 | .compressResource(builder.context.getResources(), builder.placeholder, subSize, subSize); 33 | } 34 | ProgressHandler handler = new ProgressHandler(defaultBitmap, builder.count, new OnHandlerListener() { 35 | @Override 36 | public void onComplete(Bitmap[] bitmaps) { 37 | setBitmap(builder, bitmaps); 38 | } 39 | }); 40 | for (int i = 0; i < builder.count; i++) { 41 | BitmapLoader.getInstance(builder.context).asyncLoad(i, builder.urls[i], subSize, subSize, handler); 42 | } 43 | } 44 | 45 | /** 46 | * 通过图片的资源id、bitmap加载 47 | * 48 | * @param builder 49 | */ 50 | private void loadByResBitmaps(Builder builder) { 51 | int subSize = builder.subSize; 52 | Bitmap[] compressedBitmaps = new Bitmap[builder.count]; 53 | for (int i = 0; i < builder.count; i++) { 54 | if (builder.resourceIds != null) { 55 | compressedBitmaps[i] = CompressHelper.getInstance() 56 | .compressResource(builder.context.getResources(), builder.resourceIds[i], subSize, subSize); 57 | } else if (builder.bitmaps != null) { 58 | compressedBitmaps[i] = CompressHelper.getInstance() 59 | .compressResource(builder.bitmaps[i], subSize, subSize); 60 | } 61 | } 62 | setBitmap(builder, compressedBitmaps); 63 | } 64 | 65 | public void load(Builder builder) { 66 | if (builder.progressListener != null) { 67 | builder.progressListener.onStart(); 68 | } 69 | 70 | if (builder.urls != null) { 71 | loadByUrls(builder); 72 | } else { 73 | loadByResBitmaps(builder); 74 | } 75 | } 76 | 77 | private void setBitmap(final Builder b, Bitmap[] bitmaps) { 78 | Bitmap result = b.layoutManager.combineBitmap(b.size, b.subSize, b.gap, b.gapColor, bitmaps); 79 | 80 | // 返回最终的组合Bitmap 81 | if (b.progressListener != null) { 82 | b.progressListener.onComplete(result); 83 | } 84 | 85 | // 给ImageView设置最终的组合Bitmap 86 | if (b.imageView != null) { 87 | b.imageView.setImageBitmap(result); 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/helper/CompressHelper.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.helper; 2 | 3 | 4 | import android.content.res.Resources; 5 | import android.graphics.Bitmap; 6 | import android.graphics.BitmapFactory; 7 | import android.graphics.Matrix; 8 | 9 | import java.io.FileDescriptor; 10 | 11 | public class CompressHelper { 12 | 13 | private CompressHelper() { 14 | } 15 | 16 | public static CompressHelper getInstance() { 17 | return CompressHelper.SingletonHolder.instance; 18 | } 19 | 20 | private static class SingletonHolder { 21 | private static final CompressHelper instance = new CompressHelper(); 22 | } 23 | 24 | public Bitmap compressResource(Resources res, int resId, int reqWidth, int reqHeight) { 25 | BitmapFactory.Options options = new BitmapFactory.Options(); 26 | options.inJustDecodeBounds = true; 27 | BitmapFactory.decodeResource(res, resId, options); 28 | options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 29 | options.inJustDecodeBounds = false; 30 | return BitmapFactory.decodeResource(res, resId, options); 31 | } 32 | 33 | public Bitmap compressDescriptor(FileDescriptor fd, int reqWidth, int reqHeight) { 34 | BitmapFactory.Options options = new BitmapFactory.Options(); 35 | options.inJustDecodeBounds = true; 36 | BitmapFactory.decodeFileDescriptor(fd, null, options); 37 | options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 38 | options.inJustDecodeBounds = false; 39 | return BitmapFactory.decodeFileDescriptor(fd, null, options); 40 | } 41 | 42 | public Bitmap compressResource(Bitmap bitmap, int reqWidth, int reqHeight) { 43 | 44 | int width = bitmap.getWidth(); 45 | int height = bitmap.getHeight(); 46 | float scaleWidth = ((float) reqWidth) / width; 47 | float scaleHeight = ((float) reqHeight) / height; 48 | Matrix matrix = new Matrix(); 49 | matrix.postScale(scaleWidth, scaleHeight); 50 | return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); 51 | } 52 | 53 | private int calculateInSampleSize(BitmapFactory.Options options, 54 | int reqWidth, int reqHeight) { 55 | if (reqWidth == 0 || reqHeight == 0) { 56 | return 1; 57 | } 58 | final int height = options.outHeight; 59 | final int width = options.outWidth; 60 | int inSampleSize = 1; 61 | 62 | if (height > reqHeight || width > reqWidth) { 63 | final int halfHeight = height / 2; 64 | final int halfWidth = width / 2; 65 | 66 | while ((halfHeight / inSampleSize) >= reqHeight 67 | && (halfWidth / inSampleSize) >= reqWidth) { 68 | inSampleSize *= 2; 69 | } 70 | } 71 | return inSampleSize; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/helper/ProgressHandler.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.helper; 2 | 3 | import android.graphics.Bitmap; 4 | import android.os.Handler; 5 | import android.os.Message; 6 | 7 | import com.othershe.combinebitmap.listener.OnHandlerListener; 8 | 9 | public class ProgressHandler extends Handler { 10 | private int i = 0; 11 | 12 | private Bitmap[] bitmaps; 13 | 14 | private Bitmap defaultBitmap; 15 | private OnHandlerListener listener; 16 | 17 | public ProgressHandler(Bitmap defaultBitmap, int count, OnHandlerListener listener) { 18 | this.defaultBitmap = defaultBitmap; 19 | this.bitmaps = new Bitmap[count]; 20 | this.listener = listener; 21 | } 22 | 23 | @Override 24 | public void handleMessage(Message msg) { 25 | super.handleMessage(msg); 26 | switch (msg.what) { 27 | case 1://成功 28 | bitmaps[msg.arg1] = (Bitmap) msg.obj; 29 | break; 30 | case 2://失败 31 | bitmaps[msg.arg1] = defaultBitmap; 32 | break; 33 | } 34 | i++; 35 | if (i == bitmaps.length && listener != null) { 36 | listener.onComplete(bitmaps); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/helper/ThreadPool.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.helper; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import java.util.concurrent.LinkedBlockingDeque; 6 | import java.util.concurrent.ThreadFactory; 7 | import java.util.concurrent.ThreadPoolExecutor; 8 | import java.util.concurrent.TimeUnit; 9 | import java.util.concurrent.atomic.AtomicInteger; 10 | 11 | /** 12 | * 自定义的下载线程池 13 | */ 14 | public class ThreadPool { 15 | //CPU核心数 16 | private int CPU_COUNT = Runtime.getRuntime().availableProcessors(); 17 | //可同时下载的任务数(核心线程数) 18 | private int CORE_POOL_SIZE = CPU_COUNT; 19 | //缓存队列的大小(最大线程数) 20 | private int MAX_POOL_SIZE = 2 * CPU_COUNT + 1; 21 | //非核心线程闲置的超时时间(秒),如果超时则会被回收 22 | private long KEEP_ALIVE = 10L; 23 | 24 | private ThreadPoolExecutor THREAD_POOL_EXECUTOR; 25 | 26 | private ThreadFactory sThreadFactory = new ThreadFactory() { 27 | private final AtomicInteger mCount = new AtomicInteger(); 28 | 29 | @Override 30 | public Thread newThread(@NonNull Runnable runnable) { 31 | return new Thread(runnable, "download_task#" + mCount.getAndIncrement()); 32 | } 33 | }; 34 | 35 | private ThreadPool() { 36 | } 37 | 38 | public static ThreadPool getInstance() { 39 | return SingletonHolder.instance; 40 | } 41 | 42 | private static class SingletonHolder { 43 | private static final ThreadPool instance = new ThreadPool(); 44 | } 45 | 46 | private ThreadPoolExecutor getThreadPoolExecutor() { 47 | if (THREAD_POOL_EXECUTOR == null) { 48 | THREAD_POOL_EXECUTOR = new ThreadPoolExecutor( 49 | CORE_POOL_SIZE, 50 | MAX_POOL_SIZE, 51 | KEEP_ALIVE, 52 | TimeUnit.SECONDS, 53 | new LinkedBlockingDeque(), 54 | sThreadFactory); 55 | } 56 | return THREAD_POOL_EXECUTOR; 57 | } 58 | 59 | public void execute(Runnable command) { 60 | getThreadPoolExecutor().execute(command); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/helper/Utils.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.helper; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import java.io.Closeable; 7 | import java.io.IOException; 8 | import java.security.MessageDigest; 9 | import java.security.NoSuchAlgorithmException; 10 | 11 | public class Utils { 12 | public static int dp2px(Context context, float dipValue) { 13 | final float scale = context.getResources().getDisplayMetrics().density; 14 | return (int) (dipValue * scale + 0.5f); 15 | } 16 | 17 | public static void close(Closeable closeable) { 18 | try { 19 | if (closeable != null) { 20 | closeable.close(); 21 | } 22 | } catch (IOException e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | 27 | 28 | public static String hashKeyFormUrl(String url) { 29 | String cacheKey; 30 | try { 31 | final MessageDigest mDigest = MessageDigest.getInstance("MD5"); 32 | mDigest.update(url.getBytes()); 33 | cacheKey = bytesToHexString(mDigest.digest()); 34 | } catch (NoSuchAlgorithmException e) { 35 | cacheKey = String.valueOf(url.hashCode()); 36 | } 37 | return cacheKey; 38 | } 39 | 40 | private static String bytesToHexString(byte[] bytes) { 41 | StringBuilder sb = new StringBuilder(); 42 | for (int i = 0; i < bytes.length; i++) { 43 | String hex = Integer.toHexString(0xFF & bytes[i]); 44 | if (hex.length() == 1) { 45 | sb.append('0'); 46 | } 47 | sb.append(hex); 48 | } 49 | return sb.toString(); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/layout/DingLayoutManager.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.layout; 2 | 3 | import android.graphics.Bitmap; 4 | import android.graphics.Canvas; 5 | import android.graphics.Color; 6 | 7 | public class DingLayoutManager implements ILayoutManager { 8 | @Override 9 | public Bitmap combineBitmap(int size, int subSize, int gap, int gapColor, Bitmap[] bitmaps) { 10 | Bitmap result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); 11 | Canvas canvas = new Canvas(result); 12 | if (gapColor == 0) { 13 | gapColor = Color.WHITE; 14 | } 15 | canvas.drawColor(gapColor); 16 | 17 | int count = bitmaps.length; 18 | Bitmap subBitmap; 19 | 20 | int[][] dxy = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; 21 | 22 | for (int i = 0; i < count; i++) { 23 | if (bitmaps[i] == null) { 24 | continue; 25 | } 26 | subBitmap = Bitmap.createScaledBitmap(bitmaps[i], size, size, true); 27 | if (count == 2 || (count == 3 && i == 0)) { 28 | subBitmap = Bitmap.createBitmap(subBitmap, (size + gap) / 4, 0, (size - gap) / 2, size); 29 | } else if ((count == 3 && (i == 1 || i == 2)) || count == 4) { 30 | subBitmap = Bitmap.createBitmap(subBitmap, (size + gap) / 4, (size + gap) / 4, (size - gap) / 2, (size - gap) / 2); 31 | } 32 | 33 | int dx = dxy[i][0]; 34 | int dy = dxy[i][1]; 35 | 36 | canvas.drawBitmap(subBitmap, dx * (size + gap) / 2.0f, dy * (size + gap) / 2.0f, null); 37 | } 38 | return result; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/layout/ILayoutManager.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.layout; 2 | 3 | import android.graphics.Bitmap; 4 | 5 | public interface ILayoutManager { 6 | Bitmap combineBitmap(int size, int subSize, int gap, int gapColor, Bitmap[] bitmaps); 7 | } 8 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/layout/WechatLayoutManager.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.layout; 2 | 3 | import android.graphics.Bitmap; 4 | import android.graphics.Canvas; 5 | import android.graphics.Color; 6 | 7 | public class WechatLayoutManager implements ILayoutManager { 8 | @Override 9 | public Bitmap combineBitmap(int size, int subSize, int gap, int gapColor, Bitmap[] bitmaps) { 10 | Bitmap result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); 11 | Canvas canvas = new Canvas(result); 12 | if (gapColor == 0) { 13 | gapColor = Color.WHITE; 14 | } 15 | canvas.drawColor(gapColor); 16 | 17 | int count = bitmaps.length; 18 | Bitmap subBitmap; 19 | 20 | for (int i = 0; i < count; i++) { 21 | if (bitmaps[i] == null) { 22 | continue; 23 | } 24 | subBitmap = Bitmap.createScaledBitmap(bitmaps[i], subSize, subSize, true); 25 | 26 | float x = 0; 27 | float y = 0; 28 | 29 | if (count == 2) { 30 | x = gap + i * (subSize + gap); 31 | y = (size - subSize) / 2.0f; 32 | } else if (count == 3) { 33 | if (i == 0) { 34 | x = (size - subSize) / 2.0f; 35 | y = gap; 36 | } else { 37 | x = gap + (i - 1) * (subSize + gap); 38 | y = subSize + 2 * gap; 39 | } 40 | } else if (count == 4) { 41 | x = gap + (i % 2) * (subSize + gap); 42 | if (i < 2) { 43 | y = gap; 44 | } else { 45 | y = subSize + 2 * gap; 46 | } 47 | } else if (count == 5) { 48 | if (i == 0) { 49 | x = y = (size - 2 * subSize - gap) / 2.0f; 50 | } else if (i == 1) { 51 | x = (size + gap) / 2.0f; 52 | y = (size - 2 * subSize - gap) / 2.0f; 53 | } else if (i > 1) { 54 | x = gap + (i - 2) * (subSize + gap); 55 | y = (size + gap) / 2.0f; 56 | } 57 | } else if (count == 6) { 58 | x = gap + (i % 3) * (subSize + gap); 59 | if (i < 3) { 60 | y = (size - 2 * subSize - gap) / 2.0f; 61 | } else { 62 | y = (size + gap) / 2.0f; 63 | } 64 | } else if (count == 7) { 65 | if (i == 0) { 66 | x = (size - subSize) / 2.0f; 67 | y = gap; 68 | } else if (i < 4) { 69 | x = gap + (i - 1) * (subSize + gap); 70 | y = subSize + 2 * gap; 71 | } else { 72 | x = gap + (i - 4) * (subSize + gap); 73 | y = gap + 2 * (subSize + gap); 74 | } 75 | } else if (count == 8) { 76 | if (i == 0) { 77 | x = (size - 2 * subSize - gap) / 2.0f; 78 | y = gap; 79 | } else if (i == 1) { 80 | x = (size + gap) / 2.0f; 81 | y = gap; 82 | } else if (i < 5) { 83 | x = gap + (i - 2) * (subSize + gap); 84 | y = subSize + 2 * gap; 85 | } else { 86 | x = gap + (i - 5) * (subSize + gap); 87 | y = gap + 2 * (subSize + gap); 88 | } 89 | } else if (count == 9) { 90 | x = gap + (i % 3) * (subSize + gap); 91 | if (i < 3) { 92 | y = gap; 93 | } else if (i < 6) { 94 | y = subSize + 2 * gap; 95 | } else { 96 | y = gap + 2 * (subSize + gap); 97 | } 98 | } 99 | 100 | canvas.drawBitmap(subBitmap, x, y, null); 101 | } 102 | return result; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/listener/OnHandlerListener.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.listener; 2 | 3 | import android.graphics.Bitmap; 4 | 5 | public interface OnHandlerListener { 6 | void onComplete(Bitmap[] bitmaps); 7 | } 8 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/listener/OnProgressListener.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.listener; 2 | 3 | import android.graphics.Bitmap; 4 | 5 | public interface OnProgressListener { 6 | void onStart(); 7 | 8 | void onComplete(Bitmap bitmap); 9 | } 10 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/listener/OnSubItemClickListener.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.listener; 2 | 3 | public interface OnSubItemClickListener { 4 | // index和传入的图片资源数据的数组下标一一对应 5 | void onSubItemClick(int index); 6 | } 7 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/region/DingRegionManager.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.region; 2 | 3 | import android.graphics.Path; 4 | import android.graphics.Region; 5 | 6 | public class DingRegionManager implements IRegionManager { 7 | @Override 8 | public Region[] calculateRegion(int size, int subSize, int gap, int count) { 9 | Region[] regions = new Region[count]; 10 | Region globalRegion = new Region(0, 0, size, size); 11 | 12 | int[][] dxy = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; 13 | 14 | for (int i = 0; i < count; i++) { 15 | float width = size; 16 | float height = size; 17 | 18 | if (count == 2 || (count == 3 && i == 0)) { 19 | width = (size - gap) / 2; 20 | height = size; 21 | } else if ((count == 3 && (i == 1 || i == 2)) || count == 4) { 22 | width = (size - gap) / 2; 23 | height = (size - gap) / 2; 24 | } 25 | 26 | int dx = dxy[i][0]; 27 | int dy = dxy[i][1]; 28 | 29 | float left = dx * (size + gap) / 2.0f; 30 | float top = dy * (size + gap) / 2.0f; 31 | float right = left + width; 32 | float bottom = top + height; 33 | Path path = new Path(); 34 | path.addRect(left, top, right, bottom, Path.Direction.CW); 35 | 36 | Region region = new Region(); 37 | region.setPath(path, globalRegion); 38 | 39 | regions[i] = region; 40 | } 41 | return regions; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/region/IRegionManager.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.region; 2 | 3 | import android.graphics.Region; 4 | 5 | public interface IRegionManager { 6 | Region[] calculateRegion(int size, int subSize, int gap, int count); 7 | } 8 | -------------------------------------------------------------------------------- /combinebitmap/src/main/java/com/othershe/combinebitmap/region/WechatRegionManager.java: -------------------------------------------------------------------------------- 1 | package com.othershe.combinebitmap.region; 2 | 3 | import android.graphics.Path; 4 | import android.graphics.Region; 5 | 6 | public class WechatRegionManager implements IRegionManager { 7 | 8 | @Override 9 | public Region[] calculateRegion(int size, int subSize, int gap, int count) { 10 | Region[] regions = new Region[count]; 11 | Region globalRegion = new Region(0, 0, size, size); 12 | 13 | for (int i = 0; i < count; i++) { 14 | 15 | float x = 0; 16 | float y = 0; 17 | 18 | if (count == 2) { 19 | x = gap + i * (subSize + gap); 20 | y = (size - subSize) / 2.0f; 21 | } else if (count == 3) { 22 | if (i == 0) { 23 | x = (size - subSize) / 2.0f; 24 | y = gap; 25 | } else { 26 | x = gap + (i - 1) * (subSize + gap); 27 | y = subSize + 2 * gap; 28 | } 29 | } else if (count == 4) { 30 | x = gap + (i % 2) * (subSize + gap); 31 | if (i < 2) { 32 | y = gap; 33 | } else { 34 | y = subSize + 2 * gap; 35 | } 36 | } else if (count == 5) { 37 | if (i == 0) { 38 | x = y = (size - 2 * subSize - gap) / 2.0f; 39 | } else if (i == 1) { 40 | x = (size + gap) / 2.0f; 41 | y = (size - 2 * subSize - gap) / 2.0f; 42 | } else if (i > 1) { 43 | x = gap + (i - 2) * (subSize + gap); 44 | y = (size + gap) / 2.0f; 45 | } 46 | } else if (count == 6) { 47 | x = gap + (i % 3) * (subSize + gap); 48 | if (i < 3) { 49 | y = (size - 2 * subSize - gap) / 2.0f; 50 | } else { 51 | y = (size + gap) / 2.0f; 52 | } 53 | } else if (count == 7) { 54 | if (i == 0) { 55 | x = (size - subSize) / 2.0f; 56 | y = gap; 57 | } else if (i < 4) { 58 | x = gap + (i - 1) * (subSize + gap); 59 | y = subSize + 2 * gap; 60 | } else { 61 | x = gap + (i - 4) * (subSize + gap); 62 | y = gap + 2 * (subSize + gap); 63 | } 64 | } else if (count == 8) { 65 | if (i == 0) { 66 | x = (size - 2 * subSize - gap) / 2.0f; 67 | y = gap; 68 | } else if (i == 1) { 69 | x = (size + gap) / 2.0f; 70 | y = gap; 71 | } else if (i < 5) { 72 | x = gap + (i - 2) * (subSize + gap); 73 | y = subSize + 2 * gap; 74 | } else { 75 | x = gap + (i - 5) * (subSize + gap); 76 | y = gap + 2 * (subSize + gap); 77 | } 78 | } else if (count == 9) { 79 | x = gap + (i % 3) * (subSize + gap); 80 | if (i < 3) { 81 | y = gap; 82 | } else if (i < 6) { 83 | y = subSize + 2 * gap; 84 | } else { 85 | y = gap + 2 * (subSize + gap); 86 | } 87 | } 88 | 89 | float left = x; 90 | float top = y; 91 | float right = left + subSize; 92 | float bottom = top + subSize; 93 | Path path = new Path(); 94 | path.addRect(left, top, right, bottom, Path.Direction.CW); 95 | 96 | Region region = new Region(); 97 | region.setPath(path, globalRegion); 98 | 99 | regions[i] = region; 100 | } 101 | return regions; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /combinebitmap/src/main/res/drawable/defalut_placeholder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/combinebitmap/src/main/res/drawable/defalut_placeholder.png -------------------------------------------------------------------------------- /combinebitmap/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | combinebitmap 3 | 4 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Jun 19 14:03:43 CST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /images/d1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/images/d1.PNG -------------------------------------------------------------------------------- /images/d2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/images/d2.PNG -------------------------------------------------------------------------------- /images/d3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/images/d3.PNG -------------------------------------------------------------------------------- /images/w1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/images/w1.PNG -------------------------------------------------------------------------------- /images/w2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/images/w2.PNG -------------------------------------------------------------------------------- /images/w3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/images/w3.PNG -------------------------------------------------------------------------------- /images/w4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/images/w4.PNG -------------------------------------------------------------------------------- /images/w5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/images/w5.PNG -------------------------------------------------------------------------------- /images/w6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/images/w6.PNG -------------------------------------------------------------------------------- /images/w7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/images/w7.PNG -------------------------------------------------------------------------------- /images/w8.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shehuan/CombineBitmap/7dd6dcb8e520fddad83246cd782a433ae67c774e/images/w8.PNG -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':combinebitmap' 2 | --------------------------------------------------------------------------------