Android实战简易教程-第三枪(实现简单绘图组件)

首先我们要了解触摸事件(OnTouchListener)指的是当用户接触到屏幕之后所产生的一种事件形式,而当用户在屏幕上划过时,可以使用触摸事件取得用户当前的坐标。

一、坐标显示

在实现画图功能之前,我们先利用触摸事件获得当前触摸的坐标。

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" >

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>


代码非常简单,只引入一个TextView控件,下面看一下MainActivity代码:

package org.yayun.demo;

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 MainActivity extends Activity {
	private TextView textView;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); // 生命周期方法
		super.setContentView(R.layout.main); // 设置要使用的布局管理器
	textView=(TextView)findViewById(R.id.text);
	textView.setOnTouchListener(new OnTouchListener() {//触摸事件
		
		public boolean onTouch(View v, MotionEvent event) {
			textView.setText("X="+event.getX()+",Y="+event.getY());//获取坐标
			return false;
		}
	});

	}
}

运行实例:

上面可以看到实时获得当前触摸的坐标。

二、实现画图功能

由于OnTouch事件是在View类中定义的,所以如果想要完成绘图的操作,首先应该定义一个属于自己的组件,该组件专门进行绘图板的功能实现,而且组件类一定要继承View类,同时要覆写View类的onDraw()绘图方法。

代码如下:

package org.yayun.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> allPoints=new ArrayList<Point>();//保存所有的坐标点
	public MyPaintView(Context context, AttributeSet attrs) {
		super(context, attrs);
		super.setBackgroundColor(Color.WHITE);
		super.setOnTouchListener(new OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				Point point=new Point((int)event.getX(),(int)event.getY());
				if(event.getAction()==MotionEvent.ACTION_DOWN){//判断按下
					allPoints=new ArrayList<Point>();//开始新的记录
					allPoints.add(point);
				}else if(event.getAction()==MotionEvent.ACTION_UP){
					allPoints.add(point);
				}else if(event.getAction()==MotionEvent.ACTION_MOVE){
					allPoints.add(point);
					MyPaintView.this.postInvalidate();//重绘
				}
				return true;//表示下面的不再执行了
			}
		});
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		Paint paint=new Paint();
		paint.setColor(Color.RED);
		if(allPoints.size()>1){
			Iterator<Point> iterator=allPoints.iterator();
			Point firstPoint=null;//开始点
			Point lastpPoint=null;//结束点
			while (iterator.hasNext()) {
				if(firstPoint==null){//找到开始点
					firstPoint=(Point)iterator.next();
				}else{
					if(lastpPoint!=null){
						firstPoint=lastpPoint;
					}
					lastpPoint=(Point)iterator.next();
					canvas.drawLine(firstPoint.x, firstPoint.y, lastpPoint.x, lastpPoint.y, paint);//画线
				}
				
			}
		}
		super.onDraw(canvas);
	}

}


修改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" >

    <org.yayun.demo.MyPaintView
        android:id="@+id/paintView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </org.yayun.demo.MyPaintView>

</LinearLayout>


MainActivity不用加入任何东西:

package org.yayun.demo;

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 MainActivity extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); // 生命周期方法
		super.setContentView(R.layout.main); // 设置要使用的布局管理器

	}
}


运行实例:

总结

1.触摸事件OnTouchListener及onTouch()方法;

2.event.getX()//利用MotionEvent获取坐标的方法getX()

3.onDraw()方法和如何使用Canvas进行绘图的操作,而本次绘制是一条线(canvas.drawLine())。

 

喜欢的朋友可以关注我!


 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值