您的位置:首页 > 移动开发 > Android开发

从零开始学android:Android事件处理—触摸事件

2013-12-07 22:42 417 查看

触摸事件

触摸事件(OnTouchListener)指的是当用户接触到屏幕之后所产生的一种事件形式,而当用户在屏幕上划过时,可以使用触摸事件取得用户当前的坐标,OnTouchListener接口定义如下:

public interface View.OnTouchListener {
	public abstract boolean onTouch (View v, MotionEvent event) ;
}


范例一:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
	
    <TextView 
        android:id="@+id/info"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</LinearLayout>


定义Activity程序完成触摸事件

package com.richard.ontouchlistener;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView info = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.info = (TextView) super.findViewById(R.id.info) ;
		this.info.setOnTouchListener(new OnTouchListenerImpl());
		
	}
	
	private class OnTouchListenerImpl implements OnTouchListener{

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			MainActivity.this.info.setText("X = " + event.getX() + ",Y = " + event.getY());		//设置文本		
			
			return false;
		}
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


测试效果:



范例二:

编写printView类

package com.richard.myprintview;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MyPaintView extends View {
	private List<Point> allPoint = new ArrayList<Point>();		// 保存所有的坐标点
	
	public MyPaintView(Context context ,AttributeSet set) {		// 构造方法
		super(context, set);									// 调用父类构造
		super.setBackgroundColor(Color.WHITE); 					// 设置背景颜色
		super.setOnTouchListener(new OnTouchListenerImpl());	// 设置触摸事件
	}

	private class OnTouchListenerImpl implements OnTouchListener {	// 触摸事件
		@Override
		public boolean onTouch(View v, MotionEvent event) {			// 判断按下
			// 使用Point类记录当前的X和Y坐标
			Point p = new Point((int) event.getX(), (int) event.getY());
			if (event.getAction() == MotionEvent.ACTION_DOWN) {		// 判断抬起
				allPoint = new ArrayList<Point>();					// 开始新的记录
				allPoint.add(p);									// 记录坐标点
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				allPoint.add(p);									// 记录坐标点
				MyPaintView.this.postInvalidate();						// 重绘
			} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
				allPoint.add(p); 									// 记录坐标点
				MyPaintView.this.postInvalidate();						// 重绘
			}
			return true;
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {							// 进行绘图
		Paint p = new Paint();										// 进行绘图
		p.setColor(Color.RED);										// 设置颜色
		if (allPoint.size() > 1) {									// 保存有坐标
			Iterator<Point> iter = allPoint.iterator();				// 迭代输出坐标
			Point first = null;										// 开始点
			Point last = null;										// 结束点
			while (iter.hasNext()) {								// 迭代输出
				if (first == null) {								// 找到开始点
					first = (Point) iter.next();
				} else {
					if (last != null) {
						first = last;								// 修改开始点
					}
					last = (Point) iter.next();						// 结束点
					canvas.drawLine(first.x, first.y, last.x, last.y, p);	// 画线
				}
			}
		}
	}
}


编写布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
	<com.richard.myprintview.MyPaintView 
	    android.id="@+id/paintView"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"/>    
</LinearLayout>


Activity类:

package com.richard.myprintview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


测试效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: