11package me.demo.sample
22
33import android.Manifest
4+ import android.content.Context
45import android.graphics.Color
56import android.os.Bundle
67import android.util.Log
78import androidx.appcompat.app.AppCompatActivity
89import android.view.View
910import android.widget.Toast
11+ import com.bumptech.glide.Glide
12+ import com.bumptech.glide.GlideBuilder
13+ import com.bumptech.glide.Registry
1014import com.github.forjrking.image.*
1115import com.github.forjrking.image.core.ImageOptions
16+ import com.github.forjrking.image.glide.AppGlideModuleIml
1217import com.github.forjrking.image.glide.GlideImageLoader
18+ import com.github.forjrking.image.glide.IAppGlideOptions
1319import com.github.forjrking.image.glide.transformation.CircleWithBorderTransformation
1420import com.github.forjrking.image.glide.transformation.GrayscaleTransformation
1521import kotlinx.android.synthetic.main.activity_main.*
@@ -28,18 +34,34 @@ import pub.devrel.easypermissions.EasyPermissions
2834 */
2935class 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
0 commit comments