Created
July 18, 2014 08:22
-
-
Save tajchert/63eac65e6c8b9d2b7874 to your computer and use it in GitHub Desktop.
Crop Top of a Bitmap to particular size.
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
//Author: Michal Tajchert | |
//tajchert.pl | |
public Bitmap scaleTopCrop(Bitmap source, int newHeight, int newWidth) { | |
int sourceWidth = source.getWidth(); | |
int sourceHeight = source.getHeight(); | |
float xScale = (float) newWidth / sourceWidth; | |
float yScale = (float) newHeight / sourceHeight; | |
float scale = Math.max(xScale, yScale); | |
float scaledWidth = scale * sourceWidth; | |
float scaledHeight = scale * sourceHeight; | |
RectF targetRect = new RectF(0, 0, scaledWidth, scaledHeight); | |
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig()); | |
Canvas canvas = new Canvas(dest); | |
canvas.drawBitmap(source, null, targetRect, null); | |
return dest; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Forgot to add source of "scaleCenterCrop" - http://stackoverflow.com/questions/8112715/how-to-crop-bitmap-center-like-imageview