Super easy to use official core-ktx library, learn about it~

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?

What convenience can the official core-ktx library bring to the development of the SparseArray series and Pair?

This article mainly studies a series of extended APIs provided under the package in the core-ktxlibrary for drawing, , , and other operations to see what convenience it can bring to our development.graphicsViewBitmapRectColor

DrawableBitmapconversion to and from

Bitmap.toDrawable(Resource)realize BitmapturnDrawable

image.png

To Bitmapdefine an extension method that is quickly converted into BitmapDrawable, or write some template code less.

Drawable.toBitmap()Implement Drawableconversion to Bitmapobject

image.png

DrawableConversion Bitmapshould 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:

  1. First determine whether the current Drawabletype is BitmapDrawable, if it is called directly, you getBitmap()can directly get the Bitmapobject, and then perform a certain proportion of compression conversion according to the incoming width and height before returning;

  2. 如果不是BitmapDrawable,就首先需要创建一个Bitmap对象,可以理解为一个"画布",然后接着创建一个Canvas对象并传入之前创建的Bitmap对象,这样我们就可以利用Canvas提供的绘制API在Bitmap这个"画布"上作画了,接下来直接调用Drawable的draw()方法并传入Canvas,就可以将Drawable中的显示内容绘制到我们一开始创建的Bitmap上了,这样就完成了DrawableBitmap的转换

Bitmap系列

简化对Bitmap的绘制操作

我们先看下日常开发中,我们怎么在Bitmap中绘制一点东西:

private fun test4(bitmap: Bitmap) {
    val canvas = Canvas(bitmap)
    canvas.apply { 
        //进行一些绘制操作
        drawLine(0f, 0f, 100f, 100f, Paint())
    }
}

有些繁琐,看下官方库给我们提供了什么便利的扩展实现:

image.png

帮助我们创建好Canvas对象,并且方法参数是一个接收者为Canvas的函数类型,这意味我们可以直接在外部传入的lambda中进行绘制操作:

private fun test4(bitmap: Bitmap) {
    bitmap.applyCanvas {
        //进行一些绘制操作
        drawLine(0f, 0f, 100f, 100f, Paint())
    }
}

简化Bitmap创建

1.createBitmap()创建指定大小和像素格式的Bitmap

image.png

还是简化了创建Bitmap的操作,虽然很小。

2.scale()缩放(压缩)Bitmap

image.png

这个也是我们常用的通过降低分辨率压缩Bitmap大小的一种方式。

操作Bitmap中的像素点

1.Bitmap.get(x: Int, y: Int)获取指定位置的像素点RGB值

image.png

经典的运算符重载函数,代码中可以直接val pointRGB = bitmap[100, 100]使用。

2.Bitmap.set()设置某个点的RGB像素值

image.png

同样也是个运算符重载方法,代码中直接bitmap[100, 100] = Color.RED使用。

3.Bitmap.contains()判断指定位置点是否落在Bitmap

image.png

运算符重载方法,直接Point(100, 100) in bitmap使用

color系列

普通扩展属性获取颜色的A、R、G、B

image.png

使用如下:

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

image.png

带有operator修饰componenX就是解构方法,X和参数声明的位置一一对应:

private fun test10(@ColorInt value: Int) {
    val (a, r, g, b) = value
}

向我们常见的data classHashMap都实现了类似的解扩展。

转换颜色Color对象

1.Int.toColor()整形颜色转换Color对象

image.png

2.String.toColorInt()实现字符串转Color对象

image.png

这个应该比较常用,直接"#ffffff".toColorInt()即可

Rect系列

解构获取左、上、右、下的值

image.png

熟悉的解构,使用和上面一样(RectF也同样提供了相同的解构方法),如下:

private fun test10(rect: Rect) {
    val (left, top, right, bottom) = rect
}

缩放Rect范围

下面是扩充Rect范围的API:

image.png image.png

image.png

使用如下:

private fun test10(rect: Rect) {
    val rect1 = rect + rect
    val rect2 = rect + 10
    val rect3 = rect + Point(100, 200)
}

同样也提供了minus()缩减Rect范围

Rect间取交集、并集等

image.png

image.png

判断某个点是否落在Rect

image.png

使用:Point(11, 11) in rect

Point、PointX系列

下面的扩展方法无非就是解构、通过运算符重载控制Point位置,上面已经讲了一大堆这样的使用,大家走马观花的看下就行,有个印象即可。

经典的解构取值方法

image.png

操作Point的位置

image.png

image.png

总结

上面的内容已经把graphics包下提供的扩展工具讲的七七八八了,大家主要是有个印象就行,使用的时候能想起来用更好,如果需要详细了解的请直接参考该包下的源码即可。

core-ktxAbout 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 .

Guess you like

Origin juejin.im/post/7121718556546482190