歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發經驗之在圖片上隨意點擊移動文字

Android開發經驗之在圖片上隨意點擊移動文字

日期:2017/3/1 10:27:42   编辑:Linux編程

只要在圖片范圍之內,文字可隨意點擊移動。

  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 = 20, y = 40;
  15. private static float windowWidth;
  16. private static float windowHeight;
  17. private static float left = 0; //圖片在屏幕中位置X坐標
  18. private static float top = 0; //圖片在屏幕中位置Y坐標
  19. private String str = "我愛你";
  20. private DisplayMetrics dm = new DisplayMetrics(); //用於獲取屏幕的高度和寬度
  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. //屏幕的寬度
  29. windowWidth = windowManager.getDefaultDisplay().getWidth();
  30. //屏幕的高度
  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. * 返回值: Bitmap 參數:原圖片,文字 功能: 根據給定的文字生成相應圖片
  42. *
  43. * @param originalMap
  44. * @param text 文字
  45. * @param x 點擊的X坐標
  46. * @param y 點擊的Y坐標
  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. // 定義畫布
  55. Canvas canvas = new Canvas(originalMap);
  56. // 定義畫筆
  57. Paint paint = new Paint();
  58. //獲得文本的長度(像素)
  59. float textWidth = paint.measureText(text);
  60. canvas.drawBitmap(originalMap, 0, 0, null);
  61. // 如果圖片寬度小於屏幕寬度
  62. if (left + bitmapWidth < windowWidth)
  63. {
  64. // 右邊界
  65. if (x >= left + bitmapWidth - textWidth)
  66. {
  67. x = left + bitmapWidth - textWidth;
  68. }
  69. // 左邊界
  70. else if (x <= left)
  71. {
  72. x = left;
  73. }
  74. }
  75. else
  76. {
  77. // 右邊界
  78. if (x >= windowWidth - textWidth)
  79. {
  80. x = windowWidth - textWidth;
  81. }
  82. // 左邊界
  83. else if (x <= 0)
  84. {
  85. x = 0;
  86. }
  87. }
  88. // 如果圖片高度小於屏幕高度
  89. if (top + bitmapHeight < windowHeight)
  90. {
  91. // 下
  92. if (y >= top + bitmapHeight)
  93. {
  94. y = top + bitmapHeight;
  95. }
  96. // 上
  97. else if (y <= top + 10)
  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. // 添加字
  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. // 重繪
  125. invalidate();
  126. }
  127. return true;
  128. }
  129. }


  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. }
Copyright © Linux教程網 All Rights Reserved