1
1
package com.yy.mobile.svga.glideplugin.demo
2
2
3
+ import android.annotation.SuppressLint
3
4
import android.graphics.Color
4
5
import android.graphics.Paint
5
6
import android.os.Bundle
6
- import androidx.annotation.RawRes
7
- import androidx.appcompat.app.AppCompatActivity
8
- import android.text.Layout
9
- import android.text.Spannable
10
- import android.text.SpannableStringBuilder
11
- import android.text.StaticLayout
12
- import android.text.TextPaint
7
+ import android.text.*
13
8
import android.text.style.ForegroundColorSpan
14
9
import android.util.Log
15
10
import android.view.View
11
+ import androidx.annotation.RawRes
12
+ import androidx.appcompat.app.AppCompatActivity
16
13
import com.bumptech.glide.Glide
17
14
import com.opensource.svgaplayer.SVGADynamicEntity
18
15
import com.opensource.svgaplayer.glideplugin.SVGATarget
@@ -27,47 +24,40 @@ import java.net.URL
27
24
*/
28
25
class MainActivity : AppCompatActivity () {
29
26
30
- private val svgaFiles = listOf (
31
- " alarm" ,
32
- " angel" ,
33
- " EmptyState" ,
34
- " heartbeat" ,
35
- " posche" ,
36
- " rose_1.5.0" ,
37
- " rose_2.0.0" )
38
-
39
- private val svgaResources = listOf (
40
- R .raw.alarm,
41
- R .raw.angel,
42
- R .raw.emptystate,
43
- R .raw.heartbeat,
44
- R .raw.posche,
45
- R .raw.rose_1_5,
46
- R .raw.rose_2_0
47
- )
48
-
49
- private var curIdx = 0
27
+ private lateinit var source: GlideSource
50
28
51
29
override fun onCreate (savedInstanceState : Bundle ? ) {
52
30
super .onCreate(savedInstanceState)
53
31
setContentView(R .layout.activity_main)
32
+
33
+ fun load () {
34
+ source = if (rb_group.checkedRadioButtonId == R .id.rb_svga) {
35
+ SVGASource ()
36
+ } else {
37
+ GifSource ()
38
+ }
39
+ loadFromRes(rb_group)
40
+ }
41
+
42
+ rb_group.setOnCheckedChangeListener { _, _ ->
43
+ load()
44
+ }
45
+ load()
54
46
}
55
47
56
- fun loadSVGAFromNetwork (v : View ) {
57
- val url = " https://github.com/YvesCheung/SVGAGlidePlugin/blob/master/" +
58
- " app/src/main/assets/${svgaFiles[curIdx]} .svga?raw=true"
59
- loadSVGAFromUrl(url)
48
+ fun loadFromNetwork (v : View ) {
49
+ val (_, url) = source.getNetworkUrl()
50
+ loadFromUrl(url)
60
51
}
61
52
62
- fun loadSVGAFromAssets (v : View ) {
63
- val fileUrl = " file:///android_asset/ ${svgaFiles[curIdx]} .svga "
64
- loadSVGAFromUrl(fileUrl )
53
+ fun loadFromAssets (v : View ) {
54
+ val (_, file) = source.getLocalFile()
55
+ loadFromUrl(file )
65
56
}
66
57
67
- fun loadSVGAFromFile (v : View ) {
68
- val url = " https://github.com/YvesCheung/SVGAGlidePlugin/blob/master/" +
69
- " app/src/main/assets/${svgaFiles[curIdx]} .svga?raw=true"
70
- val file = File (externalCacheDir, svgaFiles[curIdx].replace(" /" , " _" ))
58
+ fun loadFromFile (v : View ) {
59
+ val (_, url, fileName) = source.getNetworkUrl()
60
+ val file = File (externalCacheDir, fileName.replace(" /" , " _" ))
71
61
val buffer = ByteArray (1 * 1024 * 1024 )
72
62
Thread {
73
63
try {
@@ -85,22 +75,22 @@ class MainActivity : AppCompatActivity() {
85
75
}
86
76
}
87
77
88
- v.post { loadSVGAFromUrl (file.absolutePath) }
78
+ v.post { loadFromUrl (file.absolutePath) }
89
79
} catch (e: Exception ) {
90
80
Log .e(" Yves" , e.message, e)
91
81
}
92
82
}.start()
93
83
}
94
84
95
- fun loadSVGAFromRes (v : View ) {
96
- @RawRes val id = svgaResources[curIdx]
97
- tv_assets_name.text = this .resources.getResourceEntryName(id)
85
+ @SuppressLint(" SetTextI18n" )
86
+ fun loadFromRes (v : View ) {
87
+ val (_, id) = source.getResId()
88
+ tv_assets_name.text =
89
+ " R.${this .resources.getResourceTypeName(id)} .${this .resources.getResourceEntryName(id)} "
98
90
Glide .with (this ).load(id).into(iv_img)
99
- curIdx = ++ curIdx % svgaResources.size
100
91
}
101
92
102
- private fun loadSVGAFromUrl (url : String ) {
103
- curIdx = ++ curIdx % svgaFiles.size
93
+ private fun loadFromUrl (url : String ) {
104
94
tv_assets_name.text = url
105
95
Glide .with (this ).load(url).into(iv_img)
106
96
}
@@ -115,21 +105,26 @@ class MainActivity : AppCompatActivity() {
115
105
private fun requestDynamicItemWithSpannableText (): SVGADynamicEntity {
116
106
val dynamicEntity = SVGADynamicEntity ()
117
107
val spannableStringBuilder = SpannableStringBuilder (" Pony 送了一打风油精给主播" )
118
- spannableStringBuilder.setSpan(ForegroundColorSpan (Color .YELLOW ), 0 , 4 , Spannable .SPAN_INCLUSIVE_INCLUSIVE )
108
+ spannableStringBuilder.setSpan(
109
+ ForegroundColorSpan (Color .YELLOW ), 0 , 4 , Spannable .SPAN_INCLUSIVE_INCLUSIVE
110
+ )
119
111
val textPaint = TextPaint ()
120
112
textPaint.color = Color .WHITE
121
113
textPaint.textSize = 28f
122
- dynamicEntity.setDynamicText(StaticLayout (
123
- spannableStringBuilder,
124
- 0 ,
125
- spannableStringBuilder.length,
126
- textPaint,
127
- 0 ,
128
- Layout .Alignment .ALIGN_CENTER ,
129
- 1.0f ,
130
- 0.0f ,
131
- false
132
- ), " banner" )
114
+ dynamicEntity.setDynamicText(
115
+ StaticLayout (
116
+ spannableStringBuilder,
117
+ 0 ,
118
+ spannableStringBuilder.length,
119
+ textPaint,
120
+ 0 ,
121
+ Layout .Alignment .ALIGN_CENTER ,
122
+ 1.0f ,
123
+ 0.0f ,
124
+ false
125
+ ),
126
+ " banner"
127
+ )
133
128
dynamicEntity.setDynamicDrawer({ canvas, frameIndex ->
134
129
val aPaint = Paint ()
135
130
aPaint.color = Color .WHITE
@@ -138,4 +133,95 @@ class MainActivity : AppCompatActivity() {
138
133
}, " banner" )
139
134
return dynamicEntity
140
135
}
136
+
137
+ abstract class GlideSource {
138
+
139
+ private var curIdx = 0
140
+
141
+ @RawRes
142
+ protected abstract fun getResId (idx : Int ): Int
143
+
144
+ protected abstract fun getFileName (idx : Int ): String
145
+
146
+ open fun getLocalFile (): GlideModel <String > =
147
+ GlideModel (
148
+ curIdx,
149
+ " file:///android_asset/${getFileName(curIdx)} " ,
150
+ getFileName(curIdx++ )
151
+ )
152
+
153
+ open fun getNetworkUrl (): GlideModel <String > =
154
+ GlideModel (
155
+ curIdx,
156
+ " https://github.com/YvesCheung/SVGAGlidePlugin/blob/master/" +
157
+ " app/src/main/assets/${getFileName(curIdx)} ?raw=true" ,
158
+ getFileName(curIdx++ )
159
+ )
160
+
161
+ open fun getResId (): GlideModel <Int > =
162
+ GlideModel (
163
+ curIdx,
164
+ getResId(curIdx),
165
+ getFileName(curIdx++ )
166
+ )
167
+
168
+ data class GlideModel <Model >(
169
+ val index : Int ,
170
+ val model : Model ,
171
+ val fileName : String
172
+ )
173
+ }
174
+
175
+ class SVGASource : GlideSource () {
176
+
177
+ private val svgaFiles = listOf (
178
+ " alarm" ,
179
+ " angel" ,
180
+ " EmptyState" ,
181
+ " heartbeat" ,
182
+ " posche" ,
183
+ " rose_1.5.0" ,
184
+ " rose_2.0.0"
185
+ )
186
+
187
+ private val svgaResources = listOf (
188
+ R .raw.alarm,
189
+ R .raw.angel,
190
+ R .raw.emptystate,
191
+ R .raw.heartbeat,
192
+ R .raw.posche,
193
+ R .raw.rose_1_5,
194
+ R .raw.rose_2_0
195
+ )
196
+
197
+ @RawRes
198
+ override fun getResId (idx : Int ): Int = svgaResources[idx % svgaResources.size]
199
+
200
+ override fun getFileName (idx : Int ): String = " ${svgaFiles[idx % svgaFiles.size]} .svga"
201
+ }
202
+
203
+ class GifSource : GlideSource () {
204
+
205
+ private val gifFiles = listOf (
206
+ " giphy" ,
207
+ " pivot_wave" ,
208
+ " rotating_earth" ,
209
+ " sad_pikachu" ,
210
+ " tenor"
211
+ )
212
+
213
+ private val gifResources = listOf (
214
+ R .raw.giphy,
215
+ R .raw.pivot_wave,
216
+ R .raw.rotating_earth,
217
+ R .raw.sad_pikachu,
218
+ R .raw.tenor
219
+ )
220
+
221
+ @RawRes
222
+ override fun getResId (idx : Int ): Int = gifResources[idx % gifResources.size]
223
+
224
+ override fun getFileName (idx : Int ): String = " ${gifFiles[idx % gifFiles.size]} .gif"
225
+
226
+ }
141
227
}
0 commit comments