Skip to content

Commit 785a50b

Browse files
fix(android): drawing async image on physical device (#538)
# Summary Fixes: #535 Fixes: #536 ## Test Plan Run reproduction steps from issue: #535 and #536. Issues should not occur anymore. ## Screenshots / Videos n/a ## Compatibility | OS | Implemented | | ------- | :---------: | | iOS | ❌ | | Android | ✅ | ## Checklist - [ ] E2E tests are passing - [ ] Required E2E tests have been added (if applicable)
1 parent ac31109 commit 785a50b

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

android/src/main/java/com/swmansion/enriched/common/spans/EnrichedImageSpan.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import android.graphics.Paint
88
import android.graphics.drawable.AnimatedImageDrawable
99
import android.graphics.drawable.Drawable
1010
import android.os.Build
11+
import android.os.Handler
12+
import android.os.Looper
1113
import android.text.Editable
1214
import android.text.Spannable
1315
import android.text.style.ImageSpan
@@ -96,15 +98,17 @@ open class EnrichedImageSpan :
9698
if (spannable == null) {
9799
return@onLoaded
98100
}
99-
100-
val start = spannable.getSpanStart(this@EnrichedImageSpan)
101-
val end = spannable.getSpanEnd(this@EnrichedImageSpan)
102-
103-
if (start != -1 && end != -1) {
104-
// trick for adding empty span to force redraw when image is loaded
105-
val redrawSpan = ForceRedrawSpan()
106-
spannable.setSpan(redrawSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
107-
spannable.removeSpan(redrawSpan)
101+
// Ensure we are on the Main Thread before modifying the Spannable
102+
Handler(Looper.getMainLooper()).post {
103+
val start = spannable.getSpanStart(this@EnrichedImageSpan)
104+
val end = spannable.getSpanEnd(this@EnrichedImageSpan)
105+
106+
if (start != -1 && end != -1) {
107+
// trick for adding empty span to force redraw when image is loaded
108+
val redrawSpan = ForceRedrawSpan()
109+
spannable.setSpan(redrawSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
110+
spannable.removeSpan(redrawSpan)
111+
}
108112
}
109113
}
110114
}

0 commit comments

Comments
 (0)