@@ -11,6 +11,7 @@ import android.util.Base64
1111import android.util.TypedValue
1212import android.view.View
1313import android.widget.ImageView
14+ import android.widget.TextView
1415import com.omega_r.libs.omegatypes.Image.Companion.applyBackground
1516import java.io.*
1617
@@ -48,6 +49,8 @@ open class Image : Serializable {
4849 }
4950 }
5051
52+ open fun getDrawable (context : Context ): Drawable ? = null
53+
5154 open fun preload (context : Context ) {
5255 // nothing
5356 }
@@ -119,34 +122,38 @@ class ResourceImage(private val resId: Int) : Image() {
119122 }
120123
121124 override fun getStream (context : Context , compressFormat : Bitmap .CompressFormat , quality : Int ): InputStream {
122- val drawable = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
125+ return getDrawable(context).toBitmap {
126+ toInputStream(compressFormat, quality)
127+ }
128+ }
129+
130+ override fun getDrawable (context : Context ): Drawable {
131+ return if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
123132 context.getDrawable(resId)!!
124133 } else {
125134 context.resources.getDrawable(resId)!!
126135 }
127-
128- return drawable.toBitmap {
129- toInputStream(compressFormat, quality)
130- }
131136 }
132137}
133138
134- class DrawableImage (private val drawable : Drawable ) : Image() {
139+ class DrawableImage (private val innerDrawable : Drawable ) : Image() {
135140
136141 override fun applyImage (imageView : ImageView , placeholderResId : Int ) {
137- imageView.setImageDrawable(drawable )
142+ imageView.setImageDrawable(innerDrawable )
138143 }
139144
140145 override fun applyBackground (view : View , placeholderResId : Int ) {
141- applyBackground(view, drawable )
146+ applyBackground(view, innerDrawable )
142147 }
143148
144149 override fun getStream (context : Context , compressFormat : Bitmap .CompressFormat , quality : Int ): InputStream {
145- return drawable .toBitmap {
150+ return innerDrawable .toBitmap {
146151 toInputStream(compressFormat, quality)
147152 }
148153 }
149154
155+ override fun getDrawable (context : Context ) = innerDrawable
156+
150157}
151158
152159class BitmapImage (private val bitmap : Bitmap ) : Image() {
@@ -162,6 +169,8 @@ class BitmapImage(private val bitmap: Bitmap) : Image() {
162169 override fun getStream (context : Context , compressFormat : Bitmap .CompressFormat , quality : Int ): InputStream {
163170 return bitmap.toInputStream(compressFormat, quality)
164171 }
172+
173+ override fun getDrawable (context : Context ) = BitmapDrawable (context.resources, bitmap)
165174}
166175
167176fun Bitmap.toInputStream (compressFormat : Bitmap .CompressFormat , quality : Int ): InputStream {
@@ -221,3 +230,35 @@ fun View.setBackground(image: Image?, placeholderResId: Int = 0) {
221230 }
222231}
223232
233+ private fun TextView.getImage (index : Int ): Image ? {
234+ val drawables = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR1 ) compoundDrawablesRelative else compoundDrawables
235+ return drawables[index]?.let { Image .from(it) }
236+ }
237+
238+ private fun TextView.setImage (index : Int , image : Image ? ) {
239+ val drawables = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR1 ) compoundDrawablesRelative else compoundDrawables
240+
241+ drawables[index] = image?.getDrawable(context)
242+
243+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR1 ) {
244+ setCompoundDrawablesRelativeWithIntrinsicBounds(drawables[0 ], drawables[1 ], drawables[2 ], drawables[3 ])
245+ } else {
246+ setCompoundDrawablesWithIntrinsicBounds(drawables[0 ], drawables[1 ], drawables[2 ], drawables[3 ])
247+ }
248+ }
249+
250+ var TextView .imageStart: Image ?
251+ get() = getImage(0 )
252+ set(value) = setImage(0 , value)
253+
254+ var TextView .imageEnd: Image ?
255+ get() = getImage(2 )
256+ set(value) = setImage(2 , value)
257+
258+ var TextView .imageTop: Image ?
259+ get() = getImage(1 )
260+ set(value) = setImage(1 , value)
261+
262+ var TextView .imageBottom: Image ?
263+ get() = getImage(3 )
264+ set(value) = setImage(3 , value)
0 commit comments