Created
October 9, 2013 13:10
-
-
Save PureSpider/6901020 to your computer and use it in GitHub Desktop.
Resizes the Bitmap (can also be a Drawable with minor changes) to 32dip and masks and draws the checkmark
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 drawCheck(Bitmap in) { | |
// http://forrst.com/posts/Bitmap_Drawable_Resize_on_Android-mjr | |
// http://stackoverflow.com/questions/14801075/android-how-to-apply-mask-on-imageview | |
float newWidth = 32; | |
float newHeight = 32; | |
Bitmap bitmapOrig = in; | |
DisplayMetrics metrics = new DisplayMetrics(); | |
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | |
windowManager.getDefaultDisplay().getMetrics(metrics); | |
float logicalDensity = metrics.density; | |
float scaleWidth = ((float) newWidth) / bitmapOrig.getWidth(); | |
float scaleHeight = ((float) newHeight) / bitmapOrig.getHeight(); | |
Matrix matrix = new Matrix(); | |
matrix.postScale(scaleWidth * logicalDensity, scaleHeight * logicalDensity); | |
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrig, 0, 0, bitmapOrig.getWidth(), bitmapOrig.getHeight(), matrix, true); | |
BitmapDrawable bitmapDrawableResized = new BitmapDrawable(context.getResources(), resizedBitmap); | |
Bitmap mainImage = bitmapDrawableResized.getBitmap(); | |
Bitmap maskImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.offline_thumb_mask); // get mask image | |
Bitmap checkImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.offline_thumb_check); // get checkmark image | |
Bitmap result = Bitmap.createBitmap(mainImage.getWidth(), mainImage.getHeight(), Bitmap.Config.ARGB_8888); | |
Canvas maskCanvas = new Canvas(result); | |
Paint paint = new Paint(); | |
paint.setFilterBitmap(false); | |
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); | |
maskCanvas.drawBitmap(mainImage, 0, 0, null); | |
maskCanvas.drawBitmap(maskImage, 0, 0, paint); | |
maskCanvas.drawBitmap(checkImage, 0, 0, null); | |
paint.setXfermode(null); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment