MyDemo.java
package com.jackie.touchproject;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
}
}
MyPainView.java
package com.jackie.touchproject;
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 MyPainView extends View {
private List<Point> allPoint=new ArrayList<Point>();
public MyPainView(Context context, AttributeSet attrs) {
super(context, attrs);//调用父类的构造
super.setOnTouchListener(new OnTouchListenerImpl());
// TODO Auto-generated constructor stub
}
private class OnTouchListenerImpl implements OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent event) {
Point p=new Point((int)event.getX(),(int)event.getY());
if(event.getAction()==MotionEvent.ACTION_DOWN){
MyPainView.this.allPoint=new ArrayList<Point>();
MyPainView.this.allPoint.add(p);
}else if(event.getAction()==MotionEvent.ACTION_UP){
MyPainView.this.allPoint.add(p);
MyPainView.this.invalidate(); //重绘
}else if(event.getAction()==MotionEvent.ACTION_MOVE){
MyPainView.this.allPoint.add(p);
MyPainView.this.invalidate(); //重绘
}
return true;
}
}
@Override
protected void onDraw(Canvas canvas) {
Paint p=new Paint();
p.setColor(Color.RED);
if(MyPainView.this.allPoint.size()>1){
Iterator<Point> iter=MyPainView.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);
}
}
}
}
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 MyPainView extends View {
private List<Point> allPoint=new ArrayList<Point>();
public MyPainView(Context context, AttributeSet attrs) {
super(context, attrs);//调用父类的构造
super.setOnTouchListener(new OnTouchListenerImpl());
// TODO Auto-generated constructor stub
}
private class OnTouchListenerImpl implements OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent event) {
Point p=new Point((int)event.getX(),(int)event.getY());
if(event.getAction()==MotionEvent.ACTION_DOWN){
MyPainView.this.allPoint=new ArrayList<Point>();
MyPainView.this.allPoint.add(p);
}else if(event.getAction()==MotionEvent.ACTION_UP){
MyPainView.this.allPoint.add(p);
MyPainView.this.invalidate(); //重绘
}else if(event.getAction()==MotionEvent.ACTION_MOVE){
MyPainView.this.allPoint.add(p);
MyPainView.this.invalidate(); //重绘
}
return true;
}
}
@Override
protected void onDraw(Canvas canvas) {
Paint p=new Paint();
p.setColor(Color.RED);
if(MyPainView.this.allPoint.size()>1){
Iterator<Point> iter=MyPainView.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);
}
}
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.jackie.touchproject.MyPainView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.jackie.touchproject.MyPainView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>