Created
August 3, 2012 09:34
-
-
Save yava555/3246311 to your computer and use it in GitHub Desktop.
AndroidRoundCornerImage
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 getRoundedCornerBitmap(Bitmap bitmap) { | |
if (bitmap == null) { | |
return null; | |
} | |
if (layoutId==R.layout.appwidget_small_one_layout && styleOneBitmap == null) { | |
styleOneBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.appwidget_one); | |
} | |
if (layoutId==R.layout.appwidget_small_two_layout && styleTwoBitmap == null) { | |
styleTwoBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.appwidget_two); | |
} | |
Bitmap styleBitmap = null; | |
if (layoutId==R.layout.appwidget_small_one_layout) { | |
styleBitmap = styleOneBitmap; | |
}else{ | |
styleBitmap = styleTwoBitmap; | |
} | |
Bitmap output = Bitmap.createBitmap(styleBitmap.getWidth(), styleBitmap.getHeight(), Config.ARGB_8888); | |
Canvas canvas = new Canvas(output); | |
final Rect styleRect = new Rect(0, 0, styleBitmap.getWidth(), styleBitmap.getHeight()); | |
final int color = 0xff424242; | |
final Paint paint = new Paint(); | |
paint.setAntiAlias(true); | |
canvas.drawARGB(0, 0, 0, 0); | |
paint.setColor(color); | |
canvas.drawBitmap(styleBitmap, styleRect, styleRect, paint); | |
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); | |
int height; | |
int width; | |
int left; | |
int top; | |
if ((float)bitmap.getHeight() / bitmap.getWidth() > (float)styleBitmap.getHeight() / styleBitmap.getWidth()) { | |
width = bitmap.getWidth(); | |
height = bitmap.getWidth() * styleBitmap.getHeight() / styleBitmap.getWidth(); | |
left = 0; | |
top = (bitmap.getHeight() - height) / 2; | |
} else { | |
height = bitmap.getHeight(); | |
width = bitmap.getHeight() * styleBitmap.getWidth() / styleBitmap.getHeight(); | |
top = 0; | |
left = (bitmap.getWidth() - width) / 2; | |
} | |
final Rect bmRect = new Rect(left, top, width, height); | |
canvas.drawBitmap(bitmap, bmRect, styleRect, paint); | |
return output; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment