@@ -10,18 +10,15 @@ import android.text.Layout
1010import android.text.style.DynamicDrawableSpan
1111import android.view.View
1212import 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()
0 commit comments