android学习笔记之运动事件MotionEvent

来源:互联网 发布:跟兄弟连学php.pdf下载 编辑:程序博客网 时间:2024/05/19 19:31

运动事件的处理

1.概述

在android中,触摸屏和滚动球的实现,主要可以通过使用运动事件( MotionEvent)用于接收它们的信息。

public boolean onTouchEvent(MotionEvent event)public boolean onTrackballEvent(MotionEvent event)

触摸屏和滚动球事件主要通过实现以下上2 个函数来接收。

2.运行结果
.这里写图片描述

3.布局文件 AndroidManifest.xml 的内容如下所示:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.testmotionevent"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.testmotionevent.TestMotionEvent"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

4.程序的代码如下所示:

package com.example.testmotionevent;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.Menu;import android.view.MotionEvent;import android.widget.TextView;public class TestMotionEvent extends Activity {    private static final String TAG="TestMotionEvent";    TextView mAction;    TextView mPosition;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_test_motion_event);        mAction=(TextView)findViewById(R.id.action);        mPosition=(TextView)findViewById(R.id.positon);    }    public boolean onTouchEvent(MotionEvent event){        int Action =event.getAction();        float X=event.getX();        float Y=event.getY();        Log.v(TAG,"Action="+Action);        Log.v(TAG,"("+X+","+Y+")");        mAction.setText("Action="+Action);        mPosition.setText("Position=("+X+","+Y+")");        return true;    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.test_motion_event, menu);        return true;    }}

MotionEvent 是用于处理运动事件的类,这个类中可以获得动作的类型、动作的坐标,MotionEvent 中还包含了多点触摸的信息,当有多个触点同时起作用的时候,可以获得触点的数目和每一个触点的坐标。

0 0
原创粉丝点击