3

我正在使用 android 中的画布实现 pagecurl 效果现在我必须在同一个画布上实现文本到语音功能..所以单个 ontouch 事件上有两个不同的功能..到目前为止我实现了 pagecurl 效果 onTouch 现在我很困惑在画布上获取文本.. 我用了

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

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

4

1 回答 1

1

当您在画布上添加任何文本时,将该文本放入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()到文本放在矩形上的触摸事件。

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

http://developer.android.com/reference/android/graphics/Canvas.html

于 2013-01-22T06:27:41.230 回答