Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. /**
  2. * Draw one image over other using the canvas paint and path drawing.
  3. */
  4. public Bitmap createMaskedImageInImageCenterRightMug(Drawable back,
  5. Bitmap bitmapToDrawInTheCenter) {
  6.  
  7. Bitmap backgroundBitmap = ((BitmapDrawable) back).getBitmap();
  8. int hieghtBack = backgroundBitmap.getHeight();
  9. int widthBack = backgroundBitmap.getWidth();
  10. int hieghtFront = bitmapToDrawInTheCenter.getHeight();
  11. int widthFront = bitmapToDrawInTheCenter.getWidth();
  12. int widthToDrawOnMug = widthBack / 2;
  13. backgroundBitmap = Bitmap.createScaledBitmap(backgroundBitmap, (int) canvas_width, hieghtBack, true);
  14. // Create mask
  15. Bitmap backgroundBitmapMask = Bitmap.createBitmap(backgroundBitmap, 7, 0, (int) (canvas_width / 2), hieghtBack);
  16. if (widthToDrawOnMug <= widthFront) {
  17. bitmapToDrawInTheCenter = Bitmap.createBitmap(
  18. bitmapToDrawInTheCenter, (widthFront * 40) / 100, 0,
  19. (widthFront * 60) / 100, hieghtFront);
  20. }
  21. Bitmap backgroundBitmapScaledMask = Bitmap.createScaledBitmap(
  22. backgroundBitmapMask, widthToDrawOnMug, hieghtBack - 50, true);
  23.  
  24. bitmapToDrawInTheCenter = Bitmap.createScaledBitmap(
  25. bitmapToDrawInTheCenter, backgroundBitmapScaledMask.getWidth(),
  26. backgroundBitmapScaledMask.getHeight(), true);
  27. Bitmap resultBitmap = Bitmap.createBitmap(
  28. backgroundBitmapScaledMask.getWidth(),
  29. backgroundBitmapScaledMask.getHeight(),
  30. backgroundBitmapScaledMask.getConfig());
  31. Canvas canvas = new Canvas(resultBitmap);
  32. Paint paint = new Paint();
  33. paint.setAntiAlias(true);
  34. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
  35. canvas.drawBitmap(backgroundBitmapScaledMask, 0, 0, null);
  36. canvas.drawBitmap(bitmapToDrawInTheCenter, 0, 0, paint);
  37. return resultBitmap;
  38. }
  39.  
  40. /**
  41. * Draw one image over other using the canvas paint and path drawing.
  42. */
  43. public Bitmap pasteOverMugForRightMug(Drawable back,
  44. Bitmap bitmapToDrawInTheCenter) {
  45. Bitmap backgroundBitmap = ((BitmapDrawable) back).getBitmap();
  46. Bitmap resultBitmap = Bitmap.createBitmap(backgroundBitmap.getWidth(), backgroundBitmap.getHeight(), backgroundBitmap.getConfig());
  47. Canvas canvas = new Canvas(resultBitmap);
  48. canvas.drawBitmap(backgroundBitmap, new Matrix(), null);
  49. Paint paint = new Paint();
  50. canvas.drawBitmap(bitmapToDrawInTheCenter, 0, 25, paint);
  51. return resultBitmap;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement