解觸摸事件(OnTouchListener)指的是當(dāng)用戶接觸到屏幕之后所觸發(fā)的一種事件形式,用戶觸摸屏幕時(shí),可以使用觸摸事件監(jiān)聽取得用戶當(dāng)前的坐標(biāo)。
在實(shí)現(xiàn)畫圖功能之前,先實(shí)現(xiàn)利用觸摸事件監(jiān)聽獲得當(dāng)前觸摸的坐標(biāo)。
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>
布局代碼非常簡單,只引入一個(gè)TextView控件,用于記錄當(dāng)前左邊。
下面看一下MainActivity代碼:
package org.yayun.demo;
//省略導(dǎo)入包
public class MainActivity extends Activity {
private TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 生命周期方法
super.setContentView(R.layout.main); // 設(shè)置要使用的布局管理器
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());//獲取坐標(biāo)
return false;
}
});
}
}
實(shí)現(xiàn)了觸摸監(jiān)聽,結(jié)合MotionEvent的getX和getY方法獲取當(dāng)前坐標(biāo)值。
運(yùn)行實(shí)例:
由于OnTouch事件是在View類中定義的,所以如果想要完成繪圖的操作,首先應(yīng)該定義一個(gè)屬于自己的組件,該組件專門進(jìn)行繪圖板的功能實(shí)現(xiàn),而且組件類一定要繼承View類,同時(shí)要覆寫View類的onDraw()
繪圖方法。
代碼如下:
package org.yayun.demo;
//省略導(dǎo)入包
public class MyPaintView extends View{
private List<Point> allPoints=new ArrayList<Point>();//保存所有的坐標(biāo)點(diǎn)
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;//表示下面的不再執(zhí)行了
}
});
}
@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;//開始點(diǎn)
Point lastpPoint=null;//結(jié)束點(diǎn)
while (iterator.hasNext()) {
if(firstPoint==null){//找到開始點(diǎn)
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);
}
}
這里用到了自定義控件,繼承View類。定義了一個(gè)泛型Point的List用于保存所有的坐標(biāo)數(shù)據(jù),在繪圖 onDraw()
方法時(shí),根據(jù)這些坐標(biāo)劃線。
修改main.xml,將自定義控件MyPaintView引入:
<?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;
//省略導(dǎo)入包
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 生命周期方法
super.setContentView(R.layout.main); // 設(shè)置要使用的布局管理器
}
}
運(yùn)行實(shí)例,在實(shí)例上用手指就可以作畫了,如下:
OnTouchListener
及onTouch()
方法;event.getX()
//利用MotionEvent獲取坐標(biāo)的方法getX()onDraw()
方法和如何使用Canvas進(jìn)行繪圖的操作,而本次繪制是一條線(canvas.drawLine()
)。