3

I am implementing pagecurl effect using canvas in android now i have to implement text to speech functionality on same canvas..So two different functionalities on single ontouch event ..Upto now i implemented pagecurl effect onTouch now i am in confusion to get text on canvas.. i used

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

in my app the text will come dynamically from sqlite..Is there any way to detect text on canvas on touch.

1 Answers1

1

When u Add any text on canvas put that text in Rect()

set this Rect() on Canvas Like this way.

    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);
  }

& you easily detect Rect() of Text put onTouchevent on rect.

for more About canvas refer this:

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

Dixit Patel
  • 6,179
  • 1
  • 31
  • 42