@@ -15,7 +15,11 @@ import app.rive.runtime.kotlin.RiveAnimationView
15
15
import app.rive.runtime.kotlin.core.BytesRequest
16
16
import app.rive.runtime.kotlin.core.FileAsset
17
17
import app.rive.runtime.kotlin.core.FileAssetLoader
18
+ import app.rive.runtime.kotlin.core.FontAsset
19
+ import app.rive.runtime.kotlin.core.ImageAsset
18
20
import app.rive.runtime.kotlin.core.Loop
21
+ import app.rive.runtime.kotlin.core.RiveFont
22
+ import app.rive.runtime.kotlin.core.RiveRenderImage
19
23
import com.android.volley.RequestQueue
20
24
import com.android.volley.toolbox.Volley
21
25
import kotlin.random.Random
@@ -33,6 +37,21 @@ private fun makeContainer(context: Context): FrameLayout {
33
37
}
34
38
}
35
39
40
+ private fun makeButton (
41
+ context : Context ,
42
+ label : String ,
43
+ clickListener : View .OnClickListener
44
+ ): Button {
45
+ return Button (context).apply {
46
+ layoutParams = ViewGroup .LayoutParams (
47
+ ViewGroup .LayoutParams .WRAP_CONTENT ,
48
+ ViewGroup .LayoutParams .WRAP_CONTENT ,
49
+ )
50
+ text = label
51
+ setOnClickListener(clickListener)
52
+ }
53
+ }
54
+
36
55
class AssetLoaderFragment : Fragment () {
37
56
private var _binding : FragmentAssetLoaderBinding ? = null
38
57
private val binding get() = _binding !!
@@ -76,36 +95,28 @@ class AssetButtonFragment : Fragment() {
76
95
77
96
private val loremImage = " https://picsum.photos"
78
97
private lateinit var queue: RequestQueue
79
- private lateinit var assetStore: AssetStore
80
- private val minSize = 150
81
- private val maxSize = 500
98
+ private lateinit var assetStore: ImageAssetStore
99
+ private val minSize = 750
100
+ private val maxSize = 1000
82
101
private val maxId = 1084
83
- private var lastAssetChanged = 0
84
102
85
- private fun makeButton (context : Context ): Button {
86
- return Button (context).apply {
87
- layoutParams = ViewGroup .LayoutParams (
88
- ViewGroup .LayoutParams .WRAP_CONTENT ,
89
- ViewGroup .LayoutParams .WRAP_CONTENT ,
103
+ private fun randomImageButton (context : Context ): Button {
104
+ return makeButton(context, " Change Image" ) {
105
+ val randomWidth = Random .nextInt(minSize, maxSize)
106
+ val randomHeight = Random .nextInt(minSize, maxSize)
107
+ val imgId = Random .nextInt(maxId)
108
+ val url = " $loremImage /id/$imgId /$randomWidth /$randomHeight "
109
+ val request = BytesRequest (
110
+ url,
111
+ { bytes ->
112
+ assetStore.nextAsset.image = RiveRenderImage .make(bytes)
113
+ },
114
+ {
115
+ Log .e(" Request" , " onAssetLoaded: failed to load $url ." )
116
+ it.printStackTrace()
117
+ }
90
118
)
91
- text = " Change Image"
92
- setOnClickListener {
93
- val randomAsset =
94
- assetStore.assetList[lastAssetChanged++ % assetStore.assetList.size]
95
- val randomWidth = Random .nextInt(minSize, maxSize)
96
- val randomHeight = Random .nextInt(minSize, maxSize)
97
- val imgId = Random .nextInt(maxId)
98
- val url = " $loremImage /id/$imgId /$randomWidth /$randomHeight "
99
- val request = BytesRequest (
100
- url,
101
- { bytes -> randomAsset.decode(bytes) },
102
- {
103
- Log .e(" Request" , " onAssetLoaded: failed to load $url ." )
104
- it.printStackTrace()
105
- }
106
- )
107
- queue.add(request)
108
- }
119
+ queue.add(request)
109
120
}
110
121
}
111
122
@@ -119,39 +130,73 @@ class AssetButtonFragment : Fragment() {
119
130
val ctx = view.context
120
131
queue = Volley .newRequestQueue(ctx)
121
132
122
- assetStore = AssetStore ()
133
+ assetStore = ImageAssetStore ()
123
134
val riveView = RiveAnimationView .Builder (ctx)
124
135
.setAssetLoader(assetStore)
125
- .setResource(R .raw.walle )
136
+ .setResource(R .raw.asset_load_check )
126
137
.build()
127
138
128
139
makeContainer(ctx).let {
129
140
it.addView(riveView)
130
141
binding.fragmentButtonContainer.addView(it)
131
142
}
132
143
133
- makeButton (ctx).let {
144
+ randomImageButton (ctx).let {
134
145
binding.fragmentButtonContainer.addView(it)
135
146
}
136
147
137
148
138
149
return view
139
150
}
140
151
141
- private class AssetStore : FileAssetLoader () {
142
- val assetList = mutableListOf<FileAsset >()
152
+ private class ImageAssetStore : FileAssetLoader () {
153
+ private var lastAssetChanged = 0
154
+ val assetList = mutableListOf<ImageAsset >()
155
+
156
+ val nextAsset: ImageAsset
157
+ get() = assetList[lastAssetChanged++ % assetList.size]
158
+
143
159
override fun loadContents (asset : FileAsset , inBandBytes : ByteArray ): Boolean {
144
- return assetList.add(asset)
160
+ if (asset is ImageAsset ) {
161
+ return assetList.add(asset)
162
+ }
163
+ return false
145
164
}
146
165
}
147
166
}
148
167
149
168
class FontAssetFragment : Fragment () {
150
169
private var _binding : FragmentAssetButtonBinding ? = null
170
+ private lateinit var queue: RequestQueue
151
171
private val binding get() = _binding !!
152
172
153
173
private lateinit var fontDecoder: RandomFontDecoder
154
174
175
+ private val fontUrls = listOf (
176
+ " https://cdn.rive.app/runtime/flutter/IndieFlower-Regular.ttf" ,
177
+ " https://cdn.rive.app/runtime/flutter/comic-neue.ttf" ,
178
+ " https://cdn.rive.app/runtime/flutter/inter.ttf" ,
179
+ " https://cdn.rive.app/runtime/flutter/inter-tight.ttf" ,
180
+ " https://cdn.rive.app/runtime/flutter/josefin-sans.ttf" ,
181
+ " https://cdn.rive.app/runtime/flutter/send-flowers.ttf" ,
182
+ )
183
+
184
+
185
+ private fun randomFontButton (context : Context ): Button {
186
+ return makeButton(context, " Change Font" ) {
187
+ val url = fontUrls.random()
188
+ val request = BytesRequest (
189
+ url,
190
+ { bytes -> fontDecoder.fontAsset.font = RiveFont .make(bytes) },
191
+ {
192
+ Log .e(" Request" , " onAssetLoaded: failed to load $url ." )
193
+ it.printStackTrace()
194
+ }
195
+ )
196
+ queue.add(request)
197
+ }
198
+ }
199
+
155
200
override fun onCreateView (
156
201
inflater : LayoutInflater , container : ViewGroup ? ,
157
202
savedInstanceState : Bundle ?
@@ -160,6 +205,7 @@ class FontAssetFragment : Fragment() {
160
205
_binding = FragmentAssetButtonBinding .inflate(inflater, container, false )
161
206
val view = binding.root
162
207
val ctx = view.context
208
+ queue = Volley .newRequestQueue(ctx)
163
209
164
210
fontDecoder = RandomFontDecoder (ctx)
165
211
val riveView = RiveAnimationView .Builder (ctx)
@@ -173,20 +219,26 @@ class FontAssetFragment : Fragment() {
173
219
it.addView(riveView)
174
220
binding.fragmentButtonContainer.addView(it)
175
221
}
222
+ randomFontButton(ctx).let {
223
+ binding.fragmentButtonContainer.addView(it)
224
+ }
176
225
177
226
return view
178
227
}
179
228
180
229
private class RandomFontDecoder (private val context : Context ) : FileAssetLoader() {
181
- lateinit var fontAsset: FileAsset
230
+ lateinit var fontAsset: FontAsset
182
231
override fun loadContents (asset : FileAsset , inBandBytes : ByteArray ): Boolean {
183
- // Store a reference to the asset.
184
- fontAsset = asset
185
-
186
- // Load the original font
187
- context.resources.openRawResource(R .raw.roboto).use {
188
- return asset.decode(it.readBytes())
232
+ if (asset is FontAsset ) {
233
+ // Store a reference to the asset.
234
+ fontAsset = asset
235
+
236
+ // Load the original font
237
+ context.resources.openRawResource(R .raw.roboto).use {
238
+ return asset.decode(it.readBytes())
239
+ }
189
240
}
241
+ return false
190
242
}
191
243
}
192
244
}
0 commit comments