diff options
author | Dominik Schürmann <dominik@dominikschuermann.de> | 2014-01-12 15:50:09 -0800 |
---|---|---|
committer | Dominik Schürmann <dominik@dominikschuermann.de> | 2014-01-12 15:50:09 -0800 |
commit | 92aa5b36bba57e4927f146d49c9f124a37b7b5f9 (patch) | |
tree | 865d3962e54225fc4f13e0d19852ddc061e7e05f /libraries/AndroidBootstrap/src/com/beardedhen/androidbootstrap/utils/ImageUtils.java | |
parent | 536c4db48f12287eaf1875e2d208c0ad07e31ee6 (diff) | |
parent | e3bcf64d9e63e293c51db921572a9b87533d26ad (diff) | |
download | open-keychain-92aa5b36bba57e4927f146d49c9f124a37b7b5f9.tar.gz open-keychain-92aa5b36bba57e4927f146d49c9f124a37b7b5f9.tar.bz2 open-keychain-92aa5b36bba57e4927f146d49c9f124a37b7b5f9.zip |
Merge pull request #179 from dschuermann/key-details
Key details
Diffstat (limited to 'libraries/AndroidBootstrap/src/com/beardedhen/androidbootstrap/utils/ImageUtils.java')
-rw-r--r-- | libraries/AndroidBootstrap/src/com/beardedhen/androidbootstrap/utils/ImageUtils.java | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/libraries/AndroidBootstrap/src/com/beardedhen/androidbootstrap/utils/ImageUtils.java b/libraries/AndroidBootstrap/src/com/beardedhen/androidbootstrap/utils/ImageUtils.java new file mode 100644 index 000000000..3eefa9366 --- /dev/null +++ b/libraries/AndroidBootstrap/src/com/beardedhen/androidbootstrap/utils/ImageUtils.java @@ -0,0 +1,77 @@ +package com.beardedhen.androidbootstrap.utils; + + +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.PorterDuffXfermode; +import android.graphics.Rect; +import android.graphics.RectF; +import android.graphics.Bitmap.Config; +import android.graphics.PorterDuff.Mode; + +public class ImageUtils +{ + + public static Bitmap getCircleBitmap(Bitmap bitmap) + { + return getCircleBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight()); + } + + public static Bitmap getCircleBitmap(Bitmap bitmap, int width, int height) + { + Bitmap croppedBitmap = scaleCenterCrop(bitmap, width, height); + Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888); + Canvas canvas = new Canvas(output); + + final int color = 0xff424242; + final Paint paint = new Paint(); + + final Rect rect = new Rect(0, 0, width, height); + final RectF rectF = new RectF(rect); + + paint.setAntiAlias(true); + canvas.drawARGB(0, 0, 0, 0); + paint.setColor(color); + + int radius = 0; + if(width > height) + { + radius = height / 2; + } + else + { + radius = width / 2; + } + + canvas.drawCircle(width / 2, height / 2, radius, paint); + paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); + canvas.drawBitmap(croppedBitmap, rect, rect, paint); + + return output; + } + + public static Bitmap scaleCenterCrop(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; + + float left = (newWidth - scaledWidth) / 2; + float top = (newHeight - scaledHeight) / 2; + + RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight); + + Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig()); + Canvas canvas = new Canvas(dest); + canvas.drawBitmap(source, null, targetRect, null); + + return dest; + } +}
\ No newline at end of file |