人脸识别之触摸图片显示对应的文字

人脸识别之触摸图片显示对应的文字

xml文件:    

<?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" (插入你所需要插入的图片,显示在程序的上方)
    android:text="触摸事件测试区域"> 
</TextView>

<TextView 
    android:id="@+id/event_label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="" >(用来显示你触摸图片对应显示的内容)
</TextView>
</LinearLayout>

java文件:

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 +="你摸到我的鼻子了"+"\n";
    	}
    	else if(x>95&&x<130&&y>160&&y<180)
    	{
    		msg1 +="你摸到我的眼睛了"+"\n";
    	}
    	else if(x>140&&x<170&&y>230&&y<250)
    	{
    		msg1 +="你摸到我的嘴了"+"\n";
    	}
    		
    	msg1 += "相对坐标:"+String.valueOf(x)+","+String.valueOf(y)+"\n"; (通过这个获取你所触摸区域的相对坐标,记录下来,以方便写 if 语句)
    	
    	
    	labelView.setText(msg1); 
     } 
}

 

运行结果截图:

posted @ 2014-10-18 15:20  Rui.peng  阅读(457)  评论(0编辑  收藏  举报