-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargeImageView.kt
388 lines (282 loc) · 9.74 KB
/
LargeImageView.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package com.kirkbushman.largeimageview
import android.content.Context
import android.net.Uri
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.annotation.IntDef
import androidx.annotation.UiThread
import com.davemorrissey.labs.subscaleview.ImageSource
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
import java.io.File
@Suppress("unused", "MemberVisibilityCanBePrivate")
class LargeImageView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(
context,
attrs,
defStyleAttr
) {
companion object {
const val SHOW_THUMBNAIL_BEFORE = 0
const val SHOW_LOADING_BEFORE = 1
const val SHOWING_NOTHING = 0
const val SHOWING_THUMBNAIL = 1
const val SHOWING_LOADING = 2
const val SHOWING_ERROR = 3
const val SHOWING_SOURCE = 4
@IntDef(
SHOWING_NOTHING,
SHOWING_THUMBNAIL,
SHOWING_LOADING,
SHOWING_ERROR,
SHOWING_SOURCE
)
@Retention(AnnotationRetention.SOURCE)
annotation class State
}
private var thumbnailView: View? = null
private var loadingView: View? = null
private var errorView: View? = null
private var sourceView: SubsamplingScaleImageView? = null
private var loader: ImageLoader? = null
private var viewsShownListener: OnViewsShownListener? = null
@State
private var state: Int = SHOWING_NOTHING
private var showImageWhenAvailable: Boolean
private var showSourceImage: Boolean
private var showWhileLoading: Int = SHOW_LOADING_BEFORE
init {
val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.LargeImageView, defStyleAttr, 0)
showImageWhenAvailable = typedArray.getBoolean(R.styleable.LargeImageView_showImageWhenAvailable, true)
showSourceImage = typedArray.getBoolean(R.styleable.LargeImageView_showShourceImage, true)
showWhileLoading = typedArray.getInt(R.styleable.LargeImageView_showWhileLoading, SHOW_LOADING_BEFORE)
typedArray.recycle()
}
fun setImageLoader(loader: ImageLoader?) {
this.loader = loader
}
fun getThumbnailView(): View? {
return thumbnailView
}
fun getLoadingView(): View? {
return loadingView
}
fun getErrorView(): View? {
return errorView
}
fun getSsiv(): SubsamplingScaleImageView? {
return sourceView
}
@State
fun getState(): Int {
return state
}
fun getShowImageWhenAvailable(): Boolean {
return showImageWhenAvailable
}
fun setShowImageWhenAvailable(showImageWhenAvailable: Boolean) {
this.showImageWhenAvailable = showImageWhenAvailable
}
fun setShowSourceImage(showSourceImage: Boolean) {
this.showSourceImage = showSourceImage
}
fun setShowLoadingWhileWaiting() {
this.showWhileLoading = SHOW_LOADING_BEFORE
}
fun setShowThumbnailWhileWaiting() {
this.showWhileLoading = SHOW_THUMBNAIL_BEFORE
}
fun setOnViewShownListener(viewsShownListener: OnViewsShownListener?) {
this.viewsShownListener = viewsShownListener
}
fun triggerShowImage() {
if (showImageWhenAvailable) {
return
}
showSourceView(false)
}
fun startLoading() {
clearViews()
loader?.let { loader ->
// show the thumbnail in the meantime,
// while the large image is loading in the background.
if (showWhileLoading == SHOW_THUMBNAIL_BEFORE) {
// remember to generate view id
thumbnailView = buildThumbnailView()
thumbnailView?.let { thumbView ->
loader.loadThumbnail(thumbView)
showThumbnailView()
}
}
// show the loading view in the meantime,
// while the large image is loading in the background.
if (showWhileLoading == SHOW_LOADING_BEFORE) {
loadingView = buildLoadingView()
loadingView?.let {
showLoadingView()
}
}
// start loading in the background the real image.
if (showSourceImage) {
// remember to generate view id
sourceView = buildSourceView()
sourceView?.let { view ->
loader.preloadSource(object : ImageReadyCallback {
// if the image is ready set it in the SSIV
//
// if showImageWhenAvailable is set to true,
// show the main image right away
//
// otherwise wait for triggerShowImage() to get called.
//
override fun onImageReady(file: File) {
view.setImage(ImageSource.uri(Uri.fromFile(file)))
if (showImageWhenAvailable) {
showSourceView(false)
loadingView?.visibility = View.GONE
errorView?.visibility = View.GONE
}
}
// if something went wrong, show the error view.
override fun onImageErrored() {
// remember to generate view id
errorView = buildErrorView()
errorView?.let { _ ->
showErrorView(false)
loadingView?.visibility = View.GONE
}
}
})
}
}
}
}
fun clearViews() {
if (childCount > 0) {
removeAllViews()
}
state = SHOWING_NOTHING
thumbnailView = null
loadingView = null
errorView = null
sourceView = null
}
@UiThread
fun showThumbnailView(disableOtherViews: Boolean = true) {
if (state == SHOWING_THUMBNAIL) {
return
}
if (thumbnailView == null) {
thumbnailView = buildThumbnailView()
}
thumbnailView?.let {
addAndEnableChild(it)
state = SHOWING_THUMBNAIL
if (disableOtherViews) {
loadingView?.visibility = View.GONE
errorView?.visibility = View.GONE
sourceView?.visibility = View.GONE
}
Log.i("LargeImageView", "Showing thumb view")
viewsShownListener?.onThumbnailViewShown(it)
}
}
@UiThread
fun showLoadingView(disableOtherViews: Boolean = true) {
if (state == SHOWING_LOADING) {
return
}
if (loadingView == null) {
loadingView = buildErrorView()
}
loadingView?.let {
addAndEnableChild(it)
state = SHOWING_LOADING
if (disableOtherViews) {
errorView?.visibility = View.GONE
thumbnailView?.visibility = View.GONE
sourceView?.visibility = View.GONE
}
viewsShownListener?.onLoadingViewShown(it)
}
}
@UiThread
fun showErrorView(disableOtherViews: Boolean = true) {
if (state == SHOWING_ERROR) {
return
}
if (errorView == null) {
errorView = buildErrorView()
}
errorView?.let {
addAndEnableChild(it)
state = SHOWING_ERROR
if (disableOtherViews) {
loadingView?.visibility = View.GONE
thumbnailView?.visibility = View.GONE
sourceView?.visibility = View.GONE
}
viewsShownListener?.onErrorViewShown(it)
}
}
@UiThread
fun showSourceView(disableOtherViews: Boolean = true) {
if (state == SHOWING_SOURCE) {
return
}
if (sourceView == null) {
sourceView = buildSourceView()
}
sourceView?.let {
addAndEnableChild(it)
state = SHOWING_SOURCE
if (disableOtherViews) {
thumbnailView?.visibility = View.GONE
loadingView?.visibility = View.GONE
errorView?.visibility = View.GONE
}
Log.i("LargeImageView", "Showing source view")
viewsShownListener?.onImageViewShown(it)
}
}
private fun addAndEnableChild(view: View) {
if (findViewById<View>(view.id) == null) {
addView(
view,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
}
if (view.visibility != View.VISIBLE) {
view.visibility = View.VISIBLE
}
if (childCount > 0) {
view.bringToFront()
}
}
private fun buildThumbnailView(): View? {
val view = loader?.getThumbnailView(context)
view?.id = View.generateViewId()
return view
}
private fun buildSourceView(): SubsamplingScaleImageView {
val view = SubsamplingScaleImageView(context)
view.id = View.generateViewId()
return view
}
private fun buildLoadingView(): View? {
val view = loader?.getLoadingView(context)
view?.id = View.generateViewId()
return view
}
private fun buildErrorView(): View? {
val view = loader?.getErrorView(context)
view?.id = View.generateViewId()
return view
}
}