Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. // size of the text in the TextView on the ImageView
  2. textSize = topTextView.getTextSize();
  3.  
  4. // area of the ImageView
  5. imageViewArea = ((imageView.getWidth()) * (imageView.getHeight()));
  6.  
  7. // area of the Canvas
  8. canvasArea = ((canvas.getWidth()) * (canvas.getHeight()));
  9.  
  10. // cross multiplication
  11. x = (((textSize)/(imageViewArea)) * (canvasArea));
  12.  
  13. public Bitmap createMeme(ImageView img){
  14. BitmapDrawable bitmapDrawable = ((BitmapDrawable) img.getDrawable());
  15. Bitmap bitmap = bitmapDrawable.getBitmap();
  16. Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
  17.  
  18. String topText = topTextView.getText().toString();
  19. String bottomText = bottomTextView.getText().toString();
  20.  
  21. topText = topText.toUpperCase();
  22. bottomText = bottomText.toUpperCase();
  23.  
  24. Canvas canvas = new Canvas(mutableBitmap);
  25.  
  26. /*Below is the code for cross-multiplication.
  27. * The purpose is to assign a text size for the text drawn on the canvas
  28. * relative to the text size on the preview to get the same
  29. * text appearance*/
  30. textSize = topTextView.getTextSize();
  31. imageViewArea = ((imageView.getWidth()) * (imageView.getHeight()));
  32. canvasArea = ((canvas.getWidth()) * (canvas.getHeight()));
  33. x = (((textSize)/(imageViewArea)) * (canvasArea));
  34.  
  35. TextPaint topFillPaint = new TextPaint();
  36. TextPaint bottomFillPaint = new TextPaint();
  37.  
  38. TextPaint topStrokePaint = new TextPaint();
  39. TextPaint bottomStrokePaint = new TextPaint();
  40.  
  41. Typeface typeface = getResources().getFont(R.font.impact);
  42.  
  43. topFillPaint.setColor(Color.WHITE);
  44. topFillPaint.setTextSize(x);
  45. topFillPaint.setTypeface(typeface);
  46.  
  47. topStrokePaint.setStyle(Paint.Style.STROKE);
  48. topStrokePaint.setStrokeWidth(8);
  49. topStrokePaint.setTextSize(x);
  50. topStrokePaint.setColor(Color.BLACK);
  51. topStrokePaint.setTypeface(typeface);
  52.  
  53. bottomFillPaint.setColor(Color.WHITE);
  54. bottomFillPaint.setTextSize(x);
  55. bottomFillPaint.setTypeface(typeface);
  56.  
  57. bottomStrokePaint.setStyle(Paint.Style.STROKE);
  58. bottomStrokePaint.setStrokeWidth(8);
  59. bottomStrokePaint.setColor(Color.BLACK);
  60. bottomStrokePaint.setTextSize(x);
  61. bottomStrokePaint.setTypeface(typeface);
  62.  
  63. StaticLayout topFillLayout = new StaticLayout(topText, topFillPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
  64. 0.8f, 0.0f, false);
  65. StaticLayout topStrokeLayout = new StaticLayout(topText, topStrokePaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
  66. 0.8f, 0.0f, false);
  67. StaticLayout bottomFillLayout = new StaticLayout(bottomText, bottomFillPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
  68. 0.8f, 0.0f, false);
  69. StaticLayout bottomStrokeLayout = new StaticLayout(bottomText, bottomStrokePaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
  70. 0.8f, 0.0f, false);
  71.  
  72. topFillLayout.draw(canvas);
  73.  
  74. topStrokeLayout.draw(canvas);
  75.  
  76. canvas.translate(0, canvas.getHeight() - 210);
  77. bottomFillLayout.draw(canvas);
  78.  
  79. bottomStrokeLayout.draw(canvas);
  80.  
  81. return mutableBitmap;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement