Android Development experience at random click on the picture to move the text

Source: Internet
Author: User

As long as within the image range, the text can be arbitrarily clicked to move.

[Java]View PlainCopy
  1. Package Xiaosi. Gettextimage;
  2. Import Android.content.Context;
  3. Import android.content.res.Resources;
  4. Import Android.graphics.Bitmap;
  5. Import Android.graphics.BitmapFactory;
  6. Import Android.graphics.Canvas;
  7. Import Android.graphics.Paint;
  8. Import Android.util.DisplayMetrics;
  9. Import android.view.MotionEvent;
  10. Import Android.view.View;
  11. Import Android.view.WindowManager;
  12. Public class Gettextimage extends View
  13. {
  14. private Float x = , y = 40;
  15. private static float windowwidth;
  16. private static float windowheight;
  17. private static float left = 0; //Picture in the screen position x coordinate
  18. private static float top = 0; //Picture in the screen position y coordinate
  19. private String str = "I love You";
  20. private displaymetrics DM = new Displaymetrics (); //To get the height and width of the screen
  21. private WindowManager WindowManager;
  22. private Bitmap Newbitmap;
  23. Public Gettextimage (context context)
  24. {
  25. super (context);
  26. WindowManager = (WindowManager) context
  27. . Getsystemservice (Context.window_service);
  28. //width of the screen
  29. WindowWidth = Windowmanager.getdefaultdisplay (). GetWidth ();
  30. //height of screen
  31. WindowHeight = Windowmanager.getdefaultdisplay (). GetHeight ();
  32. }
  33. public void OnDraw (canvas canvas)
  34. {
  35. Resources res = getresources ();
  36. Bitmap BMP = Bitmapfactory.decoderesource (res, r.drawable.b);
  37. Newbitmap = gettextimage (BMP, str, x, y);
  38. Canvas.drawbitmap (Newbitmap, 0, 0, null);
  39. }
  40. /** 
  41. * Return value: Bitmap parameter: Original picture, text function: generate the corresponding picture according to the given text
  42. *
  43. * @param originalmap
  44. * @param text text
  45. * @param x Click X-coordinate
  46. * y-coordinate @param y-click
  47. * @return
  48. */
  49. public static Bitmap gettextimage (Bitmap originalmap, String text, float x,
  50. float y)
  51. {
  52. float bitmapwidth = Originalmap.getwidth ();
  53. float bitmapheight = Originalmap.getheight ();
  54. //define Canvas
  55. Canvas canvas = new Canvas (ORIGINALMAP);
  56. //define brushes
  57. Paint paint = new paint ();
  58. //Get the length of the text (in pixels)
  59. float textWidth = paint.measuretext (text);
  60. Canvas.drawbitmap (Originalmap, 0, 0, null);
  61. //If the picture width is less than the screen width
  62. if (left + bitmapwidth < windowwidth)
  63. {
  64. //Right Border
  65. if (x >= left + bitmapwidth-textwidth)
  66. {
  67. x = left + bitmapwidth-textwidth;
  68. }
  69. //Left Border
  70. Else if (x <= left)
  71. {
  72. x = left;
  73. }
  74. }
  75. Else
  76. {
  77. //Right Border
  78. if (x >= windowwidth-textwidth)
  79. {
  80. x = Windowwidth-textwidth;
  81. }
  82. //Left Border
  83. Else if (x <= 0)
  84. {
  85. x = 0;
  86. }
  87. }
  88. //If the picture height is less than the screen height
  89. if (top + bitmapheight < WindowHeight)
  90. {
  91. //Down
  92. if (y >= top + bitmapheight)
  93. {
  94. y = top + bitmapheight;
  95. }
  96. //On
  97. Else if (y <= top + Ten)
  98. {
  99. y = top + 10;
  100. }
  101. }
  102. Else
  103. {
  104. if (y >= windowheight)
  105. {
  106. y = windowheight;
  107. }
  108. Else if (y <= 0)
  109. {
  110. y = 0;
  111. }
  112. }
  113. //Add word
  114. Canvas.drawtext (text, x, y, paint);
  115. return originalmap;
  116. }
  117. @Override
  118. Public Boolean ontouchevent (Motionevent event)
  119. {
  120. if (event.getaction () = = Motionevent.action_down)
  121. {
  122. x = Event.getx ();
  123. y = event.gety ();
  124. // Redraw
  125. Invalidate ();
  126. }
  127. return true;
  128. }
  129. }


[Java]View PlainCopy
  1. Package Xiaosi. Gettextimage;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Public class Gettextimageactivity extends Activity {
  5. /** Called when the activity is first created. * /
  6. private Gettextimage get;
  7. @Override
  8. public void OnCreate (Bundle savedinstancestate) {
  9. super.oncreate (savedinstancestate);
  10. get = new Gettextimage (this);
  11. Setcontentview (get);
  12. }
  13. }


Android Development experience at random click on the picture to move the text

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.