Facial recognition-text corresponding to the display of touch Images
XML file:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: id = "@ + ID/touch_area" Android: layout_width = "fill_parent" Android: layout_height = "300dip" Android: textcolor = "# ffffff" Android: background = "@ drawable/renwu" (insert the picture you want to insert and display it above the program) Android: TEXT = "Touch event test area"> </textview> <textview Android: Id = "@ + ID/event_label" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = ""> (used to display the content displayed when you touch an image) </textview> </linearlayout>
Java file:
Package COM. toucheventdemo; import android. app. activity; import android. OS. bundle; import android. view. motionevent; import android. view. view; import android. widget. textview; public class toucheventdemo extends activity {private textview labelview, touchview; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); labelview = (textview) findviewbyid (R. id. event_label); touchview = (textview) findviewbyid (R. id. touch_area); touchview. setontouchlistener (new view. ontouchlistener () {public Boolean ontouch (view V, motionevent event) {int action = event. getaction (); Switch (Action) {Case (motionevent. action_down): Display ("action_down", event); break; Case (motionevent. action_move): Display ("action_move", event); break;} return true ;}});} private void display (string eventtype, motionevent event) {int x = (INT) event. getx (); int y = (INT) event. gety (); string msg1 = ""; if (x> 105 & x <190 & Y> 192 & Y <208) {msg1 + = "you have touched my nose" + "\ n ";} else if (x> 95 & x <130 & Y> 160 & Y <180) {msg1 + = "you have touched my eyes" + "\ n ";} else if (x> 140 & x <170 & Y> 230 & Y <250) {msg1 + = "you have touched my mouth" + "\ n";} msg1 + = "relative coordinate:" + String. valueof (x) + "," + String. valueof (y) + "\ n"; (use this to obtain the relative coordinates of the area you are touching, and record it to facilitate the write if statement) labelview. settext (msg1 );}}
Running result:
Facial recognition-text corresponding to the display of touch Images