├── .gitignore ├── LICENSE ├── README.md ├── android ├── build.gradle.kts └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── lt │ └── android │ └── MainActivity.kt ├── build.gradle.kts ├── buildSrc ├── build.gradle.kts └── src │ └── main │ └── kotlin │ └── Versions.kt ├── common ├── build.gradle.kts └── src │ ├── androidMain │ ├── AndroidManifest.xml │ └── kotlin │ │ └── com │ │ └── lt │ │ └── common │ │ └── platform.kt │ ├── commonMain │ └── kotlin │ │ └── com │ │ └── lt │ │ └── common │ │ ├── App.kt │ │ ├── TestLazyList.kt │ │ └── platform.kt │ └── desktopMain │ └── kotlin │ └── com │ └── lt │ └── common │ ├── DesktopApp.kt │ └── platform.kt ├── desktop ├── build.gradle.kts └── src │ └── jvmMain │ ├── kotlin │ └── Main.kt │ └── resources │ └── drawable-xxhdpi │ ├── img_a.jpeg │ └── load_error.jpeg ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── jitpack.yml ├── load-the-image ├── .gitignore ├── build.gradle.kts └── src │ └── main │ └── java │ └── com │ └── lt │ └── load_the_image │ ├── LoadTheImageManager.kt │ ├── RememberImagePainter.kt │ ├── cache │ ├── ImageCache.kt │ ├── ImageFileCache.kt │ ├── ImageLruMemoryCache.kt │ └── NoImageCache.kt │ ├── loader │ ├── LoadTheImage.kt │ ├── http │ │ ├── HttpLoader.kt │ │ ├── HttpURLConnectionLoader.kt │ │ └── OkHttpLoader.kt │ └── image │ │ ├── BitmapLoadTheImage.kt │ │ ├── ByteArrayLoadTheImage.kt │ │ ├── FileLoadTheImage.kt │ │ ├── HttpLoadTheImage.kt │ │ ├── ImageLoadTheImage.kt │ │ ├── InputStreamLoadTheImage.kt │ │ └── ResourcesLoadTheImage.kt │ ├── painter │ ├── AsyncImagePainter.kt │ ├── DefaultPainterCreator.kt │ ├── EmptyImagePainter.kt │ └── PainterCreator.kt │ └── util │ ├── MD5.kt │ └── PrintException.kt ├── md_resource ├── custom_LoadTheImage.png ├── example.png ├── formats.png ├── resource_location.png └── use_placeholder.png └── settings.gradle.kts /.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 | */*.iml 38 | .idea/ 39 | .idea/workspace.xml 40 | .idea/tasks.xml 41 | .idea/gradle.xml 42 | .idea/dictionaries 43 | .idea/libraries 44 | *.hprof 45 | 46 | # External native build folder generated in Android Studio 2.2 and later 47 | .externalNativeBuild 48 | 49 | # Google Services (e.g. APIs or Firebase) 50 | google-services.json 51 | 52 | # Freeline 53 | freeline.py 54 | freeline/ 55 | freeline_project_description.json 56 | 57 | # svn 58 | .svn/ 59 | 60 | # c 61 | .cxx/ 62 | 63 | # 360jiagu 64 | jiagu_libs/ 65 | jiagu_apks_output/ 66 | 67 | # macos 68 | .DS_Store 69 | 70 | # vscode 71 | .vscode/ -------------------------------------------------------------------------------- /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 | [简体中文](https://blog.csdn.net/qq_33505109/article/details/125194044) 2 | 3 | # load-the-image 4 | 5 | load-the-image Apply to compose-jb(desktop), Used to load network and local pictures. 6 | 7 | Example: 8 | 9 | ![Example](md_resource/example.png) 10 | 11 |

Mode of use

12 | 13 | Use the code load image with network, file, resources and more 14 | 15 | ```kotlin 16 | //url="https://img.zcool.cn/community/017e625e57415ea801216518c25819.jpg@1280w_1l_2o_100sh.jpg" 17 | Image(rememberImagePainter(url), "") 18 | ``` 19 | 20 |

Add to your project

21 | 22 | Step 1.Root dir, build.gradle.kts add: 23 | 24 | ```kotlin 25 | buildscript { 26 | repositories { 27 | maven("https://jitpack.io")//this 28 | ... 29 | } 30 | } 31 | 32 | allprojects { 33 | repositories { 34 | maven("https://jitpack.io")//this 35 | ... 36 | } 37 | } 38 | ``` 39 | 40 | Step 2.Your compose-desktop dir, build.gradle.kts add: 41 | 42 | version = [![](https://jitpack.io/v/ltttttttttttt/load-the-image.svg)](https://jitpack.io/#ltttttttttttt/load-the-image) 43 | 44 | ```kotlin 45 | kotlin { 46 | sourceSets { 47 | val jvmMain by getting { 48 | dependencies { 49 | ... 50 | implementation("com.github.ltttttttttttt:load-the-image:$version")//this 51 | } 52 | } 53 | } 54 | } 55 | ``` 56 | 57 | Step 3.Recommendation: uniformly configure the failure graph path displayed when loading fails 58 | 59 | ```kotlin 60 | fun main() { 61 | LoadTheImageManager.defaultErrorImagePath = "drawable-xxhdpi/load_error.jpeg"//this 62 | application { 63 | Window(onCloseRequest = ::exitApplication) { 64 | MaterialTheme { 65 | Image(rememberImagePainter("https://img.zcool.cn/community/017e625e57415ea801216518c25819.jpg@1280w_1l_2o_100sh.jpg"),"") 66 | } 67 | } 68 | } 69 | } 70 | ``` 71 | 72 | ps:Reference of Resource location, you can customize 73 | 74 | ![Resource location](md_resource/resource_location.png) 75 | 76 | Step 4.Recommendation: set the version of Compose-jb(desktop) to 1.1.1 or above 77 | 78 | According to the feedback, it is found that there is a bug in the 1.1.0 version of compose JB, which will not start 79 | 80 |

Custom configuration

81 | 82 |

1.Configure placeholder and error image

83 | 84 | You can use the default parameters in the method 85 | 86 | ![placeholder](md_resource/use_placeholder.png) 87 | 88 | Usage: 89 | 90 | ```kotlin 91 | rememberImagePainter(url, /*the placeholder path*/) 92 | ``` 93 | 94 | Or: 95 | 96 | ```kotlin 97 | rememberImagePainter(DataToBeLoaded(url).apply { 98 | placeholderResource = /*the placeholder path*/ 99 | errorImagePath = /*the error resource path*/ 100 | }) 101 | ``` 102 | 103 |

2.Modify memory cache size

104 | 105 | ```kotlin 106 | LoadTheImageManager.memoryCache = ImageLruMemoryCache(/*max memory cache size*/) 107 | ``` 108 | 109 | ps:Default memory cache size:maxOf(50MB, 1% of total memory) 110 | 111 | You can customize: 112 | 113 | ```kotlin 114 | LoadTheImageManager.memoryCache = /*your class*/ 115 | ``` 116 | 117 |

3.Modify file cache location

118 | 119 | ```kotlin 120 | LoadTheImageManager.fileCache = ImageFileCache(File("C://test_dir")) 121 | ``` 122 | 123 | Or: 124 | 125 | ```kotlin 126 | LoadTheImageManager.fileCache = /*your class*/ 127 | ``` 128 | 129 | ps:Default file cache location: user\Pictures\LoadTheImageCache 130 | 131 |

4.Modify http loader

132 | 133 | ```kotlin 134 | LoadTheImageManager.httpLoader = /*your class*/ 135 | ``` 136 | 137 |

5.Load-the-image supports multiple formats, And it can be expanded by itself

138 | 139 | ![function formats](md_resource/formats.png) 140 | 141 | Custom reference below: 142 | 143 | One.Implement your class: 144 | 145 | ![custom_load-the-image](md_resource/custom_LoadTheImage.png) 146 | 147 | Two.Configure: 148 | ```kotlin 149 | LoadTheImageManager.loadTheImage.add(ByteArrayLoadTheImage()/*your class*/) 150 | ``` 151 | 152 | Three.Use: 153 | ```kotlin 154 | rememberImagePainter(DataToBeLoaded(byteArrayOf()))//Better seal it 155 | ``` 156 | 157 |

If you use compose(Kotlin Multiplatform), You can refer to the example.

