鍍金池/ 教程/ Android/ 百戰(zhàn)經(jīng)典第三戰(zhàn)-實(shí)現(xiàn)畫圖板
百戰(zhàn)經(jīng)典第二十戰(zhàn)-ListView中點(diǎn)擊button跳轉(zhuǎn)到撥號(hào)界面實(shí)例
百戰(zhàn)經(jīng)典第十一戰(zhàn)-GridView動(dòng)態(tài)添加Item
百戰(zhàn)經(jīng)典第二戰(zhàn)-好玩的Spinner控件
百戰(zhàn)經(jīng)典第五戰(zhàn)-各種對(duì)話框Dialog精彩薈萃
百戰(zhàn)經(jīng)典第八戰(zhàn)-BitmapFactory.Options對(duì)資源圖片進(jìn)行縮放
百戰(zhàn)經(jīng)典第四戰(zhàn)-玩轉(zhuǎn)ListView
百戰(zhàn)經(jīng)典第十五-竊聽風(fēng)云之短信監(jiān)聽
前言
百戰(zhàn)經(jīng)典第十四戰(zhàn)-網(wǎng)絡(luò)交互,基于Baas用戶表查詢功能實(shí)現(xiàn)
百戰(zhàn)經(jīng)典第九戰(zhàn)-ViewFlipper實(shí)現(xiàn)幻燈效果
百戰(zhàn)經(jīng)典第三戰(zhàn)-實(shí)現(xiàn)畫圖板
百戰(zhàn)經(jīng)典第十七戰(zhàn)-基于加速度傳感器的搖一搖功能實(shí)例
百戰(zhàn)經(jīng)典第十戰(zhàn)-LayoutAnimation布局動(dòng)畫效果
百戰(zhàn)經(jīng)典第七戰(zhàn)-顯示倒計(jì)時(shí)的Button按鈕
百戰(zhàn)經(jīng)典第六戰(zhàn)-Activity啟動(dòng)模式小樣
百戰(zhàn)經(jīng)典第十二戰(zhàn)-GridView動(dòng)態(tài)刪除Item
百戰(zhàn)經(jīng)典第十六戰(zhàn)-圖片或頭像設(shè)置功能
百戰(zhàn)經(jīng)典第十九戰(zhàn)-短信監(jiān)聽實(shí)現(xiàn)驗(yàn)證碼自動(dòng)填入
百戰(zhàn)經(jīng)典第一戰(zhàn)—聽話的TextView
百戰(zhàn)經(jīng)典第十八戰(zhàn)-自定義控件實(shí)現(xiàn)一鍵清空輸入框
百戰(zhàn)經(jīng)典第十三戰(zhàn)-網(wǎng)絡(luò)交互,基于Baas實(shí)現(xiàn)用戶注冊(cè)功能

百戰(zhàn)經(jīng)典第三戰(zhàn)-實(shí)現(xiàn)畫圖板

解觸摸事件(OnTouchListener)指的是當(dāng)用戶接觸到屏幕之后所觸發(fā)的一種事件形式,用戶觸摸屏幕時(shí),可以使用觸摸事件監(jiān)聽取得用戶當(dāng)前的坐標(biāo)。

一、坐標(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í)例:

這里寫圖片描述

二、實(shí)現(xiàn)畫圖功能

由于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í)例上用手指就可以作畫了,如下:

這里寫圖片描述

總結(jié)

  1. 觸摸事件OnTouchListeneronTouch()方法;
  2. event.getX()//利用MotionEvent獲取坐標(biāo)的方法getX()
  3. onDraw()方法和如何使用Canvas進(jìn)行繪圖的操作,而本次繪制是一條線(canvas.drawLine())。