11package com.omega_r.libs.extensions.drawable
22
33import android.content.Context
4- import android.content.res.Resources
5- import android.graphics.Bitmap
6- import android.graphics.drawable.BitmapDrawable
74import android.graphics.drawable.Drawable
85import android.graphics.drawable.ScaleDrawable
96import android.view.Gravity.NO_GRAVITY
@@ -17,33 +14,17 @@ import com.omega_r.libs.extensions.context.getCompatDrawable
1714fun Context.getCompatDrawable (@DrawableRes id : Int , @ColorRes color : Int ): Drawable ? =
1815 getCompatDrawable(id)?.tint(this , color)
1916
20- fun Drawable? .tint (context : Context , @ColorRes color : Int ): Drawable ? =
21- this ?. tint(ContextCompat .getColor(context, color))
17+ fun Drawable.tint (context : Context , @ColorRes color : Int ): Drawable =
18+ tint(ContextCompat .getColor(context, color))
2219
23- fun Drawable?.tint (@ColorInt color : Int ): Drawable ? {
24- return this ?.let {
25- val mutate = DrawableCompat .wrap(it).mutate()
26- DrawableCompat .setTint(mutate, color)
27- return @let mutate
20+ fun Drawable.tint (@ColorInt color : Int ): Drawable {
21+ return DrawableCompat .wrap(this ).mutate().apply {
22+ DrawableCompat .setTint(this , color)
2823 }
2924}
3025
31- fun Drawable? .scale (resources : Resources , size : Int ) : Drawable ? = this ?. scale(resources, size, size)
26+ fun Drawable.scale (size : Int ): Drawable = scale(size, size)
3227
33- fun Drawable?.scale (resources : Resources , height : Int , width : Int ) : Drawable ? {
34- if (this == null ) return null
35- if (this is BitmapDrawable ) {
36- bitmap?.let {
37- val bitmap: Bitmap ? = Bitmap .createScaledBitmap(it, height, width, true )
38- if (bitmap != null ) return BitmapDrawable (resources, bitmap)
39- }
40- }
41-
42- val scaleDrawable = ScaleDrawable (this , NO_GRAVITY , width.toFloat(), height.toFloat()).drawable
43- scaleDrawable?.let {
44- it.setBounds(0 , 0 , width, height)
45- return it
46- }
47-
48- return this
28+ fun Drawable.scale (height : Int , width : Int ): Drawable {
29+ return ScaleDrawable (this , NO_GRAVITY , width.toFloat(), height.toFloat())
4930}
0 commit comments