1+ package com.omega_r.libs.extensions.drawable
2+
3+ import android.content.Context
4+ import android.content.res.Resources
5+ import android.graphics.Bitmap
6+ import android.graphics.drawable.BitmapDrawable
7+ import android.graphics.drawable.Drawable
8+ import android.graphics.drawable.ScaleDrawable
9+ import android.view.Gravity.NO_GRAVITY
10+ import androidx.annotation.ColorInt
11+ import androidx.annotation.ColorRes
12+ import androidx.annotation.DrawableRes
13+ import androidx.core.content.ContextCompat
14+ import androidx.core.graphics.drawable.DrawableCompat
15+ import com.omega_r.libs.extensions.context.getCompatDrawable
16+
17+ fun Context.getCompatDrawable (@DrawableRes id : Int , @ColorRes color : Int ): Drawable ? =
18+ getCompatDrawable(id)?.tint(this , color)
19+
20+ fun Drawable?.tint (context : Context , @ColorRes color : Int ): Drawable ? =
21+ this ?.tint(ContextCompat .getColor(context, color))
22+
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
28+ }
29+ }
30+
31+ fun Drawable?.scale (resources : Resources , size : Int ) : Drawable ? = this ?.scale(resources, size, size)
32+
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
49+ }
0 commit comments