Skip to content

Commit e534474

Browse files
committed
Optimize code
1 parent 702e7be commit e534474

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

extensionslib/src/main/java/com/omega_r/libs/extensions/context/ResourceExtensions.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.omega_r.libs.extensions.context
22

33
import android.content.Context
44
import android.graphics.drawable.AnimatedVectorDrawable
5+
import android.graphics.drawable.ColorDrawable
56
import android.graphics.drawable.Drawable
67
import android.os.Build
78
import android.util.TypedValue
@@ -17,9 +18,7 @@ fun Context.getCompatDrawable(@DrawableRes id: Int): Drawable? {
1718

1819
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
1920
fun Context.getAnimatedVectorDrawable(@DrawableRes res: Int): AnimatedVectorDrawable? {
20-
val drawable = getCompatDrawable(res)
21-
if (drawable == null || drawable !is AnimatedVectorDrawable) return null
22-
return drawable
21+
return getCompatDrawable(res) as? AnimatedVectorDrawable
2322
}
2423

2524
fun Context.getCompatColor(@ColorRes id: Int): Int {
@@ -30,4 +29,8 @@ fun Context.getColorByAttribute(@AttrRes attrInt: Int): Int {
3029
return TypedValue().run {
3130
if (theme.resolveAttribute(attrInt, this, true)) data else 0
3231
}
32+
}
33+
34+
fun Context.getColorDrawableByAttribute(@AttrRes attrInt: Int): ColorDrawable {
35+
return ColorDrawable(getColorByAttribute(attrInt))
3336
}

0 commit comments

Comments
 (0)