解觸摸事件(OnTouchListener)指的是當用戶接觸到屏幕之后所觸發(fā)的一種事件形式,用戶觸摸屏幕時,可以使用觸摸事件監(jiān)聽取得用戶當前的坐標。
在實現畫圖功能之前,先實現利用觸摸事件監(jiān)聽獲得當前觸摸的坐標。
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;
//省略導入包
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;
}
});
}
}
實現了觸摸監(jiān)聽,結合MotionEvent的getX和getY方法獲取當前坐標值。
運行實例:
由于OnTouch事件是在View類中定義的,所以如果想要完成繪圖的操作,首先應該定義一個屬于自己的組件,該組件專門進行繪圖板的功能實現,而且組件類一定要繼承View類,同時要覆寫View類的onDraw()繪圖方法。
代碼如下:
package org.yayun.demo;
//省略導入包
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;//表示下面的不再執(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;//開始點
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);
}
}
這里用到了自定義控件,繼承View類。定義了一個泛型Point的List用于保存所有的坐標數據,在繪圖 onDraw()方法時,根據這些坐標劃線。
修改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;
//省略導入包
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 生命周期方法
super.setContentView(R.layout.main); // 設置要使用的布局管理器
}
}
運行實例,在實例上用手指就可以作畫了,如下:
OnTouchListener及onTouch()方法;event.getX()//利用MotionEvent獲取坐標的方法getX()onDraw()方法和如何使用Canvas進行繪圖的操作,而本次繪制是一條線(canvas.drawLine())。