diff --git a/api/current.txt b/api/current.txt index 6d0ac772..911274a9 100644 --- a/api/current.txt +++ b/api/current.txt @@ -103,7 +103,9 @@ package androidx.graphics { method public static android.graphics.Bitmap applyCanvas(android.graphics.Bitmap, kotlin.jvm.functions.Function1 block); method public static android.graphics.Bitmap createBitmap(int width, int height, android.graphics.Bitmap.Config config = "Bitmap.Config.ARGB_8888"); method @RequiresApi(26) public static android.graphics.Bitmap createBitmap(int width, int height, android.graphics.Bitmap.Config config = "Bitmap.Config.ARGB_8888", boolean hasAlpha = "true", android.graphics.ColorSpace colorSpace = "ColorSpace.get(ColorSpace.Named.SRGB)"); + method public static android.graphics.Bitmap! crop(android.graphics.Bitmap, int x, int y, int width, int height); method public static operator int get(android.graphics.Bitmap, int x, int y); + method public static android.graphics.Bitmap! rotate(android.graphics.Bitmap, float degrees, float px = "width / 2.0f", float py = "height / 2.0f"); method public static android.graphics.Bitmap scale(android.graphics.Bitmap, int width, int height, boolean filter = "true"); method public static operator void set(android.graphics.Bitmap, int x, int y, @ColorInt int color); } diff --git a/src/androidTest/java/androidx/graphics/BitmapTest.kt b/src/androidTest/java/androidx/graphics/BitmapTest.kt index 2cf7ded1..4f15ab52 100644 --- a/src/androidTest/java/androidx/graphics/BitmapTest.kt +++ b/src/androidTest/java/androidx/graphics/BitmapTest.kt @@ -68,4 +68,24 @@ class BitmapTest { b[1, 1] = 0x40302010 assertEquals(0x40302010, b[1, 1]) } + + @Test fun crop() { + val src = createBitmap(10, 10) + src[3, 5] = 0x40302010 + val res = src.crop(3, 5, 2, 2) + assertEquals(2, res.width) + assertEquals(2, res.height) + assertEquals(0x40302010, res[0, 0]) + } + + @Test fun rotate() { + val src = createBitmap(10, 10) + src[3, 5] = 0x40302010 + src[0, 0] = 0x10203010 + val res = src.rotate(90f, 0f, 0f) + assertEquals(10, res.width) + assertEquals(10, res.height) + assertEquals(0x10203010, res[9, 0]) + assertEquals(0x40302010, res[4, 3]) + } } diff --git a/src/main/java/androidx/graphics/Bitmap.kt b/src/main/java/androidx/graphics/Bitmap.kt index 47047862..df164976 100644 --- a/src/main/java/androidx/graphics/Bitmap.kt +++ b/src/main/java/androidx/graphics/Bitmap.kt @@ -19,8 +19,10 @@ package androidx.graphics import android.graphics.Bitmap +import android.graphics.Bitmap.createBitmap import android.graphics.Canvas import android.graphics.ColorSpace +import android.graphics.Matrix import android.support.annotation.ColorInt import android.support.annotation.RequiresApi @@ -111,3 +113,30 @@ inline fun createBitmap( ): Bitmap { return Bitmap.createBitmap(width, height, config, hasAlpha, colorSpace) } + +/** + * Creates a new bitmap, cropped from this bitmap. If the specified [x], [y], + * [width], [height] are the same as the current width and height of this bitmap, + * this bitmap is returned and no new bitmap is created. + * + * @param x The x coordinate of the first pixel. + * @param y The y coordinate of the first pixel. + * @param width The width. + * @param height The height. + * @return the cropped bitmap + */ +inline fun Bitmap.crop(x: Int, y: Int, width: Int, height: Int) = + Bitmap.createBitmap(this, x, y, width, height) + +/** + * Creates a new bitmap, rotated from this bitmap by [degrees] - the specified number of degrees, + * with a pivot point at ([px], [py]). The pivot point is the coordinate that should remain + * unchanged by the specified transformation. + * + * @param degrees The number of degrees. + * @param px The x coordinate of the pivot point. + * @param py The y coordinate of the pivot point. + * @return the rotated bitmap + */ +inline fun Bitmap.rotate(degrees: Float, px: Float = width / 2.0f, py: Float = height / 2.0f) = + createBitmap(this, 0, 0, width, height, Matrix().apply { setRotate(degrees, px, py) }, true) \ No newline at end of file