Skip to content

Commit efb0d4a

Browse files
committed
修改bug
增加Glide配置接口
1 parent 1e841ad commit efb0d4a

File tree

7 files changed

+94
-43
lines changed

7 files changed

+94
-43
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,20 @@
4444
}
4545
}
4646
}
47+
48+
//代替AppGlideModule实现来修改glide配置接口
49+
AppGlideModuleIml.options = object : IAppGlideOptions {
50+
override fun applyOptions(context: Context, builder: GlideBuilder) {
51+
//修改缓存大小等
52+
Log.d("TAG", "applyOptions")
53+
}
54+
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
55+
//修改注册组件 例如 okhttp 注意如果修改可能会导致进度丢失
56+
Log.d("TAG", "registerComponents")
57+
}
58+
}
4759
```
48-
60+
4961
4. 其他扩展函数和效果
5062

5163
```kotlin
@@ -269,7 +281,7 @@ iv_8.load(url2, height = 800)
269281

270282
[forJrking/ImageExt: 基于Glide封装ImageView加载图片资源的扩展函数集 (github.com)](https://github.com/forJrking/ImageExt)
271283

272-
- 由于使用到基于okhttp的下载进度管理所以使用了 glide 的@GlideModule配置方法,这样可能会和你项目自定义配置有冲突,目前只能拉代码自己修改,然后依赖Module方式了。如有更好方式联系我改进。
284+
- ~~由于使用到基于okhttp的下载进度管理所以使用了 glide 的@GlideModule配置方法,这样可能会和你项目自定义配置有冲突,目前只能拉代码自己修改,然后依赖Module方式了。如有更好方式联系我改进。~~
273285

274286
- Android图片加载库常见的只有几种,其他库可以自行参考实现。Kotlin真香!!!
275287

app/src/main/java/me/demo/sample/MainActivity.kt

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package me.demo.sample
22

33
import android.Manifest
4+
import android.content.Context
45
import android.graphics.Color
56
import android.os.Bundle
67
import android.util.Log
78
import androidx.appcompat.app.AppCompatActivity
89
import android.view.View
910
import android.widget.Toast
11+
import com.bumptech.glide.Glide
12+
import com.bumptech.glide.GlideBuilder
13+
import com.bumptech.glide.Registry
1014
import com.github.forjrking.image.*
1115
import com.github.forjrking.image.core.ImageOptions
16+
import com.github.forjrking.image.glide.AppGlideModuleIml
1217
import com.github.forjrking.image.glide.GlideImageLoader
18+
import com.github.forjrking.image.glide.IAppGlideOptions
1319
import com.github.forjrking.image.glide.transformation.CircleWithBorderTransformation
1420
import com.github.forjrking.image.glide.transformation.GrayscaleTransformation
1521
import kotlinx.android.synthetic.main.activity_main.*
@@ -28,18 +34,34 @@ import pub.devrel.easypermissions.EasyPermissions
2834
*/
2935
class MainActivity : AppCompatActivity() {
3036
var url1 = "https://t7.baidu.com/it/u=3713375227,571533122&fm=193&f=GIF"
31-
var url3 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.huabanimg.com%2F3fee54d0b2e0b7a132319a8e104f5fdc2edd3d35d03ee-93Jmdq_fw658&refer=http%3A%2F%2Fhbimg.huabanimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1619934437&t=2c758765592bf56ec7eb4ad1912a2830"
37+
var url3 =
38+
"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.huabanimg.com%2F3fee54d0b2e0b7a132319a8e104f5fdc2edd3d35d03ee-93Jmdq_fw658&refer=http%3A%2F%2Fhbimg.huabanimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1619934437&t=2c758765592bf56ec7eb4ad1912a2830"
3239
var url4 = "http://img.mp.itc.cn/upload/20170311/33f2b7f7ffb04ecb81e42405e20b3fdc_th.gif"
3340
override fun onCreate(savedInstanceState: Bundle?) {
3441
super.onCreate(savedInstanceState)
3542
setContentView(R.layout.activity_main)
43+
44+
AppGlideModuleIml.options = object : IAppGlideOptions {
45+
override fun applyOptions(context: Context, builder: GlideBuilder) {
46+
//修改缓存大小等
47+
Log.d("TAG", "applyOptions")
48+
}
49+
50+
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
51+
//修改注册组件 例如 okhttp 注意如果修改可能会导致进度丢失
52+
Log.d("TAG", "registerComponents")
53+
}
54+
55+
}
56+
57+
Log.d("TAG", "clearDiskCache")
3658
GlobalScope.launch(Dispatchers.IO) {
3759
GlideImageLoader.clearDiskCache(applicationContext)
3860
}
3961
GlideImageLoader.clearMemory(applicationContext)
4062

4163
ImageOptions.DrawableOptions.setDefault {
42-
placeHolderResId = R.drawable.ic_launcher_background
64+
placeHolderResId = R.drawable.ic_launcher_background
4365
errorResId = R.color.gray
4466
}
4567

@@ -49,20 +71,20 @@ class MainActivity : AppCompatActivity() {
4971
private fun initView() {
5072
circleProgressView.visibility = View.VISIBLE
5173
iv_0.postDelayed(
52-
{
53-
iv_0.load(url3) {
54-
progressListener { isComplete, percentage, bytesRead, totalBytes ->
55-
// 跟踪进度
56-
Log.d("TAG", "onProgress: $percentage")
57-
if (isComplete) {
58-
circleProgressView.visibility = View.GONE
59-
} else {
60-
circleProgressView.progress = percentage
61-
}
74+
{
75+
iv_0.load(url3) {
76+
progressListener { isComplete, percentage, bytesRead, totalBytes ->
77+
// 跟踪进度
78+
Log.d("TAG", "onProgress: $percentage")
79+
if (isComplete) {
80+
circleProgressView.visibility = View.GONE
81+
} else {
82+
circleProgressView.progress = percentage
6283
}
6384
}
85+
}
6486

65-
}, 500
87+
}, 500
6688
)
6789

6890
iv_1.setOnClickListener { downloadImage() }
@@ -102,7 +124,8 @@ class MainActivity : AppCompatActivity() {
102124
// iv_10.loadImage(url2, placeHolder = R.color.green)
103125
iv_8.load(url1) {
104126
placeHolderResId = R.color.black
105-
transformation = arrayOf(GrayscaleTransformation(), CircleWithBorderTransformation(borderWidth = 0, borderColor = 0))
127+
transformation = arrayOf(GrayscaleTransformation(),
128+
CircleWithBorderTransformation(borderWidth = 0, borderColor = 0))
106129
progressListener { isComplete, percentage, bytesRead, totalBytes ->
107130
//加载进度
108131
}
@@ -127,18 +150,19 @@ class MainActivity : AppCompatActivity() {
127150
if (hasStoragePermission()) {
128151
GlobalScope.launch {
129152
try {
130-
val downloadImage = GlideImageLoader.downloadImage(context = this@MainActivity, imgUrl = url1)
153+
val downloadImage =
154+
GlideImageLoader.downloadImage(context = this@MainActivity, imgUrl = url1)
131155
Log.d("TAG", "downloadImage: ${downloadImage?.absolutePath}")
132156
} catch (e: Exception) {
133157
e.printStackTrace()
134158
}
135159
}
136160
} else {
137161
EasyPermissions.requestPermissions(
138-
this,
139-
getString(R.string.need_write_external),
140-
WRITE_EXTERNAL_PERM,
141-
Manifest.permission.WRITE_EXTERNAL_STORAGE)
162+
this,
163+
getString(R.string.need_write_external),
164+
WRITE_EXTERNAL_PERM,
165+
Manifest.permission.WRITE_EXTERNAL_STORAGE)
142166
}
143167
}
144168

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildscript {
99
maven { url 'https://jitpack.io' }
1010
}
1111
dependencies {
12-
classpath 'com.android.tools.build:gradle:4.0.2'
12+
classpath 'com.android.tools.build:gradle:4.1.2'
1313
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
1414
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1515
// NOTE: Do not place your application dependencies here; they belong

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip

libImage/src/main/java/com/github/forjrking/image/core/ImageOptions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ImageOptions {
6666
var errorDrawable: Drawable?
6767
get() = TODO()
6868
set(value) {
69-
setDrawableOptions { placeHolderDrawable = value }
69+
setDrawableOptions { errorDrawable = value }
7070
}
7171

7272
/** DES: fallback */

libImage/src/main/java/com/github/forjrking/image/glide/GlideModule.kt renamed to libImage/src/main/java/com/github/forjrking/image/glide/AppGlideModuleIml.kt

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import com.bumptech.glide.load.model.GlideUrl
99
import com.bumptech.glide.module.AppGlideModule
1010
import com.github.forjrking.image.glide.http.OkHttpUrlLoader
1111
import com.github.forjrking.image.glide.progress.ProgressManager
12+
import com.github.forjrking.image.glide.progress.ProgressManager.glideProgressInterceptor
13+
import okhttp3.OkHttpClient
1214
import java.io.InputStream
1315

1416
@GlideModule(glideName = "IGlideModule")
15-
class GlideModule : AppGlideModule() {
17+
class AppGlideModuleIml : AppGlideModule() {
1618
override fun applyOptions(context: Context, builder: GlideBuilder) {
19+
options?.applyOptions(context, builder)
1720
// builder.setDiskCache(InternalCacheDiskCacheFactory(context, "Glide", IMAGE_DISK_CACHE_MAX_SIZE.toLong()))
1821
// val calculator = MemorySizeCalculator.Builder(context).build()
1922
// val defaultMemoryCacheSize = calculator.memoryCacheSize
@@ -22,20 +25,31 @@ class GlideModule : AppGlideModule() {
2225
// val customBitmapPoolSize = (1.2 * defaultBitmapPoolSize).toInt()
2326
// builder.setMemoryCache(LruResourceCache(customMemoryCacheSize.toLong()))
2427
// builder.setBitmapPool(LruBitmapPool(customBitmapPoolSize.toLong()))
28+
2529
}
2630

2731
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
28-
registry.replace(GlideUrl::class.java, InputStream::class.java, OkHttpUrlLoader.Factory(ProgressManager.okHttpClient))
32+
//下载进度的实现
33+
registry.replace(GlideUrl::class.java,
34+
InputStream::class.java,
35+
OkHttpUrlLoader.Factory(OkHttpClient.Builder().glideProgressInterceptor().build()))
36+
options?.registerComponents(context, glide, registry)
2937
}
3038

3139
override fun isManifestParsingEnabled(): Boolean {
32-
return false
40+
return options?.isManifestParsingEnabled() ?: false
3341
}
3442

3543
companion object {
36-
/**
37-
* 图片缓存文件最大值为100Mb
38-
*/
39-
const val IMAGE_DISK_CACHE_MAX_SIZE = 100 * 1024 * 1024
44+
/**把Glide配置方法进行暴露接口*/
45+
var options: IAppGlideOptions? = null
4046
}
47+
}
48+
49+
/**把Glide配置方法进行保留*/
50+
interface IAppGlideOptions {
51+
fun isManifestParsingEnabled(): Boolean = false
52+
fun applyOptions(context: Context, builder: GlideBuilder)
53+
/**glide 下载进度的主要逻辑 需要在OkHttpClient.Builder().glideProgressInterceptor()**/
54+
fun registerComponents(context: Context, glide: Glide, registry: Registry)
4155
}

libImage/src/main/java/com/github/forjrking/image/glide/progress/ProgressManager.kt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,20 @@ import java.util.concurrent.ConcurrentHashMap
1616
object ProgressManager {
1717
private val listenersMap = ConcurrentHashMap<String, OnProgressListener>()
1818

19-
var okHttpClient: Call.Factory = OkHttpClient
20-
.Builder()
21-
.addNetworkInterceptor { chain: Interceptor.Chain ->
22-
val request = chain.request()
23-
val response = chain.proceed(request)
24-
response.newBuilder().run {
25-
val body = response.body()
26-
if (body != null) {
27-
this.body(ProgressResponseBody(request.url().toString(), LISTENER, body))
28-
}
29-
this.build()
19+
/**glide 下载进度的主要逻辑 需要在GlideModule注入*/
20+
fun OkHttpClient.Builder.glideProgressInterceptor(): OkHttpClient.Builder =
21+
this.addNetworkInterceptor { chain: Interceptor.Chain ->
22+
val request = chain.request()
23+
val response = chain.proceed(request)
24+
response.newBuilder().run {
25+
val body = response.body()
26+
if (body != null) {
27+
this.body(ProgressResponseBody(request.url().toString(), LISTENER, body))
3028
}
31-
}.build()
29+
this.build()
30+
}
31+
}
32+
3233

3334
private val LISTENER = object : InternalProgressListener {
3435
override fun onProgress(url: String, bytesRead: Long, totalBytes: Long) {

0 commit comments

Comments
 (0)