Android--多点触控事件捕捉

来源:互联网 发布:淘宝交保证金的类目 编辑:程序博客网 时间:2024/05/21 17:02

   本实例实现的多点触控,当用户一个手指点击屏幕时,显示的是一个触控动作,两个手指点击显示两个,没有则显示为0.

下面给出实现的截图对比:


下面给出本实例的源代码:

package irdc.ex07_20;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.widget.TextView;public class EX07_20 extends Activity{  private TextView myText1;  private TextView myText2;    /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    /* 加载main.xml Layout */    setContentView(R.layout.main);    /* TextView初始化 */    myText1=(TextView)findViewById(R.id.text1);    myText2=(TextView)findViewById(R.id.text2);  }    /* 重写onTouchEvent() */    @Override  public boolean onTouchEvent(MotionEvent event)   {       /* 显示触碰点的数量 */    myText2.setText(""+event.getPointerCount());        /* event的Action?断 */    switch(event.getAction())    {      /* 触碰事件发生 */      case MotionEvent.ACTION_DOWN:        myText1.setText(getResources().getString(R.string.act1));        break;      /* 触碰事件结束 */      case MotionEvent.ACTION_UP:        myText1.setText(getResources().getString(R.string.act2));        /* 显示点数为0 */        myText2.setText("0");        break;      /* 第1个触碰点被按下 */      case MotionEvent.ACTION_POINTER_1_DOWN:        myText1.setText(getResources().getString(R.string.act3));        break;      /* 第1个触碰点被移除 */      case MotionEvent.ACTION_POINTER_1_UP:        myText1.setText(getResources().getString(R.string.act4));        break;      /* 第2个触碰点被按下 */      case MotionEvent.ACTION_POINTER_2_DOWN:        myText1.setText(getResources().getString(R.string.act5));        break;      /* 第2个触碰点被移除 */      case MotionEvent.ACTION_POINTER_2_UP:        myText1.setText(getResources().getString(R.string.act6));        break;      default:        break;    }    return super.onTouchEvent(event);  }}

布局文件如下:

<?xml version="1.0" encoding="utf-8"?><FrameLayout   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/text1"    android:textSize="24sp"    android:textColor="#FFFFFF"    android:layout_width="fill_parent"     android:layout_height="wrap_content"    android:text="请触碰屏幕以触发事件"  />  <TextView    android:id="@+id/text2"    android:textSize="80sp"    android:textColor="#FFFFFF"    android:layout_width="fill_parent"     android:layout_height="fill_parent"    android:gravity="center"    android:text="0"  /></FrameLayout>


原创粉丝点击