本文主要是介绍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;@Overrideprotected 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;}@Overridepublic 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 中还包含了多点触摸的信息,当有多个触点同时起作用的时候,可以获得触点的数目和每一个触点的坐标。
这篇关于android学习笔记之运动事件MotionEvent的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!