控件:TouchProject(绘图) --- 触摸事件_第1张图片

main.xml

View Code
<?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

View Code
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

View Code
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

View Code
<?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>

控件:TouchProject(绘图) --- 触摸事件_第2张图片

更多相关文章

  1. android中的坐标变换
  2. 控件_RadioGroup&&RadioButton(单选按钮)和Toast
  3. Android的xml布局文件代码讲解(TextView控件)
  4. Android 三角标签控件、角标(AvatarLabelView)
  5. Android在源图片上的XY坐标再画一个图片
  6. Android23-视图坐标系以及MotionEvent事件
  7. [Android]ButterKnife-无尽之刃-绑定视图控件和事件的快速开发工

随机推荐

  1. Tomcat 介绍及使用教程
  2. JSP forward用法分析实例代码分析
  3. JSP request(return String)用法详例
  4. JSP JavaBean的setProperty属性
  5. PHP基于进程控制函数实现多线程
  6. 如何画角色透视?人物透视动态绘画步骤!
  7. JSP errorPage设置方法
  8. JSP bean获取各种参数
  9. PHP笛卡尔积实现原理及代码实例
  10. discuz论坛更换域名,详细文件修改步骤