Skip to content

Commit 4e597fa

Browse files
authored
Merge pull request #432 from wordpress-mobile/revert-421-issue/experimenting-with-images-and-memory-comsumption
Fix drawables keep disappearing
2 parents 0a0b857 + 37b0b6f commit 4e597fa

File tree

3 files changed

+24
-30
lines changed

3 files changed

+24
-30
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecDynamicImageSpan.kt

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,15 @@ import android.text.Layout
1010
import android.text.style.DynamicDrawableSpan
1111
import android.view.View
1212
import org.wordpress.aztec.AztecText
13-
import java.lang.ref.WeakReference
1413

15-
abstract class AztecDynamicImageSpan(val context: Context, imageDrawable: Drawable?) : DynamicDrawableSpan() {
14+
abstract class AztecDynamicImageSpan(val context: Context, protected var imageDrawable: Drawable?) : DynamicDrawableSpan() {
1615

1716
var textView: AztecText? = null
1817
var originalBounds = Rect(imageDrawable?.bounds ?: Rect(0, 0, 0, 0))
1918
var aspectRatio: Double = 1.0
2019

2120
private var measuring = false
2221

23-
protected var drawableRef: WeakReference<Drawable>? = WeakReference<Drawable>(imageDrawable)
24-
2522
companion object {
2623
@JvmStatic protected fun setInitBounds(drawable: Drawable?) {
2724
drawable?.let {
@@ -59,16 +56,16 @@ abstract class AztecDynamicImageSpan(val context: Context, imageDrawable: Drawab
5956
}
6057

6158
init {
62-
computeAspectRatio(drawableRef?.get())
59+
computeAspectRatio()
6360

64-
setInitBounds(drawableRef?.get())
61+
setInitBounds(imageDrawable)
6562
}
6663

67-
fun computeAspectRatio(drawable: Drawable?) {
68-
if ((drawable?.intrinsicWidth ?: -1) > -1 && (drawable?.intrinsicHeight ?: -1) > -1) {
69-
aspectRatio = 1.0 * (drawable?.intrinsicWidth ?: 1) / (drawable?.intrinsicHeight ?: 1)
70-
} else if (!(drawable?.bounds?.isEmpty ?: true)) {
71-
aspectRatio = 1.0 * (drawable?.bounds?.width() ?: 0) / (drawable?.bounds?.height() ?: 1)
64+
fun computeAspectRatio() {
65+
if ((imageDrawable?.intrinsicWidth ?: -1) > -1 && (imageDrawable?.intrinsicHeight ?: -1) > -1) {
66+
aspectRatio = 1.0 * (imageDrawable?.intrinsicWidth ?: 1) / (imageDrawable?.intrinsicHeight ?: 1)
67+
} else if (!(imageDrawable?.bounds?.isEmpty ?: true)) {
68+
aspectRatio = 1.0 * (imageDrawable?.bounds?.width() ?: 0) / (imageDrawable?.bounds?.height() ?: 1)
7269
} else {
7370
aspectRatio = 1.0
7471
}
@@ -89,10 +86,8 @@ abstract class AztecDynamicImageSpan(val context: Context, imageDrawable: Drawab
8986
}
9087

9188
fun adjustBounds(start: Int): Rect {
92-
var drawable: Drawable? = drawableRef?.get()
93-
9489
if (textView == null || textView?.widthMeasureSpec == 0) {
95-
return Rect(drawable?.bounds ?: Rect(0, 0, 0, 0))
90+
return Rect(imageDrawable?.bounds ?: Rect(0, 0, 0, 0))
9691
}
9792

9893
val layout = textView?.layout
@@ -120,20 +115,20 @@ abstract class AztecDynamicImageSpan(val context: Context, imageDrawable: Drawab
120115
// just assume maximum size.
121116

122117
var width = if (originalBounds.width() > 0) originalBounds.width()
123-
else if ((drawable?.intrinsicWidth ?: -1) > -1) drawable?.intrinsicWidth ?: -1
118+
else if ((imageDrawable?.intrinsicWidth ?: -1) > -1) imageDrawable?.intrinsicWidth ?: -1
124119
else maxWidth
125120
var height = if (originalBounds.height() > 0) originalBounds.height()
126-
else if ((drawable?.intrinsicHeight ?: -1) > -1) drawable?.intrinsicHeight ?: -1
121+
else if ((imageDrawable?.intrinsicHeight ?: -1) > -1) imageDrawable?.intrinsicHeight ?: -1
127122
else (width / aspectRatio).toInt()
128123

129124
if (width > maxWidth) {
130125
width = maxWidth
131126
height = (width / aspectRatio).toInt()
132127
}
133128

134-
drawable?.bounds = Rect(0, 0, width, height)
129+
imageDrawable?.bounds = Rect(0, 0, width, height)
135130

136-
return Rect(drawable?.bounds ?: Rect(0, 0, 0, 0))
131+
return Rect(imageDrawable?.bounds ?: Rect(0, 0, 0, 0))
137132
}
138133

139134
fun calculateWantedWidth(widthMeasureSpec: Int): Int {
@@ -174,20 +169,20 @@ abstract class AztecDynamicImageSpan(val context: Context, imageDrawable: Drawab
174169
}
175170

176171
override fun getDrawable(): Drawable? {
177-
return drawableRef?.get()
172+
return imageDrawable
178173
}
179174

180175
override fun draw(canvas: Canvas, text: CharSequence, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
181176
canvas.save()
182177

183-
if (getDrawable() != null) {
178+
if (imageDrawable != null) {
184179
var transY = top
185180
if (mVerticalAlignment == DynamicDrawableSpan.ALIGN_BASELINE) {
186181
transY -= paint.fontMetricsInt.descent
187182
}
188183

189184
canvas.translate(x, transY.toFloat())
190-
getDrawable()!!.draw(canvas)
185+
imageDrawable!!.draw(canvas)
191186
}
192187

193188
canvas.restore()

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecImageSpan.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ class AztecImageSpan(context: Context, drawable: Drawable?, attributes: AztecAtt
1313
override val TAG: String = "img"
1414

1515
override fun onClick() {
16-
onImageTappedListener?.onImageTapped(attributes, getWidth(getDrawable()), getHeight(getDrawable()))
16+
onImageTappedListener?.onImageTapped(attributes, getWidth(imageDrawable), getHeight(imageDrawable))
1717
}
1818
}

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecMediaSpan.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import android.graphics.drawable.Drawable
88
import android.view.Gravity
99
import org.wordpress.aztec.AztecAttributes
1010
import org.wordpress.aztec.AztecText
11-
import java.lang.ref.WeakReference
1211
import java.util.*
1312

1413
abstract class AztecMediaSpan(context: Context, drawable: Drawable?, override var attributes: AztecAttributes = AztecAttributes(),
@@ -23,13 +22,13 @@ abstract class AztecMediaSpan(context: Context, drawable: Drawable?, override va
2322
}
2423

2524
fun setDrawable(newDrawable: Drawable?) {
26-
drawableRef = WeakReference<Drawable>(newDrawable)
25+
imageDrawable = newDrawable
2726

28-
originalBounds = Rect(getDrawable()?.bounds ?: Rect(0, 0, 0, 0))
27+
originalBounds = Rect(imageDrawable?.bounds ?: Rect(0, 0, 0, 0))
2928

3029
setInitBounds(newDrawable)
3130

32-
computeAspectRatio(newDrawable)
31+
computeAspectRatio()
3332
}
3433

3534
fun setOverlay(index: Int, newDrawable: Drawable?, gravity: Int) {
@@ -54,8 +53,8 @@ abstract class AztecMediaSpan(context: Context, drawable: Drawable?, override va
5453
}
5554

5655
private fun applyOverlayGravity(overlay: Drawable?, gravity: Int) {
57-
if (getDrawable() != null && overlay != null) {
58-
val rect = Rect(0, 0, getDrawable()!!.bounds.width(), getDrawable()!!.bounds.height())
56+
if (imageDrawable != null && overlay != null) {
57+
val rect = Rect(0, 0, imageDrawable!!.bounds.width(), imageDrawable!!.bounds.height())
5958
val outRect = Rect()
6059

6160
Gravity.apply(gravity, overlay.bounds.width(), overlay.bounds.height(), rect, outRect)
@@ -67,14 +66,14 @@ abstract class AztecMediaSpan(context: Context, drawable: Drawable?, override va
6766
override fun draw(canvas: Canvas, text: CharSequence, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
6867
canvas.save()
6968

70-
if (getDrawable() != null) {
69+
if (imageDrawable != null) {
7170
var transY = top
7271
if (mVerticalAlignment == ALIGN_BASELINE) {
7372
transY -= paint.fontMetricsInt.descent
7473
}
7574

7675
canvas.translate(x, transY.toFloat())
77-
getDrawable()!!.draw(canvas)
76+
imageDrawable!!.draw(canvas)
7877
}
7978

8079
overlays.forEach {

0 commit comments

Comments
 (0)