在main.xml文件中代码如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<com.tarena.touch.MyPaintView

android:id="@+id/paintView"

android:layout_width="fill_parent"

android:layout_height="fill_parent"/>

</LinearLayout>

在MyPaintView.java中代码如下:

package com.tarena.touch;

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 attrs) { //接收Context,同时接收属性集合

super(context, attrs); //调用父类的构造

super.setOnTouchListener(new OnTouchListenerImpl());

}

private class OnTouchListenerImpl implements OnTouchListener{

public boolean onTouch(View v, MotionEvent event) {

Point p = new Point((int) event.getX(),(int) event.getY()); //将坐标保存在Point类中

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; //表示下面的操作不再执行

}

}

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中代码如下:

package com.tarena.touch;

import android.app.Activity;

import android.os.Bundle;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnTouchListener;

import android.widget.TextView;

public class MyTouchDemo extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.main);

}

}

更多相关文章

  1. Android不同层次开启硬件加速的方式
  2. Android学习中遇到的问题及解决方案
  3. Android(安卓)中自定义View的应用
  4. Android(安卓)View回顾之坐标系
  5. Android(安卓)教你如何通过 LocationManager 获取得到当前位置坐
  6. Android学习之Android本地存储的五种方式
  7. Android如何保存和读取设置
  8. Android:控件AutoCompleteTextView 客户端保存搜索历史自动提示
  9. Android下使用Properties文件保存程序设置

随机推荐

  1. Android 仿微信支付密码界面
  2. Android 定时获取系统内存信息
  3. Android OEM 厂商提供的 USB驱动
  4. Android 采用Pull解析XML内容 【学习记录
  5. Android 仿苹果底部弹出Dialog
  6. Android 之欢迎界面Splash
  7. android分享应用工具类
  8. Android 实现Activity后台运行
  9. Android ImageSwitcher
  10. android判断手机有没有安装微博