如何在android中使用onTouch事件获取画布上的文本

如何在android中使用onTouch事件获取画布上的文本,android,android-canvas,Android,Android Canvas,我在android中使用canvas实现pagecurl效果,现在我必须在同一个画布上实现文本到语音的功能。因此,在单个ontouch事件上有两个不同的功能。到目前为止,我在ontouch上实现了pagecurl效果。现在我对在canvas上获取文本感到困惑。。 我曾经 canvas.drawText("Hello world this is experiment", 300,400 , paint); 在我的应用程序中,文本将动态来自sqlite。有没有办法在触摸屏上检测画布上的文本。当你

我在android中使用canvas实现pagecurl效果,现在我必须在同一个画布上实现文本到语音的功能。因此,在单个ontouch事件上有两个不同的功能。到目前为止,我在ontouch上实现了pagecurl效果。现在我对在canvas上获取文本感到困惑。。 我曾经

 canvas.drawText("Hello world this is experiment", 300,400 , paint);

在我的应用程序中,文本将动态来自sqlite。有没有办法在触摸屏上检测画布上的文本。

当你在画布上添加任何文本时,将该文本放入
Rect()

这样在画布上设置此
Rect()

    protected void onDraw(Canvas canvas){
     final String s = "Hello. I'm some text!";

     Paint p = new Paint();
     Rect bounds = new Rect();
     p.setTextSize(60);

     p.getTextBounds(s, 0, s.length(), bounds);
     float mt = p.measureText(s);
     int bw = bounds.width();

     Log.i("LCG", String.format(
          "measureText %f, getTextBounds %d (%s)",
          mt,
          bw, bounds.toShortString())
      );
     bounds.offset(0, -bounds.top);
     p.setStyle(Style.STROKE);
     canvas.drawColor(0xff000080);
     p.setColor(0xffff0000);
     canvas.drawRect(bounds, p);
     p.setColor(0xff00ff00);
     canvas.drawText(s, 0, bounds.bottom, p);
  }
&您可以很容易地检测到文本的
Rect()
,将ontochevent放在Rect上

有关画布的更多信息,请参阅:

查看此帖子