Android開發(fā):簡單涂鴉板的實例
涂鴉板應(yīng)用的代碼實現(xiàn)
新建工程MyWall,修改/res/layout/main.xml文件,在里面添加一個SurfaceView和兩個Button,用到了RelativeLayout布局,完整的main.xml文件如下:
- <?xmlversion="1.0"encoding="utf-8"?>
 - <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
 - android:layout_width="fill_parent"
 - android:layout_height="fill_parent"
 - android:orientation="vertical"
 - >
 - <SurfaceView
 - android:id="@+id/surfaceview"
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:layout_above="@+id/line"
 - android:layout_alignParentTop="true"
 - />
 - <LinearLayout
 - android:id="@+id/line"
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:layout_alignParentBottom="true"
 - >
 - <Button
 - android:id="@+id/flushbutton"
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:layout_weight="1"
 - android:text="清屏"
 - />
 - <Button
 - android:id="@+id/colorbutton"
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:layout_weight="1"
 - android:text="顏色"
 - />
 - </LinearLayout>
 - </RelativeLayout>
 
接著,修改MyWallActivity.java文件,最主要是重寫了onTouchEvent()函數(shù),在這個函數(shù)里過濾出觸屏拖動事件,然后獲取其相應(yīng)的坐標(biāo)和畫線。完整的內(nèi)容如下:
- package com.nan.wall;
 - import android.app.Activity;
 - import android.app.AlertDialog;
 - import android.app.Dialog;
 - import android.content.DialogInterface;
 - import android.graphics.Canvas;
 - import android.graphics.Color;
 - import android.graphics.Paint;
 - import android.graphics.Rect;
 - import android.os.Bundle;
 - import android.view.MotionEvent;
 - import android.view.SurfaceHolder;
 - import android.view.SurfaceView;
 - import android.view.View;
 - import android.widget.Button;
 - publicclass MyWallActivity extends Activity
 - {
 - private SurfaceView mSurfaceView = null;
 - private SurfaceHolder mSurfaceHolder = null;
 - private Button cleanButton = null;
 - private Button colorButton = null;
 - privatefloat oldX = 0f;
 - privatefloat oldY = 0f;
 - privateboolean canDraw = false;
 - private Paint mPaint = null;
 - //用來記錄當(dāng)前是哪一種顏色
 - privateint whichColor = 0;
 - /** Called when the activity is first created. */
 - @Override
 - publicvoid onCreate(Bundle savedInstanceState)
 - {
 - super.onCreate(savedInstanceState);
 - setContentView(R.layout.main);
 - mSurfaceView = (SurfaceView)this.findViewById(R.id.surfaceview);
 - mSurfaceHolder = mSurfaceView.getHolder();
 - mPaint = new Paint();
 - //畫筆的顏色
 - mPaint.setColor(Color.RED);
 - //畫筆的粗細(xì)
 - mPaint.setStrokeWidth(2.0f);
 - cleanButton = (Button)this.findViewById(R.id.flushbutton);
 - //按鈕監(jiān)聽
 - cleanButton.setOnClickListener(new View.OnClickListener()
 - {
 - @Override
 - publicvoid onClick(View v)
 - {
 - // TODO Auto-generated method stub
 - //鎖定整個SurfaceView
 - Canvas mCanvas = mSurfaceHolder.lockCanvas();
 - mCanvas.drawColor(Color.BLACK);
 - //繪制完成,提交修改
 - mSurfaceHolder.unlockCanvasAndPost(mCanvas);
 - //重新鎖一次
 - mSurfaceHolder.lockCanvas(new Rect(0, 0, 0, 0));
 - mSurfaceHolder.unlockCanvasAndPost(mCanvas);
 - }
 - });
 - colorButton = (Button)this.findViewById(R.id.colorbutton);
 - //按鈕監(jiān)聽
 - colorButton.setOnClickListener(new View.OnClickListener()
 - {
 - @Override
 - publicvoid onClick(View v)
 - {
 - // TODO Auto-generated method stub
 - Dialog mDialog = new AlertDialog.Builder(MyWallActivity.this)
 - .setTitle("顏色設(shè)置")
 - .setSingleChoiceItems(new String[]{"紅色","綠色","藍(lán)色"}, whichColor, new DialogInterface.OnClickListener()
 - {
 - @Override
 - publicvoid onClick(DialogInterface dialog, int which)
 - {
 - // TODO Auto-generated method stub
 - switch(which)
 - {
 - case0:
 - {
 - //畫筆的顏色
 - mPaint.setColor(Color.RED);
 - whichColor = 0;
 - break;
 - }
 - case1:
 - {
 - //畫筆的顏色
 - mPaint.setColor(Color.GREEN);
 - whichColor = 1;
 - break;
 - }
 - case2:
 - {
 - //畫筆的顏色106
 - mPaint.setColor(Color.BLUE);
 - whichColor = 2;
 - break;
 - }
 - }
 - }
 - })
 - .setPositiveButton("確定", new DialogInterface.OnClickListener()
 - {
 - @Override
 - publicvoid onClick(DialogInterface dialog, int which)
 - {
 - // TODO Auto-generated method stub
 - dialog.dismiss();
 - }
 - })
 - .create();
 - mDialog.show();
 - }
 - });
 - @Override
 - publicboolean onTouchEvent(MotionEvent event)
 - {
 - //獲取x坐標(biāo)
 - float x = event.getX();
 - //獲取y坐標(biāo)(不知道為什么要減去一個偏移值才對得準(zhǔn)屏幕)
 - float y = event.getY()-50;
 - //第一次進(jìn)來先不管
 - if(canDraw)
 - {
 - //獲取觸屏事件
 - switch(event.getAction())
 - {
 - //如果是拖動事件
 - case MotionEvent.ACTION_MOVE:
 - {
 - //鎖定整個SurfaceView
 - Canvas mCanvas = mSurfaceHolder.lockCanvas();
 - mCanvas.drawLine(x, y, oldX, oldY, mPaint);
 - mSurfaceHolder.unlockCanvasAndPost(mCanvas);
 - //重新鎖一次
 - mSurfaceHolder.lockCanvas(new Rect(0, 0, 0, 0));
 - mSurfaceHolder.unlockCanvasAndPost(mCanvas);
 - break;
 - }
 - }
 - }
 - //保存目前的x坐標(biāo)值
 - oldX = x;
 - //保存目前的y坐標(biāo)值
 - oldY = y;
 - canDraw = true;
 - returntrue;
 - }
 - }
 
應(yīng)用測試
在模擬器上運行此應(yīng)用是如下效果:

在Android手機(jī)上運行效果則是這樣的:

字寫的有點丑,但是功能實現(xiàn)了。在獲取了Y坐標(biāo)后減去一個偏移值50,這個值是猜出來的,沒想到在模擬器和真機(jī)上定位得都還蠻準(zhǔn)的。
應(yīng)用比較簡易,但是大家可以在此基礎(chǔ)上豐富它的功能,使其成為一個像樣的Android應(yīng)用。















 
 
 


 
 
 
 