Skip to content

Commit e3d93e0

Browse files
committed
Add gif demo for Glide 4.13.2 ,see #12
1 parent 30c3e11 commit e3d93e0

File tree

2 files changed

+181
-68
lines changed

2 files changed

+181
-68
lines changed
Lines changed: 143 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package com.yy.mobile.svga.glideplugin.demo
22

3+
import android.annotation.SuppressLint
34
import android.graphics.Color
45
import android.graphics.Paint
56
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.*
138
import android.text.style.ForegroundColorSpan
149
import android.util.Log
1510
import android.view.View
11+
import androidx.annotation.RawRes
12+
import androidx.appcompat.app.AppCompatActivity
1613
import com.bumptech.glide.Glide
1714
import com.opensource.svgaplayer.SVGADynamicEntity
1815
import com.opensource.svgaplayer.glideplugin.SVGATarget
@@ -27,47 +24,40 @@ import java.net.URL
2724
*/
2825
class MainActivity : AppCompatActivity() {
2926

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
5028

5129
override fun onCreate(savedInstanceState: Bundle?) {
5230
super.onCreate(savedInstanceState)
5331
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()
5446
}
5547

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)
6051
}
6152

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)
6556
}
6657

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("/", "_"))
7161
val buffer = ByteArray(1 * 1024 * 1024)
7262
Thread {
7363
try {
@@ -85,22 +75,22 @@ class MainActivity : AppCompatActivity() {
8575
}
8676
}
8777

88-
v.post { loadSVGAFromUrl(file.absolutePath) }
78+
v.post { loadFromUrl(file.absolutePath) }
8979
} catch (e: Exception) {
9080
Log.e("Yves", e.message, e)
9181
}
9282
}.start()
9383
}
9484

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)}"
9890
Glide.with(this).load(id).into(iv_img)
99-
curIdx = ++curIdx % svgaResources.size
10091
}
10192

102-
private fun loadSVGAFromUrl(url: String) {
103-
curIdx = ++curIdx % svgaFiles.size
93+
private fun loadFromUrl(url: String) {
10494
tv_assets_name.text = url
10595
Glide.with(this).load(url).into(iv_img)
10696
}
@@ -115,21 +105,26 @@ class MainActivity : AppCompatActivity() {
115105
private fun requestDynamicItemWithSpannableText(): SVGADynamicEntity {
116106
val dynamicEntity = SVGADynamicEntity()
117107
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+
)
119111
val textPaint = TextPaint()
120112
textPaint.color = Color.WHITE
121113
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+
)
133128
dynamicEntity.setDynamicDrawer({ canvas, frameIndex ->
134129
val aPaint = Paint()
135130
aPaint.color = Color.WHITE
@@ -138,4 +133,95 @@ class MainActivity : AppCompatActivity() {
138133
}, "banner")
139134
return dynamicEntity
140135
}
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+
}
141227
}

app/src/main/res/layout/activity_main.xml

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,68 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout
3-
xmlns:android="http://schemas.android.com/apk/res/android"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
43
xmlns:tools="http://schemas.android.com/tools"
54
android:layout_width="match_parent"
65
android:layout_height="match_parent"
76
android:orientation="vertical"
87
tools:context=".MainActivity">
98

9+
<RadioGroup
10+
android:id="@+id/rb_group"
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"
13+
android:checkedButton="@+id/rb_svga"
14+
android:orientation="horizontal">
15+
16+
<RadioButton
17+
android:id="@+id/rb_svga"
18+
android:layout_width="wrap_content"
19+
android:layout_height="wrap_content"
20+
android:text="SVGA"
21+
tools:ignore="HardcodedText" />
22+
23+
<RadioButton
24+
android:id="@+id/rb_gif"
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:layout_marginStart="16dp"
28+
android:text="Gif"
29+
tools:ignore="HardcodedText" />
30+
</RadioGroup>
31+
1032
<Button
1133
android:layout_width="match_parent"
1234
android:layout_height="wrap_content"
13-
android:onClick="loadSVGAFromNetwork"
14-
android:text="load from network" />
35+
android:onClick="loadFromNetwork"
36+
android:text="load from network"
37+
tools:ignore="HardcodedText" />
1538

1639
<Button
1740
android:layout_width="match_parent"
1841
android:layout_height="wrap_content"
19-
android:onClick="loadSVGAFromAssets"
20-
android:text="load from assets" />
42+
android:onClick="loadFromAssets"
43+
android:text="load from assets"
44+
tools:ignore="HardcodedText" />
2145

2246
<Button
2347
android:layout_width="match_parent"
2448
android:layout_height="wrap_content"
25-
android:onClick="loadSVGAFromFile"
26-
android:text="load from file" />
49+
android:onClick="loadFromFile"
50+
android:text="load from file"
51+
tools:ignore="HardcodedText" />
2752

2853
<Button
2954
android:layout_width="match_parent"
3055
android:layout_height="wrap_content"
3156
android:onClick="loadSVGAFromNetworkAndAddText"
32-
android:text="load from network and add text" />
57+
android:text="load from network and add text"
58+
tools:ignore="HardcodedText" />
3359

3460
<Button
3561
android:layout_width="match_parent"
3662
android:layout_height="wrap_content"
37-
android:onClick="loadSVGAFromRes"
38-
android:text="load from res/raw" />
63+
android:onClick="loadFromRes"
64+
android:text="load from res/raw"
65+
tools:ignore="HardcodedText" />
3966

4067
<TextView
4168
android:id="@+id/tv_assets_name"

0 commit comments

Comments
 (0)