Android的动态文本图纸/触摸检测 [英] Android dynamic text drawing / touch detection

查看:142
本文介绍了Android的动态文本图纸/触摸检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示的文字(比较大的),并确定哪些字符用户触摸。我也想通过画在某些字符一个圆圈(例如)美化的文字。我怎么知道哪些字符是在什么位置?强制等宽字体将被罚款,如果有帮助的编程。

I want to display text (relatively large) and determine which character the user touches. I'd also like to embellish the text by drawing a circle (for example) over certain characters. How do I know which characters are at what location? Enforcing monospaced font would be fine if helpful programmatically.

难道还能够使文本滚动的这一行左/右(使用触摸>幻灯片),仍然有接触的工作,以确定正确的字符?

Would it also be able to make this one line of text scroll left/right (using touch->slide) and still have touch work to determine the correct character?

推荐答案

一个很广泛的问题,你有很多工作要做,但这里的一个可能的解决方案大纲:

A very broad question, and you've got a lot of work ahead, but here's the outline of one possible solution:


  • 创建自定义视图。 ImageView的往往是一个很好的基类
       继承。

  • Create a custom view. ImageView is often a good base class to inherit.

重写视图的onDraw()方法。

Override the onDraw() method of the view.

使用canvas.drawText()把你的屏幕上的文字。

Use canvas.drawText() to place your text on screen.

使用paint.getTextBounds到meausure文本。查看讨论
   下面链接为透彻理解。由于您绘制的文本
   在屏幕上,你测量,你确切地知道每个
   性格。

Use paint.getTextBounds to meausure your text. See the discussion linked below for a thorough understanding. Since you drew the text on screen, and you measured it, you know exactly where each character is.

覆盖在你的自定义视图中onTouch()方法和跟踪
   你需要的事件。有很多的问题答案在这里等如何做到这一点。

Override the onTouch() method in your custom view and track the events you need. There are many QAs here in SO on how to do this.

有关滚动文本,使用使用移动canvas.translate()
   您在onTouch计算()。既然你知道有多少你
   滚动(翻译),你知道你的角色已经移动的距离
   并且可以抵消你的触摸检测来检测触摸字符

For scrolling the text, use canvas.translate() using the movement you calculate in the onTouch(). Since you know how much you scrolled (translated), you know how far your characters have moved and can offset your touch detection to detect character touches.

最后,因为你现在控制所有的绘图和,你
   字符位置是抽象的,只有具有意义进行比较时
   以触​​摸位置,你可以以任何方式选择美化他们。

Finally, since you now control all of the drawing and, your character positions are abstract, only having meaning when compared to a touch position, you can embellish them in any way choose.

Android的油漆:.measureText()VS .getTextBounds()

唷!

祝你好运。

这篇关于Android的动态文本图纸/触摸检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