This article is a research official
core-ktx库的第四篇文章
, aiming to explore which tool classes or methods in the library can improve our development efficiency.
Updated series of articles:
What conveniences can the official core-ktx library bring to development you need to know?
What convenience can the official core-ktx library bring to the development of rich text spans?
This article mainly studies a series of extended APIs provided under the package in the
core-ktx
library for drawing, , , and other operations to see what convenience it can bring to our development.graphics
View
Bitmap
Rect
Color
Drawable
Bitmap
conversion to and from
Bitmap.toDrawable(Resource)
realize Bitmap
turnDrawable
To Bitmap
define an extension method that is quickly converted into BitmapDrawable
, or write some template code less.
Drawable.toBitmap()
Implement Drawable
conversion to Bitmap
object
Drawable
Conversion Bitmap
should be a common scenario in our daily development. Here the official library directly provides us with an toBitmap()
API, which is very convenient. Let's briefly introduce the principle:
-
First determine whether the current
Drawable
type isBitmapDrawable
, if it is called directly, yougetBitmap()
can directly get theBitmap
object, and then perform a certain proportion of compression conversion according to the incoming width and height before returning; -
如果不是
BitmapDrawable
,就首先需要创建一个Bitmap
对象,可以理解为一个"画布"
,然后接着创建一个Canvas
对象并传入之前创建的Bitmap
对象,这样我们就可以利用Canvas
提供的绘制API在Bitmap这个"画布"
上作画了,接下来直接调用Drawable的draw()
方法并传入Canvas
,就可以将Drawable
中的显示内容绘制到我们一开始创建的Bitmap
上了,这样就完成了Drawable
到Bitmap
的转换
Bitmap
系列
简化对Bitmap的绘制操作
我们先看下日常开发中,我们怎么在Bitmap
中绘制一点东西:
private fun test4(bitmap: Bitmap) {
val canvas = Canvas(bitmap)
canvas.apply {
//进行一些绘制操作
drawLine(0f, 0f, 100f, 100f, Paint())
}
}
有些繁琐,看下官方库给我们提供了什么便利的扩展实现:
帮助我们创建好Canvas
对象,并且方法参数是一个接收者为Canvas
的函数类型,这意味我们可以直接在外部传入的lambda
中进行绘制操作:
private fun test4(bitmap: Bitmap) {
bitmap.applyCanvas {
//进行一些绘制操作
drawLine(0f, 0f, 100f, 100f, Paint())
}
}
简化Bitmap
创建
1.createBitmap()
创建指定大小和像素格式的Bitmap
还是简化了创建Bitmap
的操作,虽然很小。
2.scale()
缩放(压缩)Bitmap
这个也是我们常用的通过降低分辨率压缩Bitmap
大小的一种方式。
操作Bitmap中的像素点
1.Bitmap.get(x: Int, y: Int)
获取指定位置的像素点RGB值
经典的运算符重载函数,代码中可以直接val pointRGB = bitmap[100, 100]
使用。
2.Bitmap.set()
设置某个点的RGB像素值
同样也是个运算符重载方法,代码中直接bitmap[100, 100] = Color.RED
使用。
3.Bitmap.contains()
判断指定位置点是否落在Bitmap
中
运算符重载方法,直接Point(100, 100) in bitmap
使用
color
系列
普通扩展属性获取颜色的A、R、G、B
值
使用如下:
private fun test10(@ColorInt value: Int) {
val a = value.alpha
val r = value.red
val g = value.green
val b = value.blue
}
解构获取颜色的A、R、G、B
值
带有operator
修饰componenX
就是解构方法,X和参数声明的位置一一对应:
private fun test10(@ColorInt value: Int) {
val (a, r, g, b) = value
}
向我们常见的data class
、HashMap
都实现了类似的解扩展。
转换颜色Color对象
1.Int.toColor()
整形颜色转换Color
对象
2.String.toColorInt()
实现字符串转Color
对象
这个应该比较常用,直接"#ffffff".toColorInt()
即可
Rect
系列
解构获取左、上、右、下的值
熟悉的解构,使用和上面一样(RectF
也同样提供了相同的解构方法),如下:
private fun test10(rect: Rect) {
val (left, top, right, bottom) = rect
}
缩放Rect
范围
下面是扩充Rect
范围的API:
使用如下:
private fun test10(rect: Rect) {
val rect1 = rect + rect
val rect2 = rect + 10
val rect3 = rect + Point(100, 200)
}
同样也提供了minus()
缩减Rect
范围
Rect间取交集、并集等
判断某个点是否落在Rect
中
使用:Point(11, 11) in rect
Point、PointX
系列
下面的扩展方法无非就是解构、通过运算符重载控制
Point
位置,上面已经讲了一大堆这样的使用,大家走马观花的看下就行,有个印象即可。
经典的解构取值方法
操作Point
的位置
总结
上面的内容已经把
graphics
包下提供的扩展工具讲的七七八八了,大家主要是有个印象就行,使用的时候能想起来用更好,如果需要详细了解的请直接参考该包下的源码即可。
core-ktx
About the last article about exploring the official library, I hope I can give you some help in this series and improve your development efficiency. And by learning the packaging ideas in the official library, it will also bring inspiration to everyone's daily development of small and medium optimization.
I am participating in the recruitment of the creator signing program of the Nuggets Technology Community, click the link to register and submit .