在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

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

百戰(zhàn)經典第三戰(zhàn)-實現畫圖板

解觸摸事件(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); // 設置要使用的布局管理器  
    }  
} 

運行實例,在實例上用手指就可以作畫了,如下:

這里寫圖片描述

總結

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