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

本文详细介绍了Android中的触摸事件OnTouchListener的应用,包括如何通过监听获取用户的触摸坐标,并提供了两个实例:一个展示如何更新视图上的坐标信息;另一个展示了如何利用触摸事件绘制路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

触摸事件

触摸事件(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;
	}

}

测试效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

e421083458

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值