Skip to content

Commit 2ff0c04

Browse files
committed
Add canvas extensions
Fix drawable extensions
1 parent 353064f commit 2ff0c04

2 files changed

Lines changed: 26 additions & 28 deletions

File tree

extensionslib/src/main/java/com/omega_r/libs/extensions/canvas/CanvasExtensions.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,28 @@ package com.omega_r.libs.extensions.canvas
22

33
import android.graphics.Canvas
44
import android.graphics.Paint
5+
import android.graphics.Rect
6+
import android.graphics.drawable.Drawable
57

68
fun Canvas.drawCircle(cx: Int, cy: Int, radius: Int, paint: Paint) {
79
drawCircle(cx.toFloat(), cy.toFloat(), radius.toFloat(), paint)
810
}
911

1012
fun Canvas.drawCircle(cx: Int, cy: Int, radius: Float, paint: Paint) {
1113
drawCircle(cx.toFloat(), cy.toFloat(), radius, paint)
12-
}
14+
}
15+
16+
fun Canvas.drawDrawable(left: Float, top: Float, right: Float, bottom: Float, drawable: Drawable) {
17+
drawable.setBounds(left.toInt(), top.toInt(), right.toInt(), bottom.toInt())
18+
drawable.draw(this)
19+
}
20+
21+
fun Canvas.drawDrawable(left: Int, top: Int, right: Int, bottom: Int, drawable: Drawable) {
22+
drawable.setBounds(left, top, right, bottom)
23+
drawable.draw(this)
24+
}
25+
26+
fun Canvas.drawDrawable(rect: Rect, drawable: Drawable) {
27+
drawable.bounds = rect
28+
drawable.draw(this)
29+
}
Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.omega_r.libs.extensions.drawable
22

33
import android.content.Context
4-
import android.content.res.Resources
5-
import android.graphics.Bitmap
6-
import android.graphics.drawable.BitmapDrawable
74
import android.graphics.drawable.Drawable
85
import android.graphics.drawable.ScaleDrawable
96
import android.view.Gravity.NO_GRAVITY
@@ -17,33 +14,17 @@ import com.omega_r.libs.extensions.context.getCompatDrawable
1714
fun 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

Comments
 (0)