Android 開發(fā)實(shí)例簡單涂鴉板
在Android上開發(fā)一些小應(yīng)用既可以積累知識(shí)又可以增加樂趣,與任務(wù)式開發(fā)不同,所以想到在Android系統(tǒng)上實(shí)現(xiàn)一個(gè)簡單的涂鴉板,這是我們練手的一種好的方法。
涂鴉板應(yīng)用的代碼實(shí)現(xiàn)
新建工程MyWall,修改/res/layout/main.xml文件,在里面添加一個(gè)SurfaceView和兩個(gè)Button,用到了RelativeLayout布局,完整的main.xml文件如下:
XML/HTML代碼
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns: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ù),在這個(gè)函數(shù)里過濾出觸屏拖動(dòng)事件,然后獲取其相應(yīng)的坐標(biāo)和畫線。完整的內(nèi)容如下:
Java代碼
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;
public class MyWallActivity extends Activity
{
private SurfaceView mSurfaceView = null;
private SurfaceHolder mSurfaceHolder = null;
private Button cleanButton = null;
private Button colorButton = null;
private float oldX = 0f;
private float oldY = 0f;
private boolean canDraw = false;
private Paint mPaint = null;
//用來記錄當(dāng)前是哪一種顏色
private int whichColor = 0;
/** Called when the activity is first created. */
@Override
public void 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
public void onClick(View v)
{
// TODO Auto-generated method stub
//鎖定整個(gè)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
public void 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
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
switch(which)
{
case 0:
{
//畫筆的顏色
mPaint.setColor(Color.RED);
whichColor = 0;
break;
}
case 1:
{
//畫筆的顏色
mPaint.setColor(Color.GREEN);
whichColor = 1;
break;
}
case 2:
{
//畫筆的顏色106
mPaint.setColor(Color.BLUE);
whichColor = 2;
break;
}
}
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
dialog.dismiss();
}
})
.create();
mDialog.show();
}
});
@Override
public boolean onTouchEvent(MotionEvent event)
{
//獲取x坐標(biāo)
float x = event.getX();
//獲取y坐標(biāo)(不知道為什么要減去一個(gè)偏移值才對得準(zhǔn)屏幕)
float y = event.getY()-50;
//第一次進(jìn)來先不管
if(canDraw)
{
//獲取觸屏事件
switch(event.getAction())
{
//如果是拖動(dòng)事件
case MotionEvent.ACTION_MOVE:
{
//鎖定整個(gè)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;
return true;
}
}
應(yīng)用測試
在模擬器上運(yùn)行此應(yīng)用是如下效果:

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

字寫的有點(diǎn)丑,但是功能實(shí)現(xiàn)了。在獲取了Y坐標(biāo)后減去一個(gè)偏移值50,這個(gè)值是猜出來的,沒想到在模擬器和真機(jī)上定位得都還蠻準(zhǔn)的。
應(yīng)用比較簡易,但是大家可以在此基礎(chǔ)上豐富它的功能,使其成為一個(gè)像樣的Android應(yīng)用。
以上就是Android 簡單涂鴉板的簡單示例,后續(xù)繼續(xù)整理相關(guān)資料,謝謝大家對本站的支持!
相關(guān)文章
Android自定義view實(shí)現(xiàn)日歷打卡簽到
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)日歷打卡簽到,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Android UI控件之ListView實(shí)現(xiàn)圓角效果
這篇文章主要為大家詳細(xì)介紹了Android UI控件之ListView實(shí)現(xiàn)圓角效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android使用多線程實(shí)現(xiàn)斷點(diǎn)下載
這篇文章主要介紹了Android使用多線程實(shí)現(xiàn)斷點(diǎn)下載,多線程下載是加快下載速度的一種方式,感興趣的小伙伴們可以參考一下2016-03-03
Android筆記之:App應(yīng)用之啟動(dòng)界面SplashActivity的使用
當(dāng)前比較成熟一點(diǎn)的應(yīng)用基本上都會(huì)在進(jìn)入應(yīng)用之顯示一個(gè)啟動(dòng)界面.這個(gè)啟動(dòng)界面或簡單,或復(fù)雜,或簡陋,或華麗,用意不同,風(fēng)格也不同2013-04-04
Android實(shí)現(xiàn)文件解壓帶進(jìn)度條功能
本文通過實(shí)例代碼給大家介紹了android實(shí)現(xiàn)文件解壓帶進(jìn)度條效果,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-08-08

