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