android学习笔记之运动事件MotionEvent

运动事件的处理

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 中还包含了多点触摸的信息,当有多个触点同时起作用的时候,可以获得触点的数目和每一个触点的坐标。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值