只要在图片范围之内,文字可随意点击移动。

[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 = 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. }
[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开发经验之在图片上随意点击移动文字相关推荐

  1. Android WebView 支持H5图片上传input type=file

    2019独角兽企业重金招聘Python工程师标准>>> Android WebView 缓存处理 Android WebView 支持H5图片上传<input type=&qu ...

  2. css如何实现鼠标移至图片上显示遮罩层及文字

    css如何实现鼠标移至图片上显示遮罩层及文字 问题:一张图片当鼠标移动到上方时,会显示一个遮罩层,并且显示一些提示文字 html: <div class="contentimg&quo ...

  3. 鼠标悬停 -css如何实现鼠标移至图片上显示遮罩层及文字

    搜索这个效果的时候,很多人都是用css结合jQuery实现的,其实直接用css也可以实现哦~ 效果前: 效果后: 代码: <!DOCTYPE html> <html lang=&qu ...

  4. Android实现拍照相册图片上传功能

    更改头像功能不像修改信息一样直接提交参数就可以,需要上传图片文件 我就直接贴代码了首先给出布局文件 <ImageViewandroid:id="@+id/iv"android ...

  5. android 自定义图片上传,android自定义ImageView仿图片上传示例

    看下效果图 主要看下自定义view 代码 public class ProcessImageView extends ImageView{ private Context context; priva ...

  6. android 自定义图片上传,android自定义ImageView仿图片上传(示例代码)

    Activity代码 1 public classMainActivity extends AppCompatActivity {2     ProcessImageView processImage ...

  7. android 队列上传图片,话说android端七牛图片上传

    七牛图片上传业务流程如下图(这是官方的图): 由上图可知,要想实现图片上传,是要三端进行交互的(我刚刚开始以为只要七牛服务器跟客户端交互就行) 接下来步骤如下: 1.首先肯定是要有一个七牛的账号,并创 ...

  8. 话说android端七牛图片上传

    七牛图片上传业务流程如下图(这是官方的图): 由上图可知,要想实现图片上传,是要三端进行交互的(我刚刚开始以为只要七牛服务器跟客户端交互就行) 接下来步骤如下: 1.首先肯定是要有一个七牛的账号,并创 ...

  9. HTML如何在图片上添加内容(如文字,菜单栏,按钮等)

    HTML如何在图片上添加内容 演示效果 最近做的一个效果是网页头部有一张大图,菜单栏漂浮在图片之上.我看到网上讲的很简单,大致原理就是图片定为相对定位,文字定为绝对定位即可.不过我实现的并不顺利,花了 ...

最新文章

  1. ORA-24247: network access denied by access control list (ACL)
  2. 原创 | 数据资产确权浅议
  3. Paramiko: SSH and SFTP With Python
  4. 使用stream类型的Result实现Ajax
  5. 关于前后端交互的一些基础知识点
  6. 文献记录(part66)--一种基于交叉熵的社区发现算法
  7. 现在有一个map集合如下: Map<Integer,String> map = new HashMap<Integer, String>(); map.put(1, “
  8. 用c语言加密,求助:如何用C语言实现LFSR加密
  9. div和div之间画横线,如何在两个div之间画一条线?
  10. ASCII对应码表(键值)
  11. 理解苏宁:互联网转型之战
  12. 三菱a系列motion软体_三菱各类伺服电机标准参数一览表
  13. python改变当前工作目录_在python中更改当前工作目录
  14. hive中reduce类函数说明
  15. ปอเช็ตติโน่ เสียดายโอกาสของทีมในเกมฟัดหงส์
  16. 烤仔建工承建,著名画家孙天骄的元宇宙美术馆即将开门迎客
  17. 多款国产手机无一幸免:人脸识别被破解
  18. 引入第三方sdk错误提示
  19. 【OpenGL ES】立方体贴图(6张图)
  20. 测绘资质高性能数据服务器,测绘资质分级标准2020年

热门文章

  1. 华为ipd产品开发流程_华为集成产品开发(IPD)流程的解读
  2. 图像降噪算法——Variance Stabilizing Transform / Generalization Anscombe Transform算法
  3. 视觉SLAM总结——LSD SLAM中关键知识点总结
  4. 【每周CV论文推荐】 初学GAN必须要读的文章
  5. lazadashopee代运营服务有哪些,能帮商家解决哪些问题?
  6. 中国杀菌剂行业需求趋势与投资战略规划研究报告2021-2027年版
  7. 粤东农批项目座谈会 农业大健康·李喜贵:功能性农业差异化加工
  8. 世界农业发展史-国际农民丰收节贸易会:人类的发展史
  9. websocket与ajax的区别浅析
  10. 洛谷 P2197 nim游戏