How to get text on canvas using onTouch event in android
I am implementing pagecurl effect using canvas in android, now I need to implement text to speech functionality on the same canvas. So two different functions in one ontouch event. Now I have implemented the pagecurl onTouch effect, now I am confused to get the text on the canvas. I used
canvas.drawText("Hello world this is experiment", 300,400 , paint);
in my application the text will dynamically come out of sqlite. Is there a way to detect text on canvas when touched.
When u add text to canvas, put that text in Rect()
set this one Rect()
to canvas. Thus.
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 the Rect()
text placed onTouchevent in the rectangle.
For more on canvas see this:
http://developer.android.com/reference/android/graphics/Canvas.html
source to share