Created
March 7, 2011 17:25
-
-
Save codeswimmer/858833 to your computer and use it in GitHub Desktop.
Android: rotate a bitmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Bitmap rotateBitmap(Bitmap original, float degrees) { | |
int width = original.getWidth(); | |
int height = original.getHeight(); | |
Matrix matrix = new Matrix(); | |
matrix.preRotate(degrees); | |
Bitmap rotatedBitmap = Bitmap.createBitmap(original, 0, 0, width, height, matrix, true); | |
Canvas canvas = new Canvas(rotatedBitmap); | |
canvas.drawBitmap(original, 5.0f, 0.0f, null); | |
return rotatedBitmap; | |
} |
Extension for Kotlin
fun Bitmap.rotate(degrees: Float): Bitmap {
val matrix = Matrix().apply { postRotate(degrees) }
return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
}
And usage:
bitmap.rotate(90)
awsome, thanx for kotlin ext too!!
Bitmap.createBitmap
is java call and might be null. And I think we can use Number
for parameter and infix
for readability
infix fun Bitmap.rotate(degrees: Number): Bitmap? {
return Bitmap.createBitmap(
this,
0,
0,
width,
height,
Matrix().apply { postRotate(degrees.toFloat()) },
true
)
}
Usage
bitmap rotate 90
great !
To understand, this piece of code will rotate my photo to the right position or I choose how much and where it will rotate. Thank you.
Thank you, nice code !
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice code