Android 開發(fā)實例簡單涂鴉板
在Android上開發(fā)一些小應(yīng)用既可以積累知識又可以增加樂趣,與任務(wù)式開發(fā)不同,所以想到在Android系統(tǒng)上實現(xiàn)一個簡單的涂鴉板,這是我們練手的一種好的方法。
涂鴉板應(yīng)用的代碼實現(xiàn)
新建工程MyWall,修改/res/layout/main.xml文件,在里面添加一個SurfaceView和兩個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ù),在這個函數(shù)里過濾出觸屏拖動事件,然后獲取其相應(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
//鎖定整個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)(不知道為什么要減去一個偏移值才對得準(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;
return true;
}
}
應(yīng)用測試
在模擬器上運(yùn)行此應(yīng)用是如下效果:

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

字寫的有點丑,但是功能實現(xiàn)了。在獲取了Y坐標(biāo)后減去一個偏移值50,這個值是猜出來的,沒想到在模擬器和真機(jī)上定位得都還蠻準(zhǔn)的。
應(yīng)用比較簡易,但是大家可以在此基礎(chǔ)上豐富它的功能,使其成為一個像樣的Android應(yīng)用。
以上就是Android 簡單涂鴉板的簡單示例,后續(xù)繼續(xù)整理相關(guān)資料,謝謝大家對本站的支持!
相關(guān)文章
Android使用OKhttp3實現(xiàn)登錄注冊功能+springboot搭建后端的詳細(xì)過程
這篇教程主要實現(xiàn)Android使用OKhttp3實現(xiàn)登錄注冊的功能,后端使用SSM框架,本文通過實例圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-07-07
Golang+Android基于HttpURLConnection實現(xiàn)的文件上傳功能示例
這篇文章主要介紹了Golang+Android基于HttpURLConnection實現(xiàn)的文件上傳功能,結(jié)合具體實例形式分析了Android基于HttpURLConnection的客戶端結(jié)合Go語言服務(wù)器端實現(xiàn)文件上傳功能的操作技巧,需要的朋友可以參考下2017-03-03
Android高仿2048小游戲?qū)崿F(xiàn)代碼
這篇文章主要介紹了Android高仿2048小游戲?qū)崿F(xiàn)代碼的相關(guān)資料,非常不錯具有一定的參考借鑒價值,需要的朋友可以參考下2016-10-10
Android文件存儲SharedPreferences源碼解析
SharedPreferences是安卓平臺上一個輕量級的存儲類,用來保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時,將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時,再從SharedPreferences中將值取出2022-08-08
Android清空編輯框內(nèi)容功能的實現(xiàn)實例代碼
本篇文章主要介紹了Android清空編輯框數(shù)據(jù)功能的實現(xiàn)實例代碼,非常具有實用價值,需要的朋友可以參考下。2017-03-03
Android Studio 2020新版本卡在Gradle downloading/sync failed/下載緩慢/
Android Studio 2020新版本 卡在Gradle downloading / sync failed / 下載緩慢 / 下載超時 親測有效解決辦法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-12-12

