控件:TouchProject(绘图) --- 触摸事件
main.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<org.lxh.demo.MyPaintView
android:id="@+id/paintView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
MyPaintView.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package org.lxh.demo;
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>();
// 接收Context,同时接收属性集合
public MyPaintView(Context context, AttributeSet attrs) {
// 调用父类的构造
super(context, attrs);
super.setOnTouchListener(new OnTouchListenerImpl());
}
private class OnTouchListenerImpl implements OnTouchListener {
public boolean onTouch(View v, MotionEvent event) {
// 将坐标保存在Point类
Point p = new Point((int) event.getX(), (int) event.getY());
// 按下,表示重新开始保存点
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// 重绘
MyPaintView.this.allPoint = new ArrayList<Point>();
// 保存点
MyPaintView.this.allPoint.add(p);
// 用户松开
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// 记录坐标点
MyPaintView.this.allPoint.add(p);
// 重绘图形
MyPaintView.this.postInvalidate();
// 用户移动
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
// 记录坐标点
MyPaintView.this.allPoint.add(p);
// 重绘图形
MyPaintView.this.postInvalidate();
}
return true; // 表示下面的操作不再执行了。
}
}
@Override
protected void onDraw(Canvas canvas) { // 进行绘图
// 依靠此类开始画线
Paint p = new Paint();
// 定义图的颜色
p.setColor(Color.RED);
// 现在有坐标点保存的时候可以开始进行绘图
if (MyPaintView.this.allPoint.size() > 1) {
Iterator<Point> iter = MyPaintView.this.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);
}
}
}
}
}
MyTouchDemo.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package org.lxh.demo;
import android.app.Activity;
import android.os.Bundle;
public class MyTouchDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
}
}
AndroidManifest.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.lxh.demo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="5" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyTouchDemo"
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>