158 | 159 | version = [![](https://jitpack.io/v/ltttttttttttt/load-the-image.svg)](https://jitpack.io/#ltttttttttttt/load-the-image) 160 | 161 | Your common dir, build.gradle.kts add: 162 | 163 | ```kotlin 164 | val desktopMain by getting{ 165 | dependencies { 166 | implementation 'com.github.ltttttttttttt:load-the-image:$version' 167 | } 168 | } 169 | ``` 170 | 171 | commonMain add function: 172 | 173 | ```kotlin 174 | @Composable 175 | expect fun rememberImagePainter(url: String): Painter 176 | ``` 177 | 178 | androidMain add function(and other target): 179 | 180 | ```kotlin 181 | @Composable 182 | actual fun rememberImagePainter(url: String): Painter = 183 | coil.compose.rememberImagePainter(data = url) 184 | ``` 185 | 186 | desktopMain add function: 187 | 188 | ```kotlin 189 | @Composable 190 | actual fun rememberImagePainter(url: String): Painter = 191 | com.lt.load_the_image.rememberImagePainter(url) 192 | 193 | ``` 194 | Use the code load image with network and file and resources 195 | 196 | ```kotlin 197 | Image(rememberImagePainter(/*url*/"https://img.zcool.cn/community/017e625e57415ea801216518c25819.jpg@1280w_1l_2o_100sh.jpg","") 198 | ``` -------------------------------------------------------------------------------- /android/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | id("org.jetbrains.compose") version composeVersion 19 | id("com.android.application") 20 | kotlin("android") 21 | } 22 | 23 | group = "com.lt" 24 | version = "1.0" 25 | 26 | repositories { 27 | jcenter() 28 | } 29 | 30 | dependencies { 31 | implementation(project(":common")) 32 | implementation("androidx.activity:activity-compose:1.4.0") 33 | } 34 | 35 | android { 36 | compileSdkVersion(32) 37 | defaultConfig { 38 | applicationId = "com.lt.android" 39 | minSdkVersion(24) 40 | targetSdkVersion(31) 41 | versionCode = 1 42 | versionName = "1.0" 43 | } 44 | compileOptions { 45 | sourceCompatibility = JavaVersion.VERSION_1_8 46 | targetCompatibility = JavaVersion.VERSION_1_8 47 | } 48 | buildTypes { 49 | getByName("release") { 50 | isMinifyEnabled = false 51 | } 52 | } 53 | dexOptions { 54 | javaMaxHeapSize = "4G" 55 | } 56 | } -------------------------------------------------------------------------------- /android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 21 | 22 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /android/src/main/java/com/lt/android/MainActivity.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.android 18 | 19 | import android.os.Bundle 20 | import androidx.activity.compose.setContent 21 | import androidx.appcompat.app.AppCompatActivity 22 | import androidx.compose.material.MaterialTheme 23 | import com.lt.common.App 24 | 25 | class MainActivity : AppCompatActivity() { 26 | override fun onCreate(savedInstanceState: Bundle?) { 27 | super.onCreate(savedInstanceState) 28 | setContent { 29 | MaterialTheme { 30 | App() 31 | //TestLazyList() 32 | } 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | buildscript { 18 | repositories { 19 | gradlePluginPortal() 20 | jcenter() 21 | google() 22 | mavenCentral() 23 | } 24 | dependencies { 25 | classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion") 26 | classpath("com.android.tools.build:gradle:7.1.1") 27 | } 28 | } 29 | 30 | group = "com.lt" 31 | version = "1.0" 32 | 33 | allprojects { 34 | repositories { 35 | google() 36 | mavenCentral() 37 | maven("https://maven.pkg.jetbrains.space/public/p/compose/dev") 38 | } 39 | } -------------------------------------------------------------------------------- /buildSrc/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | `kotlin-dsl` 19 | } 20 | 21 | repositories { 22 | maven("https://mirrors.tencent.com/nexus/repository/maven-public/") 23 | } 24 | 25 | dependencies { 26 | } -------------------------------------------------------------------------------- /buildSrc/src/main/kotlin/Versions.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | const val kotlinVersion = "1.7.20"//kotlin版本 18 | const val composeVersion = "1.2.0"//compose版本 19 | //const val composeCompilerVersion = "1.3.0"//compose编译版本 -------------------------------------------------------------------------------- /common/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | kotlin("multiplatform") 19 | id("org.jetbrains.compose") version composeVersion 20 | id("com.android.library") 21 | } 22 | 23 | group = "com.lt" 24 | version = "1.0" 25 | 26 | kotlin { 27 | android() 28 | jvm("desktop") { 29 | compilations.all { 30 | kotlinOptions.jvmTarget = "11" 31 | } 32 | } 33 | sourceSets { 34 | val commonMain by getting { 35 | dependencies { 36 | api(compose.runtime) 37 | api(compose.foundation) 38 | api(compose.material) 39 | } 40 | } 41 | val commonTest by getting { 42 | dependencies { 43 | implementation(kotlin("test")) 44 | } 45 | } 46 | val androidMain by getting { 47 | dependencies { 48 | api("androidx.appcompat:appcompat:1.4.1") 49 | api("androidx.core:core-ktx:1.7.0") 50 | //coil图片加载,适用于compose 51 | api("io.coil-kt:coil-compose:1.4.0") 52 | } 53 | } 54 | val androidTest by getting { 55 | dependencies { 56 | implementation("junit:junit:4.13.2") 57 | } 58 | } 59 | val desktopMain by getting { 60 | dependencies { 61 | api(compose.preview) 62 | api(project(":load-the-image")) 63 | } 64 | } 65 | val desktopTest by getting 66 | } 67 | } 68 | 69 | android { 70 | compileSdkVersion(31) 71 | sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml") 72 | sourceSets["main"].res.srcDir("../desktop/src/jvmMain/resources") 73 | defaultConfig { 74 | minSdkVersion(24) 75 | targetSdkVersion(31) 76 | } 77 | compileOptions { 78 | sourceCompatibility = JavaVersion.VERSION_1_8 79 | targetCompatibility = JavaVersion.VERSION_1_8 80 | } 81 | } -------------------------------------------------------------------------------- /common/src/androidMain/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | -------------------------------------------------------------------------------- /common/src/androidMain/kotlin/com/lt/common/platform.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.common 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.ui.graphics.painter.Painter 21 | 22 | actual fun getPlatformName(): String { 23 | return "Android" 24 | } 25 | 26 | @Composable 27 | actual fun rememberImagePainter(url: String): Painter = 28 | coil.compose.rememberImagePainter(data = url) -------------------------------------------------------------------------------- /common/src/commonMain/kotlin/com/lt/common/App.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.common 18 | 19 | import androidx.compose.foundation.Image 20 | import androidx.compose.foundation.layout.Row 21 | import androidx.compose.foundation.layout.fillMaxHeight 22 | import androidx.compose.foundation.layout.fillMaxSize 23 | import androidx.compose.foundation.layout.height 24 | import androidx.compose.foundation.layout.size 25 | import androidx.compose.foundation.lazy.LazyColumn 26 | import androidx.compose.foundation.lazy.LazyListState 27 | import androidx.compose.foundation.lazy.items 28 | import androidx.compose.foundation.lazy.rememberLazyListState 29 | import androidx.compose.runtime.Composable 30 | import androidx.compose.ui.Modifier 31 | import androidx.compose.ui.unit.dp 32 | 33 | @Composable 34 | fun App( 35 | lazyColumnState: LazyListState = rememberLazyListState() 36 | ) { 37 | val images = listOf( 38 | //Correct 39 | "C:\\SpringBootFiles\\imgs\\7633948650d6b30461fce6d13422ec3a.jpeg", 40 | "https://img.zcool.cn/community/017e625e57415ea801216518c25819.jpg@1280w_1l_2o_100sh.jpg", 41 | "drawable-xxhdpi/img_a.jpeg", 42 | "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbkimg.cdn.bcebos.com%2Fpic%2Ff9198618367adab44aed43580398a41c8701a18b4ed1&refer=http%3A%2F%2Fbkimg.cdn.bcebos.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651992355&t=0eb27c606fc381fdaaaca828505ac114", 43 | "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Folimg.3dmgame.com%2Fuploads%2Fimages%2Fraiders%2F20200319%2F1584597196_470838.jpg&refer=http%3A%2F%2Folimg.3dmgame.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651991879&t=6ff7794b1d539b27df0f55a97004a36e", 44 | "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jbzj.com%2Ffile_images%2Fgame%2F201512%2F2015122310510491.jpg&refer=http%3A%2F%2Fimg.jbzj.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651991926&t=7f0680dcc5189609ead24cdbb5d6d291", 45 | "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbkimg.cdn.bcebos.com%2Fpic%2F8326cffc1e178a82b9018131e84f648da97739124247&refer=http%3A%2F%2Fbkimg.cdn.bcebos.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651992324&t=afbd7342f9b42a0c05f1ddd6e4e7d5f0", 46 | "https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/baike/pic/item/e850352ac65c10380e49077eba119313b07e8953.jpg", 47 | "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbkimg.cdn.bcebos.com%2Fpic%2F0df431adcbef76094b36910e3091b4cc7cd98d103044&refer=http%3A%2F%2Fbkimg.cdn.bcebos.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651991945&t=a6c5f8f1fa6a8bbeba11b99ec0a03f94", 48 | 49 | //Three error 50 | "C:\\SpringBootFiles\\imgs\\45260159a1f8864027389751.jpg", 51 | "drawable-xxhdpi/image_b.jpg", 52 | "https://img.zcool.cn/community/017e625e57415ea80121128c25819.jpg@1280w_1l_2o_100sh.jpg", 53 | ) 54 | 55 | LazyColumn(state = lazyColumnState) { 56 | items(images) { 57 | Image(rememberImagePainter(it), "", Modifier.size(500.dp)) 58 | } 59 | } 60 | } 61 | 62 | val moreImages = mutableListOf().apply { 63 | repeat(9999) { 64 | add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbkimg.cdn.bcebos.com%2Fpic%2Ff9198618367adab44aed43580398a41c8701a18b4ed1&refer=http%3A%2F%2Fbkimg.cdn.bcebos.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651992355&t=0eb27c606fc381fdaaaca828505ac114&a=$it&b=") 65 | } 66 | } 67 | 68 | //更多图片测试 69 | @Composable 70 | fun MoreImage() { 71 | LazyColumn(Modifier.fillMaxSize()) { 72 | items(moreImages) { 73 | Row(Modifier.height(50.dp)) { 74 | repeat(10) { i -> 75 | Image( 76 | rememberImagePainter(it + i), "", Modifier.weight(1f).fillMaxHeight() 77 | ) 78 | } 79 | } 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /common/src/commonMain/kotlin/com/lt/common/TestLazyList.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.common 18 | 19 | import androidx.compose.foundation.Image 20 | import androidx.compose.foundation.layout.Column 21 | import androidx.compose.foundation.layout.size 22 | import androidx.compose.foundation.lazy.LazyColumn 23 | import androidx.compose.foundation.lazy.items 24 | import androidx.compose.material.Button 25 | import androidx.compose.material.Text 26 | import androidx.compose.runtime.Composable 27 | import androidx.compose.runtime.mutableStateListOf 28 | import androidx.compose.ui.Modifier 29 | import androidx.compose.ui.unit.dp 30 | 31 | /** 32 | * creator: lt 2022/7/23 lt.dygzs@qq.com 33 | * effect : 34 | * warning: 35 | */ 36 | val list = mutableStateListOf( 37 | "https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/baike/pic/item/e850352ac65c10380e49077eba119313b07e8953.jpg", 38 | "https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/baike/pic/item/e850352ac65c10380e49077eba119313b07e8953.jpg", 39 | "https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/baike/pic/item/e850352ac65c10380e49077eba119313b07e8953.jpg", 40 | ) 41 | 42 | @Composable 43 | fun TestLazyList() { 44 | Column { 45 | Button({ 46 | list[0]="C:\\Users\\Administrator\\Pictures\\imageviewer\\1.jpg" 47 | list.add("https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/baike/pic/item/e850352ac65c10380e49077eba119313b07e8953.jpg") 48 | }) { 49 | Text("add") 50 | } 51 | LazyColumn { 52 | items(list) { 53 | Image(rememberImagePainter(it), "", modifier = Modifier.size(100.dp)) 54 | } 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /common/src/commonMain/kotlin/com/lt/common/platform.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.common 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.ui.graphics.painter.Painter 21 | 22 | expect fun getPlatformName(): String 23 | 24 | @Composable 25 | expect fun rememberImagePainter(url: String): Painter -------------------------------------------------------------------------------- /common/src/desktopMain/kotlin/com/lt/common/DesktopApp.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.common 18 | 19 | import androidx.compose.desktop.ui.tooling.preview.Preview 20 | import androidx.compose.runtime.Composable 21 | 22 | @Preview 23 | @Composable 24 | fun AppPreview() { 25 | App() 26 | } -------------------------------------------------------------------------------- /common/src/desktopMain/kotlin/com/lt/common/platform.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.common 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.ui.graphics.painter.Painter 21 | 22 | actual fun getPlatformName(): String { 23 | return "Desktop" 24 | } 25 | 26 | @Composable 27 | actual fun rememberImagePainter(url: String): Painter = 28 | com.lt.load_the_image.rememberImagePainter(url) -------------------------------------------------------------------------------- /desktop/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import org.jetbrains.compose.desktop.application.dsl.TargetFormat 18 | 19 | plugins { 20 | kotlin("multiplatform") 21 | id("org.jetbrains.compose") version composeVersion 22 | } 23 | 24 | group = "com.lt" 25 | version = "1.0" 26 | 27 | kotlin { 28 | jvm { 29 | compilations.all { 30 | kotlinOptions.jvmTarget = "11" 31 | } 32 | withJava() 33 | } 34 | sourceSets { 35 | val jvmMain by getting { 36 | dependencies { 37 | implementation(project(":common")) 38 | implementation(compose.desktop.currentOs) 39 | //引入协程,并引入基于swing的Main协程调度器 40 | //implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.6.2") 41 | } 42 | } 43 | val jvmTest by getting 44 | } 45 | } 46 | 47 | compose.desktop { 48 | application { 49 | mainClass = "MainKt" 50 | nativeDistributions { 51 | targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb) 52 | packageName = "jvm" 53 | packageVersion = "1.0.0" 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /desktop/src/jvmMain/kotlin/Main.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import androidx.compose.foundation.Image 18 | import androidx.compose.foundation.VerticalScrollbar 19 | import androidx.compose.foundation.layout.Box 20 | import androidx.compose.foundation.layout.Column 21 | import androidx.compose.foundation.layout.Row 22 | import androidx.compose.foundation.layout.fillMaxHeight 23 | import androidx.compose.foundation.layout.fillMaxSize 24 | import androidx.compose.foundation.layout.size 25 | import androidx.compose.foundation.lazy.LazyListState 26 | import androidx.compose.foundation.lazy.rememberLazyListState 27 | import androidx.compose.foundation.rememberScrollbarAdapter 28 | import androidx.compose.material.MaterialTheme 29 | import androidx.compose.runtime.Composable 30 | import androidx.compose.runtime.remember 31 | import androidx.compose.ui.Alignment 32 | import androidx.compose.ui.Modifier 33 | import androidx.compose.ui.unit.dp 34 | import androidx.compose.ui.window.Window 35 | import androidx.compose.ui.window.application 36 | import com.lt.common.App 37 | import com.lt.common.MoreImage 38 | import com.lt.load_the_image.LoadTheImageManager 39 | import com.lt.load_the_image.loader.DataToBeLoaded 40 | import com.lt.load_the_image.rememberImagePainter 41 | import java.io.File 42 | 43 | 44 | fun main() { 45 | LoadTheImageManager.defaultErrorImagePath = "drawable-xxhdpi/load_error.jpeg" 46 | application { 47 | Window(onCloseRequest = ::exitApplication) { 48 | MaterialTheme { 49 | //TestLazyList() 50 | //UI() 51 | MoreImage() 52 | } 53 | } 54 | } 55 | } 56 | 57 | @Composable 58 | private fun UI() { 59 | Column { 60 | Row { 61 | Image( 62 | rememberImagePainter( 63 | "https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/baike/pic/item/e850352ac65c10380e49077eba119313b07e8953.jpg", 64 | "drawable-xxhdpi/img_a.jpeg" 65 | ), "", Modifier.size(50.dp) 66 | ) 67 | Image( 68 | rememberImagePainter( 69 | File("C:\\SpringBootFiles\\imgs\\7633948650d6b30461fce6d13422ec3a.jpeg"), 70 | ), "", Modifier.size(50.dp) 71 | ) 72 | Image( 73 | rememberImagePainter( 74 | remember { 75 | DataToBeLoaded( 76 | try { 77 | File("C:\\SpringBootFiles\\imgs\\7633948650d6b30461fce6d13422ec3a.jpeg").inputStream() 78 | } catch (e: Exception) { 79 | File("C:\\SpringBootFiles\\imgs\\7633948650d6b30461fce6d13422ec3a.jpeg") 80 | } 81 | ) 82 | }, 83 | ), "", Modifier.size(50.dp) 84 | ) 85 | } 86 | Box(Modifier.fillMaxSize()) { 87 | val lazyColumnState: LazyListState = rememberLazyListState() 88 | App(lazyColumnState) 89 | VerticalScrollbar( 90 | modifier = Modifier.align(Alignment.CenterEnd).fillMaxHeight(), 91 | adapter = rememberScrollbarAdapter( 92 | scrollState = lazyColumnState 93 | ) 94 | ) 95 | } 96 | } 97 | } -------------------------------------------------------------------------------- /desktop/src/jvmMain/resources/drawable-xxhdpi/img_a.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ltttttttttttt/load-the-image/b0df20b73de63c7d2305676dc04507f7bc3a4c84/desktop/src/jvmMain/resources/drawable-xxhdpi/img_a.jpeg -------------------------------------------------------------------------------- /desktop/src/jvmMain/resources/drawable-xxhdpi/load_error.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ltttttttttttt/load-the-image/b0df20b73de63c7d2305676dc04507f7bc3a4c84/desktop/src/jvmMain/resources/drawable-xxhdpi/load_error.jpeg -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright lt 2023 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | kotlin.code.style=official 18 | android.useAndroidX=true 19 | kotlin.mpp.enableGranularSourceSetsMetadata=true 20 | kotlin.native.enableDependencyPropagation=false 21 | android.enableBuildCache=false 22 | kotlin.mpp.stability.nowarn=true 23 | org.gradle.jvmargs=-Xmx4608m -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ltttttttttttt/load-the-image/b0df20b73de63c7d2305676dc04507f7bc3a4c84/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright lt 2023 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | distributionBase=GRADLE_USER_HOME 18 | distributionPath=wrapper/dists 19 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip 20 | zipStoreBase=GRADLE_USER_HOME 21 | zipStorePath=wrapper/dists -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | # Determine the Java command to use to start the JVM. 86 | if [ -n "$JAVA_HOME" ] ; then 87 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 88 | # IBM's JDK on AIX uses strange locations for the executables 89 | JAVACMD="$JAVA_HOME/jre/sh/java" 90 | else 91 | JAVACMD="$JAVA_HOME/bin/java" 92 | fi 93 | if [ ! -x "$JAVACMD" ] ; then 94 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 95 | 96 | Please set the JAVA_HOME variable in your environment to match the 97 | location of your Java installation." 98 | fi 99 | else 100 | JAVACMD="java" 101 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 102 | 103 | Please set the JAVA_HOME variable in your environment to match the 104 | location of your Java installation." 105 | fi 106 | 107 | # Increase the maximum file descriptors if we can. 108 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 109 | MAX_FD_LIMIT=`ulimit -H -n` 110 | if [ $? -eq 0 ] ; then 111 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 112 | MAX_FD="$MAX_FD_LIMIT" 113 | fi 114 | ulimit -n $MAX_FD 115 | if [ $? -ne 0 ] ; then 116 | warn "Could not set maximum file descriptor limit: $MAX_FD" 117 | fi 118 | else 119 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 120 | fi 121 | fi 122 | 123 | # For Darwin, add options to specify how the application appears in the dock 124 | if $darwin; then 125 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 126 | fi 127 | 128 | # For Cygwin or MSYS, switch paths to Windows format before running java 129 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 130 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 131 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 132 | JAVACMD=`cygpath --unix "$JAVACMD"` 133 | 134 | # We build the pattern for arguments to be converted via cygpath 135 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 136 | SEP="" 137 | for dir in $ROOTDIRSRAW ; do 138 | ROOTDIRS="$ROOTDIRS$SEP$dir" 139 | SEP="|" 140 | done 141 | OURCYGPATTERN="(^($ROOTDIRS))" 142 | # Add a user-defined pattern to the cygpath arguments 143 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 144 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 145 | fi 146 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 147 | i=0 148 | for arg in "$@" ; do 149 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 150 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 151 | 152 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 153 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 154 | else 155 | eval `echo args$i`="\"$arg\"" 156 | fi 157 | i=`expr $i + 1` 158 | done 159 | case $i in 160 | 0) set -- ;; 161 | 1) set -- "$args0" ;; 162 | 2) set -- "$args0" "$args1" ;; 163 | 3) set -- "$args0" "$args1" "$args2" ;; 164 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 165 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 166 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 167 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 168 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 169 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 170 | esac 171 | fi 172 | 173 | # Escape application args 174 | save () { 175 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 176 | echo " " 177 | } 178 | APP_ARGS=`save "$@"` 179 | 180 | # Collect all arguments for the java command, following the shell quoting and substitution rules 181 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 182 | 183 | exec "$JAVACMD" "$@" 184 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 33 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 34 | 35 | @rem Find java.exe 36 | if defined JAVA_HOME goto findJavaFromJavaHome 37 | 38 | set JAVA_EXE=java.exe 39 | %JAVA_EXE% -version >NUL 2>&1 40 | if "%ERRORLEVEL%" == "0" goto init 41 | 42 | echo. 43 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 44 | echo. 45 | echo Please set the JAVA_HOME variable in your environment to match the 46 | echo location of your Java installation. 47 | 48 | goto fail 49 | 50 | :findJavaFromJavaHome 51 | set JAVA_HOME=%JAVA_HOME:"=% 52 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 53 | 54 | if exist "%JAVA_EXE%" goto init 55 | 56 | echo. 57 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 58 | echo. 59 | echo Please set the JAVA_HOME variable in your environment to match the 60 | echo location of your Java installation. 61 | 62 | goto fail 63 | 64 | :init 65 | @rem Get command-line arguments, handling Windows variants 66 | 67 | if not "%OS%" == "Windows_NT" goto win9xME_args 68 | 69 | :win9xME_args 70 | @rem Slurp the command line arguments. 71 | set CMD_LINE_ARGS= 72 | set _SKIP=2 73 | 74 | :win9xME_args_slurp 75 | if "x%~1" == "x" goto execute 76 | 77 | set CMD_LINE_ARGS=%* 78 | 79 | :execute 80 | @rem Setup the command line 81 | 82 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 83 | 84 | @rem Execute Gradle 85 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 86 | 87 | :end 88 | @rem End local scope for the variables with windows NT shell 89 | if "%ERRORLEVEL%"=="0" goto mainEnd 90 | 91 | :fail 92 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 93 | rem the _cmd.exe /c_ return code! 94 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 95 | exit /b 1 96 | 97 | :mainEnd 98 | if "%OS%"=="Windows_NT" endlocal 99 | 100 | :omega 101 | -------------------------------------------------------------------------------- /jitpack.yml: -------------------------------------------------------------------------------- 1 | jdk: 2 | - openjdk11 -------------------------------------------------------------------------------- /load-the-image/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /load-the-image/build.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | plugins { 18 | id("java-library") 19 | id("org.jetbrains.kotlin.jvm") 20 | id("org.jetbrains.compose") version composeVersion 21 | id("maven-publish") 22 | } 23 | 24 | group = "com.github.ltttttttttttt" 25 | version = "1.0.0" 26 | 27 | java { 28 | sourceCompatibility = JavaVersion.VERSION_11 29 | targetCompatibility = JavaVersion.VERSION_11 30 | } 31 | 32 | dependencies { 33 | implementation(compose.ui) 34 | implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion") 35 | implementation("com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.2") 36 | implementation("com.squareup.okhttp3:okhttp:4.9.1") 37 | } 38 | 39 | publishing { 40 | publications { 41 | create("maven_public", MavenPublication::class) { 42 | from(components.getByName("java")) 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/LoadTheImageManager.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.runtime.remember 21 | import androidx.compose.ui.ExperimentalComposeUiApi 22 | import androidx.compose.ui.graphics.ImageBitmap 23 | import androidx.compose.ui.graphics.painter.Painter 24 | import androidx.compose.ui.res.ClassLoaderResourceLoader 25 | import androidx.compose.ui.res.loadImageBitmap 26 | import androidx.compose.ui.res.painterResource 27 | import com.lt.load_the_image.cache.ImageCache 28 | import com.lt.load_the_image.cache.ImageFileCache 29 | import com.lt.load_the_image.cache.ImageLruMemoryCache 30 | import com.lt.load_the_image.loader.* 31 | import com.lt.load_the_image.loader.http.HttpLoader 32 | import com.lt.load_the_image.loader.http.HttpURLConnectionLoader 33 | import com.lt.load_the_image.loader.http.OkHttpLoader 34 | import com.lt.load_the_image.loader.image.* 35 | import com.lt.load_the_image.painter.DefaultPainterCreator 36 | import com.lt.load_the_image.painter.EmptyImagePainter 37 | import com.lt.load_the_image.painter.PainterCreator 38 | 39 | /** 40 | * Set LoadTheImage settings 41 | */ 42 | object LoadTheImageManager { 43 | /** 44 | * Set is use the memory cache 45 | */ 46 | var memoryCache: ImageCache = ImageLruMemoryCache() 47 | 48 | /** 49 | * Set is use the file cache 50 | */ 51 | var fileCache: ImageCache = ImageFileCache() 52 | 53 | /** 54 | * Load network resource to byteArray 55 | */ 56 | var httpLoader: HttpLoader = OkHttpLoader() 57 | 58 | /** 59 | * Create Painter from ByteArray 60 | */ 61 | var painterCreator: PainterCreator = DefaultPainterCreator() 62 | 63 | /** 64 | * Handler load image 65 | */ 66 | var loadTheImage: MutableList = mutableListOf( 67 | HttpLoadTheImage(), 68 | BitmapLoadTheImage(), 69 | ImageLoadTheImage(), 70 | ByteArrayLoadTheImage(), 71 | InputStreamLoadTheImage(), 72 | FileLoadTheImage(), 73 | ResourcesLoadTheImage(), 74 | ) 75 | 76 | /** 77 | * The path of the picture displayed when an exception occurred while loading the picture 78 | */ 79 | var defaultErrorImagePath = "" 80 | 81 | /** 82 | * Load the image 83 | */ 84 | @Composable 85 | fun load(data: DataToBeLoaded): Painter { 86 | val loader = remember(data.data) { 87 | loadTheImage.find { it.canLoad(data) } 88 | } 89 | if (loader != null) 90 | return loader.load(data) 91 | ?: kotlin.run { 92 | println("Load the image error: Exception loading URL, data=${data.data}") 93 | createErrorPainter(data) 94 | } 95 | 96 | println("Load the image error: No suitable LoadTheImage found, data=${data.data}") 97 | return createErrorPainter(data) 98 | } 99 | 100 | @Composable 101 | internal fun createErrorPainter(data: DataToBeLoaded): Painter { 102 | val errorImagePath = data.errorImagePath 103 | if (errorImagePath.isEmpty()) 104 | return remember { EmptyImagePainter() } 105 | return painterResource(errorImagePath) 106 | } 107 | 108 | internal fun createErrorImageBitmap(data: DataToBeLoaded): ImageBitmap? { 109 | val errorImagePath = data.errorImagePath 110 | if (errorImagePath.isEmpty()) 111 | return null 112 | return loadResourceImageBitmap(errorImagePath) 113 | } 114 | 115 | @OptIn(ExperimentalComposeUiApi::class) 116 | internal fun loadResourceImageBitmap(resourcePath: String) = 117 | loadImageBitmap(ClassLoaderResourceLoader().load(resourcePath)) 118 | } 119 | -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/RememberImagePainter.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.runtime.remember 21 | import androidx.compose.ui.graphics.painter.Painter 22 | import com.lt.load_the_image.loader.DataToBeLoaded 23 | import com.lt.load_the_image.loader.rememberDataToBeLoaded 24 | import org.jetbrains.skia.Bitmap 25 | import org.jetbrains.skia.Image 26 | import java.io.File 27 | import java.io.InputStream 28 | 29 | /** 30 | * Load the image to @Composable Image 31 | * [url]Can use Net url or native url 32 | * [placeholderResource]Bitmap when loading network pictures (resource) 33 | */ 34 | @Composable 35 | fun rememberImagePainter(url: String, placeholderResource: String = ""): Painter { 36 | val data = remember(url, placeholderResource) { 37 | val data = DataToBeLoaded(url) 38 | data.placeholderResource = placeholderResource 39 | data 40 | } 41 | return rememberImagePainter(data) 42 | } 43 | 44 | /** 45 | * Load the image to @Composable Image 46 | * [file]Local file 47 | */ 48 | @Composable 49 | fun rememberImagePainter(file: File): Painter { 50 | return LoadTheImageManager.load(rememberDataToBeLoaded(file)) 51 | } 52 | 53 | /** 54 | * Load the image to @Composable Image 55 | * [bitmap]The [Bitmap] 56 | */ 57 | @Composable 58 | fun rememberImagePainter(bitmap: Bitmap): Painter { 59 | return LoadTheImageManager.load(rememberDataToBeLoaded(bitmap)) 60 | } 61 | 62 | /** 63 | * Load the image to @Composable Image 64 | * [image]The [Image] 65 | */ 66 | @Composable 67 | fun rememberImagePainter(image: Image): Painter { 68 | return LoadTheImageManager.load(rememberDataToBeLoaded(image)) 69 | } 70 | 71 | /** 72 | * Load the image to @Composable Image 73 | * [byteArray]Bytes of image 74 | */ 75 | @Composable 76 | fun rememberImagePainter(byteArray: ByteArray): Painter { 77 | return LoadTheImageManager.load(rememberDataToBeLoaded(byteArray)) 78 | } 79 | 80 | /** 81 | * Load the image to @Composable Image 82 | * [inputStream]InputStream of image 83 | * [placeholderResource]Bitmap when loading network pictures (resource) 84 | * [isAutoCloseStream]Whether to close the stream automatically 85 | */ 86 | @Composable 87 | fun rememberImagePainter( 88 | inputStream: InputStream, 89 | placeholderResource: String = "", 90 | isAutoCloseStream: Boolean = true 91 | ): Painter { 92 | val data = remember(inputStream, placeholderResource, isAutoCloseStream) { 93 | val data = DataToBeLoaded(inputStream) 94 | data.placeholderResource = placeholderResource 95 | data.isAutoCloseStream = isAutoCloseStream 96 | data 97 | } 98 | return rememberImagePainter(data) 99 | } 100 | 101 | /** 102 | * Load the image to @Composable Image 103 | * [data]Self assembling data 104 | */ 105 | @Composable 106 | fun rememberImagePainter(data: DataToBeLoaded): Painter { 107 | return LoadTheImageManager.load(data) 108 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/cache/ImageCache.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.cache 18 | 19 | /** 20 | * creator: lt 2022/4/8 lt.dygzs@qq.com 21 | * effect : Cache configuration of network image 22 | * warning: 23 | */ 24 | interface ImageCache { 25 | 26 | /** 27 | * Save image Cache 28 | * [url]Unique key 29 | * [t]The cache 30 | */ 31 | fun saveCache(url: String, t: ByteArray) 32 | 33 | /** 34 | * Get image Cache 35 | * [url]Unique key 36 | */ 37 | fun getCache(url: String): ByteArray? 38 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/cache/ImageFileCache.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.cache 18 | 19 | import com.lt.load_the_image.util.MD5 20 | import com.lt.load_the_image.util.println 21 | import java.io.File 22 | 23 | /** 24 | * creator: lt 2022/4/8 lt.dygzs@qq.com 25 | * effect : Native file cache configuration of network image 26 | * warning: [cacheDir] Native file cache dir 27 | */ 28 | open class ImageFileCache( 29 | private val cacheDir: File = createCacheDir() 30 | ) : ImageCache { 31 | 32 | init { 33 | if (!cacheDir.exists() || !cacheDir.isDirectory) 34 | cacheDir.mkdirs() 35 | } 36 | 37 | override fun saveCache(url: String, t: ByteArray) { 38 | try { 39 | val file = File(cacheDir, urlToFileName(url)) 40 | if (file.exists()) { 41 | if (file.length() == t.size.toLong()) 42 | return 43 | file.delete() 44 | } 45 | file.createNewFile() 46 | file.writeBytes(t) 47 | } catch (e: Exception) { 48 | e.println() 49 | } 50 | } 51 | 52 | override fun getCache(url: String): ByteArray? { 53 | try { 54 | val file = File(cacheDir, urlToFileName(url)) 55 | if (file.exists()) 56 | return file.readBytes() 57 | } catch (e: Exception) { 58 | e.println() 59 | } 60 | return null 61 | } 62 | 63 | //Convert url to file name 64 | private fun urlToFileName(url: String): String { 65 | return MD5.GetMD5Code(url) + url.length + url.hashCode() + ".jpg" 66 | } 67 | } 68 | 69 | //Create cache dir 70 | private fun createCacheDir() = File( 71 | System.getProperty("user.home") 72 | + File.separator 73 | + "Pictures" 74 | + File.separator 75 | + "LoadTheImageCache" 76 | + File.separator 77 | ) -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/cache/ImageLruMemoryCache.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.cache 18 | 19 | import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap 20 | import java.util.concurrent.atomic.AtomicLong 21 | 22 | /** 23 | * creator: lt 2022/4/8 lt.dygzs@qq.com 24 | * effect : Memory cache configuration of network image, cache use LRU 25 | * warning: [maxMemorySize] Byte maximum memory usage 26 | */ 27 | open class ImageLruMemoryCache( 28 | private val maxMemorySize: Long = getMemoryWithOnePercent() 29 | ) : ImageCache { 30 | //image lru cache 31 | private val cacheMap = ConcurrentLinkedHashMap.Builder() 32 | .initialCapacity(35) 33 | .maximumWeightedCapacity(9999) 34 | .build() 35 | 36 | //image cache byte size sum 37 | private var cacheSize = AtomicLong(0) 38 | 39 | override fun saveCache(url: String, t: ByteArray) { 40 | if (t.size > maxMemorySize) 41 | return 42 | cacheMap[url] = t 43 | cacheSize.getAndAdd(t.size.toLong()) 44 | while (cacheSize.get() > maxMemorySize && cacheMap.isNotEmpty()) { 45 | val byteArray = cacheMap.remove(cacheMap.keys.firstOrNull() ?: run { 46 | cacheSize.set(0) 47 | return 48 | }) 49 | cacheSize.getAndAdd(-(byteArray?.size ?: 0).toLong()) 50 | } 51 | } 52 | 53 | override fun getCache(url: String): ByteArray? { 54 | return cacheMap[url] 55 | } 56 | } 57 | 58 | //Gets one percent of the total memory 59 | private fun getMemoryWithOnePercent(): Long { 60 | return maxOf(50L * 1024 * 1024, Runtime.getRuntime().maxMemory() / 100) 61 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/cache/NoImageCache.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.cache 18 | 19 | /** 20 | * creator: lt 2022/4/8 lt.dygzs@qq.com 21 | * effect : Do not use cache 22 | * warning: 23 | */ 24 | class NoImageCache : ImageCache { 25 | override fun saveCache(url: String, t: ByteArray) { 26 | } 27 | 28 | override fun getCache(url: String): ByteArray? = null 29 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/loader/LoadTheImage.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.loader 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.runtime.Stable 21 | import androidx.compose.runtime.remember 22 | import androidx.compose.ui.graphics.painter.Painter 23 | import com.lt.load_the_image.LoadTheImageManager 24 | 25 | /** 26 | * creator: lt 2022/4/8 lt.dygzs@qq.com 27 | * effect : Load the image: url to Painter 28 | * warning: 29 | */ 30 | interface LoadTheImage { 31 | 32 | /** 33 | * Complete process with load image 34 | */ 35 | @Composable 36 | fun load(data: DataToBeLoaded): Painter? 37 | 38 | /** 39 | * Return false means it cannot be processed 40 | */ 41 | fun canLoad(data: DataToBeLoaded): Boolean 42 | } 43 | 44 | /** 45 | * Data loaded in [LoadTheImage] 46 | * [data]The data 47 | */ 48 | @Stable 49 | open class DataToBeLoaded(val data: Any) { 50 | /** 51 | * Bitmap when loading network pictures (resource) 52 | */ 53 | var placeholderResource: String = "" 54 | 55 | /** 56 | * Whether to close the stream automatically when [data] is a stream 57 | */ 58 | var isAutoCloseStream: Boolean = true 59 | 60 | /** 61 | * The path of the picture displayed when an exception occurred while loading the picture 62 | */ 63 | var errorImagePath = LoadTheImageManager.defaultErrorImagePath 64 | } 65 | 66 | /** 67 | * Remember the DataToBeLoaded 68 | */ 69 | @Composable 70 | fun rememberDataToBeLoaded(data: Any) = remember(data) { DataToBeLoaded(data) } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/loader/http/HttpLoader.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.loader.http 18 | 19 | /** 20 | * creator: lt 2022/4/8 lt.dygzs@qq.com 21 | * effect : Load network resource to byteArray 22 | * warning: 23 | */ 24 | fun interface HttpLoader { 25 | 26 | /** 27 | * Load network resource to byteArray 28 | * [url]Network url 29 | */ 30 | fun load(url: String): ByteArray? 31 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/loader/http/HttpURLConnectionLoader.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.loader.http 18 | 19 | import com.lt.load_the_image.util.println 20 | import java.net.URL 21 | 22 | /** 23 | * creator: lt 2022/4/8 lt.dygzs@qq.com 24 | * effect : Use HttpURLConnection load url 25 | * warning: 26 | */ 27 | open class HttpURLConnectionLoader : HttpLoader { 28 | override fun load(url: String): ByteArray? { 29 | return try { 30 | URL(url).readBytes() 31 | } catch (e: Exception) { 32 | e.println() 33 | null 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/loader/http/OkHttpLoader.kt: -------------------------------------------------------------------------------- 1 | package com.lt.load_the_image.loader.http 2 | 3 | import com.lt.load_the_image.util.println 4 | import okhttp3.OkHttpClient 5 | import okhttp3.Request 6 | 7 | /** 8 | * creator: lt 2022/4/8 lt.dygzs@qq.com 9 | * effect : Use OkHttp load url 10 | * warning: 11 | */ 12 | open class OkHttpLoader( 13 | private val okHttpClient: OkHttpClient = OkHttpClient() 14 | ) : HttpLoader { 15 | 16 | override fun load(url: String): ByteArray? { 17 | return try { 18 | val build = Request.Builder() 19 | .url(url) 20 | .build() 21 | okHttpClient.newCall(build).execute().body?.bytes() 22 | } catch (e: Exception) { 23 | e.println() 24 | null 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/loader/image/BitmapLoadTheImage.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.loader.image 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.runtime.remember 21 | import androidx.compose.ui.graphics.asComposeImageBitmap 22 | import androidx.compose.ui.graphics.painter.BitmapPainter 23 | import androidx.compose.ui.graphics.painter.Painter 24 | import com.lt.load_the_image.loader.DataToBeLoaded 25 | import com.lt.load_the_image.loader.LoadTheImage 26 | import org.jetbrains.skia.Bitmap 27 | 28 | /** 29 | * creator: lt 2022/6/1 lt.dygzs@qq.com 30 | * effect : Load the image from [Bitmap] 31 | * warning: 32 | */ 33 | class BitmapLoadTheImage : LoadTheImage { 34 | @Composable 35 | override fun load(data: DataToBeLoaded): Painter? { 36 | val bitmap = data.data as? Bitmap ?: return null 37 | return remember(bitmap) { BitmapPainter(bitmap.asComposeImageBitmap()) } 38 | } 39 | 40 | override fun canLoad(data: DataToBeLoaded): Boolean { 41 | return data.data is Bitmap 42 | } 43 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/loader/image/ByteArrayLoadTheImage.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.loader.image 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.runtime.remember 21 | import androidx.compose.ui.graphics.painter.Painter 22 | import com.lt.load_the_image.LoadTheImageManager 23 | import com.lt.load_the_image.loader.DataToBeLoaded 24 | import com.lt.load_the_image.loader.LoadTheImage 25 | 26 | /** 27 | * creator: lt 2022/6/1 lt.dygzs@qq.com 28 | * effect : Load the image from [ByteArray] of image 29 | * warning: 30 | */ 31 | class ByteArrayLoadTheImage : LoadTheImage { 32 | @Composable 33 | override fun load(data: DataToBeLoaded): Painter? { 34 | val byteArray = data.data as? ByteArray ?: return null 35 | return remember(byteArray) { LoadTheImageManager.painterCreator.create(byteArray) } 36 | } 37 | 38 | override fun canLoad(data: DataToBeLoaded): Boolean { 39 | return data.data is ByteArray 40 | } 41 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/loader/image/FileLoadTheImage.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.loader.image 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.runtime.LaunchedEffect 21 | import androidx.compose.runtime.remember 22 | import androidx.compose.ui.graphics.painter.Painter 23 | import com.lt.load_the_image.LoadTheImageManager 24 | import com.lt.load_the_image.loader.DataToBeLoaded 25 | import com.lt.load_the_image.loader.LoadTheImage 26 | import com.lt.load_the_image.painter.AsyncImagePainter 27 | import com.lt.load_the_image.util.println 28 | import kotlinx.coroutines.Dispatchers 29 | import kotlinx.coroutines.withContext 30 | import java.io.File 31 | 32 | /** 33 | * creator: lt 2022/4/8 lt.dygzs@qq.com 34 | * effect : Load the image from file path 35 | * warning: 36 | */ 37 | open class FileLoadTheImage : LoadTheImage { 38 | @Composable 39 | override fun load(data: DataToBeLoaded): Painter? { 40 | val file = if (data.data is String) 41 | remember { File(data.data) } 42 | else if (data.data is File) 43 | data.data 44 | else 45 | return null 46 | val painter = remember(file.absolutePath) { AsyncImagePainter() } 47 | LaunchedEffect(file.absolutePath) { 48 | withContext(Dispatchers.IO) { 49 | var byteArray: ByteArray? = LoadTheImageManager.memoryCache.getCache(file.absolutePath) 50 | if (byteArray == null) { 51 | try { 52 | val array = file.readBytes() 53 | if (array.isEmpty()) { 54 | painter.imageBitmap.value = 55 | LoadTheImageManager.loadResourceImageBitmap(data.errorImagePath) 56 | return@withContext 57 | } 58 | LoadTheImageManager.memoryCache.saveCache(file.absolutePath, array) 59 | byteArray = array 60 | } catch (e: Exception) { 61 | e.println() 62 | painter.imageBitmap.value = 63 | LoadTheImageManager.loadResourceImageBitmap(data.errorImagePath) 64 | return@withContext 65 | } 66 | } 67 | painter.imageBitmap.value = 68 | try { 69 | LoadTheImageManager.painterCreator.createImageBitmap(byteArray) 70 | } catch (e: Exception) { 71 | e.println() 72 | LoadTheImageManager.createErrorImageBitmap(data) 73 | } 74 | } 75 | } 76 | return painter 77 | } 78 | 79 | override fun canLoad(data: DataToBeLoaded): Boolean { 80 | val file = if (data.data is String) 81 | File(data.data) 82 | else if (data.data is File) 83 | data.data 84 | else 85 | return false 86 | if (file.exists() && file.isFile) 87 | return true 88 | return false 89 | } 90 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/loader/image/HttpLoadTheImage.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.loader.image 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.runtime.LaunchedEffect 21 | import androidx.compose.runtime.remember 22 | import androidx.compose.ui.graphics.painter.Painter 23 | import androidx.compose.ui.graphics.toComposeImageBitmap 24 | import com.lt.load_the_image.LoadTheImageManager 25 | import com.lt.load_the_image.loader.DataToBeLoaded 26 | import com.lt.load_the_image.loader.LoadTheImage 27 | import com.lt.load_the_image.painter.AsyncImagePainter 28 | import kotlinx.coroutines.Dispatchers 29 | import kotlinx.coroutines.withContext 30 | import org.jetbrains.skia.Image 31 | 32 | /** 33 | * creator: lt 2022/4/8 lt.dygzs@qq.com 34 | * effect : Load the image from network 35 | * warning: 36 | */ 37 | open class HttpLoadTheImage : LoadTheImage { 38 | 39 | @Composable 40 | override fun load(data: DataToBeLoaded): Painter? { 41 | val url = data.data as? String ?: return null 42 | val painter = remember(url) { AsyncImagePainter() } 43 | LaunchedEffect(url) { 44 | withContext(Dispatchers.IO) { 45 | //Use cache 46 | var byteArray: ByteArray? = LoadTheImageManager.memoryCache.getCache(url) 47 | if (byteArray == null) { 48 | byteArray = LoadTheImageManager.fileCache.getCache(url) 49 | if (byteArray != null) { 50 | LoadTheImageManager.memoryCache.saveCache(url, byteArray) 51 | } 52 | } 53 | //Http load 54 | if (byteArray == null) { 55 | val placeholderResource = data.placeholderResource 56 | if (placeholderResource.isNotEmpty()) 57 | painter.imageBitmap.value = 58 | LoadTheImageManager.loadResourceImageBitmap(placeholderResource) 59 | //Load with http 60 | byteArray = LoadTheImageManager.httpLoader.load(url) 61 | if (byteArray == null) { 62 | painter.imageBitmap.value = LoadTheImageManager.createErrorImageBitmap(data) 63 | return@withContext 64 | } 65 | LoadTheImageManager.memoryCache.saveCache(url, byteArray) 66 | LoadTheImageManager.fileCache.saveCache(url, byteArray) 67 | } 68 | val imageBitmap = Image.makeFromEncoded(byteArray).toComposeImageBitmap() 69 | painter.imageBitmap.value = imageBitmap 70 | } 71 | } 72 | return painter 73 | } 74 | 75 | override fun canLoad(data: DataToBeLoaded): Boolean { 76 | val url = data.data as? String ?: return false 77 | if (url.startsWith("http://") || url.startsWith("https://")) 78 | return true 79 | return false 80 | } 81 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/loader/image/ImageLoadTheImage.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.loader.image 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.runtime.remember 21 | import androidx.compose.ui.graphics.painter.BitmapPainter 22 | import androidx.compose.ui.graphics.painter.Painter 23 | import androidx.compose.ui.graphics.toComposeImageBitmap 24 | import com.lt.load_the_image.loader.DataToBeLoaded 25 | import com.lt.load_the_image.loader.LoadTheImage 26 | import org.jetbrains.skia.Image 27 | 28 | /** 29 | * creator: lt 2022/6/1 lt.dygzs@qq.com 30 | * effect : Load the image from [Image] 31 | * warning: 32 | */ 33 | class ImageLoadTheImage : LoadTheImage { 34 | @Composable 35 | override fun load(data: DataToBeLoaded): Painter? { 36 | val image = data.data as? Image ?: return null 37 | return remember(image) { BitmapPainter(image.toComposeImageBitmap()) } 38 | } 39 | 40 | override fun canLoad(data: DataToBeLoaded): Boolean { 41 | return data.data is Image 42 | } 43 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/loader/image/InputStreamLoadTheImage.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.loader.image 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.runtime.LaunchedEffect 21 | import androidx.compose.runtime.remember 22 | import androidx.compose.ui.graphics.painter.Painter 23 | import androidx.compose.ui.res.loadImageBitmap 24 | import com.lt.load_the_image.LoadTheImageManager 25 | import com.lt.load_the_image.loader.DataToBeLoaded 26 | import com.lt.load_the_image.loader.LoadTheImage 27 | import com.lt.load_the_image.painter.AsyncImagePainter 28 | import com.lt.load_the_image.util.println 29 | import kotlinx.coroutines.Dispatchers 30 | import kotlinx.coroutines.withContext 31 | import java.io.InputStream 32 | 33 | /** 34 | * creator: lt 2022/6/1 lt.dygzs@qq.com 35 | * effect : Load the image from [InputStream] of image 36 | * warning: 37 | */ 38 | class InputStreamLoadTheImage : LoadTheImage { 39 | @Composable 40 | override fun load(data: DataToBeLoaded): Painter? { 41 | val inputStream = data.data as? InputStream ?: return null 42 | val painter = remember(inputStream) { AsyncImagePainter() } 43 | LaunchedEffect(inputStream) { 44 | withContext(Dispatchers.IO) { 45 | val placeholderResource = data.placeholderResource 46 | if (placeholderResource.isNotEmpty()) 47 | painter.imageBitmap.value = 48 | LoadTheImageManager.loadResourceImageBitmap(placeholderResource) 49 | val imageBitmap = loadImageBitmap(inputStream) 50 | if (data.isAutoCloseStream) 51 | try { 52 | inputStream.close() 53 | } catch (e: Exception) { 54 | e.println() 55 | } 56 | painter.imageBitmap.value = imageBitmap 57 | } 58 | } 59 | return painter 60 | } 61 | 62 | override fun canLoad(data: DataToBeLoaded): Boolean { 63 | return data.data is InputStream 64 | } 65 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/loader/image/ResourcesLoadTheImage.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.loader.image 18 | 19 | import androidx.compose.runtime.Composable 20 | import androidx.compose.ui.ExperimentalComposeUiApi 21 | import androidx.compose.ui.graphics.painter.Painter 22 | import androidx.compose.ui.res.ClassLoaderResourceLoader 23 | import androidx.compose.ui.res.painterResource 24 | import com.lt.load_the_image.loader.DataToBeLoaded 25 | import com.lt.load_the_image.loader.LoadTheImage 26 | import com.lt.load_the_image.util.println 27 | 28 | /** 29 | * creator: lt 2022/4/8 lt.dygzs@qq.com 30 | * effect : Load the image from resources path, reference [painterResource] 31 | * warning: 32 | */ 33 | open class ResourcesLoadTheImage : LoadTheImage { 34 | @Composable 35 | override fun load(data: DataToBeLoaded): Painter? { 36 | return painterResource(data.data as? String ?: return null) 37 | } 38 | 39 | @OptIn(ExperimentalComposeUiApi::class) 40 | override fun canLoad(data: DataToBeLoaded): Boolean { 41 | val url = data.data as? String 42 | if (url.isNullOrEmpty()) return false 43 | //Check reference [ClassLoaderResourceLoader] 44 | val contextClassLoader = Thread.currentThread().contextClassLoader ?: return false 45 | val resource = try { 46 | contextClassLoader.getResourceAsStream(url) 47 | ?: (::ClassLoaderResourceLoader.javaClass).getResourceAsStream(url) 48 | ?: return false 49 | } catch (e: Exception) { 50 | e.println() 51 | return false 52 | } 53 | resource.close() 54 | return true 55 | } 56 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/painter/AsyncImagePainter.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.painter 18 | 19 | import androidx.compose.runtime.MutableState 20 | import androidx.compose.runtime.mutableStateOf 21 | import androidx.compose.ui.geometry.Size 22 | import androidx.compose.ui.graphics.ImageBitmap 23 | import androidx.compose.ui.graphics.drawscope.DrawScope 24 | import androidx.compose.ui.graphics.painter.Painter 25 | import androidx.compose.ui.unit.IntSize 26 | import kotlin.math.roundToInt 27 | 28 | /** 29 | * creator: lt 2022/5/31 lt.dygzs@qq.com 30 | * effect : Image painter with async. 31 | * warning: 32 | */ 33 | open class AsyncImagePainter( 34 | imageBitmap: ImageBitmap? = null 35 | ) : Painter() { 36 | val imageBitmap: MutableState = mutableStateOf(imageBitmap) 37 | 38 | override val intrinsicSize: Size 39 | get() { 40 | val bitmap = imageBitmap.value ?: return Size(1f, 1f) 41 | return Size(bitmap.width.toFloat(), bitmap.height.toFloat()) 42 | } 43 | 44 | override fun DrawScope.onDraw() { 45 | val bitmap = imageBitmap.value ?: return 46 | drawImage( 47 | bitmap, 48 | dstSize = IntSize( 49 | this@onDraw.size.width.roundToInt(), 50 | this@onDraw.size.height.roundToInt() 51 | ) 52 | ) 53 | } 54 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/painter/DefaultPainterCreator.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.painter 18 | 19 | import androidx.compose.ui.graphics.ImageBitmap 20 | import androidx.compose.ui.graphics.painter.BitmapPainter 21 | import androidx.compose.ui.graphics.painter.Painter 22 | import androidx.compose.ui.graphics.toComposeImageBitmap 23 | import org.jetbrains.skia.Image.Companion.makeFromEncoded 24 | 25 | 26 | /** 27 | * creator: lt 2022/4/8 lt.dygzs@qq.com 28 | * effect : Default PainterCreator 29 | * warning: 30 | */ 31 | open class DefaultPainterCreator : PainterCreator { 32 | override fun create(byteArray: ByteArray): Painter = BitmapPainter(createImageBitmap(byteArray)) 33 | 34 | override fun createImageBitmap(byteArray: ByteArray): ImageBitmap = 35 | makeFromEncoded(byteArray).toComposeImageBitmap() 36 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/painter/EmptyImagePainter.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.painter 18 | 19 | import androidx.compose.ui.geometry.Size 20 | import androidx.compose.ui.graphics.drawscope.DrawScope 21 | import androidx.compose.ui.graphics.painter.Painter 22 | 23 | /** 24 | * creator: lt 2022/6/1 lt.dygzs@qq.com 25 | * effect : Empty image painter 26 | * warning: 27 | */ 28 | class EmptyImagePainter : Painter() { 29 | override val intrinsicSize: Size 30 | get() = Size(1f, 1f) 31 | 32 | override fun DrawScope.onDraw() { 33 | } 34 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/painter/PainterCreator.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.painter 18 | 19 | import androidx.compose.ui.graphics.ImageBitmap 20 | import androidx.compose.ui.graphics.painter.Painter 21 | 22 | /** 23 | * creator: lt 2022/4/8 lt.dygzs@qq.com 24 | * effect : Create [Painter] from ByteArray 25 | * warning: 26 | */ 27 | interface PainterCreator { 28 | 29 | /** 30 | * Create [Painter] from [byteArray] 31 | */ 32 | fun create(byteArray: ByteArray): Painter 33 | 34 | /** 35 | * Create [ImageBitmap] from [byteArray] 36 | */ 37 | fun createImageBitmap(byteArray: ByteArray): ImageBitmap 38 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/util/MD5.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.util 18 | 19 | import java.lang.Exception 20 | import java.lang.StringBuilder 21 | import java.security.MessageDigest 22 | import java.security.NoSuchAlgorithmException 23 | 24 | object MD5 { 25 | // 全局数组 26 | private val strDigits = arrayOf( 27 | "0", "1", "2", "3", "4", "5", 28 | "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" 29 | ) 30 | 31 | // 返回形式为数字跟字符串 32 | private fun byteToArrayString(bByte: Byte): String { 33 | var iRet = bByte.toInt() 34 | // System.out.println("iRet="+iRet); 35 | if (iRet < 0) { 36 | iRet += 256 37 | } 38 | val iD1 = iRet / 16 39 | val iD2 = iRet % 16 40 | return strDigits[iD1] + strDigits[iD2] 41 | } 42 | 43 | // 返回形式只为数字 44 | private fun byteToNum(bByte: Byte): String { 45 | var iRet = bByte.toInt() 46 | println("iRet1=$iRet") 47 | if (iRet < 0) { 48 | iRet += 256 49 | } 50 | return iRet.toString() 51 | } 52 | 53 | // 转换字节数组为16进制字串 54 | private fun byteToString(bByte: ByteArray): String { 55 | val sBuffer = StringBuilder() 56 | for (b in bByte) { 57 | sBuffer.append(byteToArrayString(b)) 58 | } 59 | return sBuffer.toString() 60 | } 61 | 62 | fun GetMD5Code(strObj: String): String? { 63 | var resultString: String? = null 64 | try { 65 | resultString = strObj 66 | val md = MessageDigest.getInstance("MD5") 67 | // md.digest() 该函数返回值为存放哈希值结果的byte数组 68 | resultString = byteToString(md.digest(strObj.toByteArray())) 69 | } catch (ex: NoSuchAlgorithmException) { 70 | println("MD5.GetMD5Code 54 : $ex") 71 | } 72 | return resultString 73 | } 74 | 75 | fun getMessageDigest(buffer: ByteArray?): String? { 76 | val hexDigits = charArrayOf( 77 | '0', 78 | '1', 79 | '2', 80 | '3', 81 | '4', 82 | '5', 83 | '6', 84 | '7', 85 | '8', 86 | '9', 87 | 'a', 88 | 'b', 89 | 'c', 90 | 'd', 91 | 'e', 92 | 'f' 93 | ) 94 | return try { 95 | val mdTemp = MessageDigest.getInstance("MD5") 96 | mdTemp.update(buffer) 97 | val md = mdTemp.digest() 98 | val j = md.size 99 | val str = CharArray(j * 2) 100 | var k = 0 101 | for (byte0 in md) { 102 | str[k++] = hexDigits[byte0.toInt() ushr 4 and 0xf] 103 | str[k++] = hexDigits[byte0.toInt() and 0xf] 104 | } 105 | String(str) 106 | } catch (e: Exception) { 107 | println("MD5.getMessageDigest 79 : $e") 108 | null 109 | } 110 | } 111 | } -------------------------------------------------------------------------------- /load-the-image/src/main/java/com/lt/load_the_image/util/PrintException.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.lt.load_the_image.util 18 | 19 | /** 20 | * Print [Exception] 21 | */ 22 | fun Exception.println() { 23 | RuntimeException("Load-The-Image inner exception", this).printStackTrace() 24 | } 25 | -------------------------------------------------------------------------------- /md_resource/custom_LoadTheImage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ltttttttttttt/load-the-image/b0df20b73de63c7d2305676dc04507f7bc3a4c84/md_resource/custom_LoadTheImage.png -------------------------------------------------------------------------------- /md_resource/example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ltttttttttttt/load-the-image/b0df20b73de63c7d2305676dc04507f7bc3a4c84/md_resource/example.png -------------------------------------------------------------------------------- /md_resource/formats.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ltttttttttttt/load-the-image/b0df20b73de63c7d2305676dc04507f7bc3a4c84/md_resource/formats.png -------------------------------------------------------------------------------- /md_resource/resource_location.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ltttttttttttt/load-the-image/b0df20b73de63c7d2305676dc04507f7bc3a4c84/md_resource/resource_location.png -------------------------------------------------------------------------------- /md_resource/use_placeholder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ltttttttttttt/load-the-image/b0df20b73de63c7d2305676dc04507f7bc3a4c84/md_resource/use_placeholder.png -------------------------------------------------------------------------------- /settings.gradle.kts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright lt 2023 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | pluginManagement { 18 | repositories { 19 | google() 20 | jcenter() 21 | gradlePluginPortal() 22 | mavenCentral() 23 | maven("https://maven.pkg.jetbrains.space/public/p/compose/dev") 24 | } 25 | 26 | } 27 | rootProject.name = "LoadTheImageSample" 28 | 29 | 30 | include(":android") 31 | include(":desktop") 32 | include(":common") 33 | 34 | include(":load-the-image") 35 | --------------------------------------------------------------------------------