Android 开发实例简单涂鸦板
程序员文章站
2024-03-07 20:14:51
在android上开发一些小应用既可以积累知识又可以增加乐趣,与任务式开发不同,所以想到在andr...
在android上开发一些小应用既可以积累知识又可以增加乐趣,与任务式开发不同,所以想到在android系统上实现一个简单的涂鸦板,这是我们练手的一种好的方法。
涂鸦板应用的代码实现
新建工程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()函数,在这个函数里过滤出触屏拖动事件,然后获取其相应的坐标和画线。完整的内容如下:
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; //用来记录当前是哪一种颜色 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); //画笔的粗细 mpaint.setstrokewidth(2.0f); cleanbutton = (button)this.findviewbyid(r.id.flushbutton); //按钮监听 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); //按钮监听 colorbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub dialog mdialog = new alertdialog.builder(mywallactivity.this) .settitle("颜色设置") .setsinglechoiceitems(new string[]{"红色","绿色","蓝色"}, 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坐标 float x = event.getx(); //获取y坐标(不知道为什么要减去一个偏移值才对得准屏幕) float y = event.gety()-50; //第一次进来先不管 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坐标值 oldx = x; //保存目前的y坐标值 oldy = y; candraw = true; return true; } }
应用测试
在模拟器上运行此应用是如下效果:
在android手机上运行效果则是这样的:
字写的有点丑,但是功能实现了。在获取了y坐标后减去一个偏移值50,这个值是猜出来的,没想到在模拟器和真机上定位得都还蛮准的。
应用比较简易,但是大家可以在此基础上丰富它的功能,使其成为一个像样的android应用。
以上就是android 简单涂鸦板的简单示例,后续继续整理相关资料,谢谢大家对本站的支持!
上一篇: SearchView
下一篇: log4j 详解异步日志的配置和测试