2023年7月7日发(作者:)
Android开发实例篇(1)-----简易涂鸦板
一、概述
这次要做一个简单的涂鸦板应用,以前在Qt上实现过,突然想到要把它在Android上实现,呵呵,既简单又有趣。
二、实现
新建工程MyWall,修改/res/layout/文件,在里面添加一个SurfaceView和两个Button,用到了RelativeLayout布局,完整的文件如下:
1
2
3 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 android:orientation="vertical" 7 > 8 9 10 android:id="@+id/surfaceview" 11 android:layout_width="fill_parent" 12 android:layout_height="wrap_content" 13 android:layout_above="@+id/line" 14 android:layout_alignParentTop="true" 15 /> 16 17 18 android:id="@+id/line" 19 android:layout_width="fill_parent" 20 android:layout_height="wrap_content" 21 android:layout_alignParentBottom="true" 22 > 23 24 40
复制代码
接着,修改文件,最主要是覆写了onTouchEvent()函数,在这个函数里过滤出触屏拖动事件,然后获取其相应的坐标和画线,关于SurfaceView的用法在基础篇里有讲到。完整的内容如下:
1 package ;
2
3 import ty;
4 import ialog;
5 import ;
6 import Interface;
7 import ;
8 import ;
9 import ;
10 import ;
11 import ;
12 import Event;
13 import eHolder;
14 import eView;
15 import ;
16 import ;
17
18 public class MyWallActivity extends Activity
19 {
20 private SurfaceView mSurfaceView = null;
21 private SurfaceHolder mSurfaceHolder = null;
22 private Button cleanButton = null;
23 private Button colorButton = null;
24
25 private float oldX = 0f;
26 private float oldY = 0f;
27
28 private boolean canDraw = false;
29 private Paint mPaint = null;
30 //用来记录当前是哪一种颜色
31 private int whichColor = 0;
32
33 /** Called when the activity is first created. */
34 @Override
35 public void onCreate(Bundle savedInstanceState)
36 {
37 te(savedInstanceState);
38 setContentView();
39
40 mSurfaceView = (SurfaceView)ewById(eview);
41 mSurfaceHolder = der();
42
43 mPaint = new Paint();
44 //画笔的颜色
45 or();
46 //画笔的粗细
47 okeWidth(2.0f);
48
49 cleanButton = (Button)ewById(utton);
50 //按钮监听
51 lickListener(new kListener()
52 {
53
54 @Override
55 public void onClick(View v)
56 {
57 // TODO Auto-generated method stub
58 //锁定整个SurfaceView
59 Canvas mCanvas = nvas();
60 lor();
61 //绘制完成,提交修改
62 CanvasAndPost(mCanvas);
63 //重新锁一次
64 nvas(new Rect(0, 0, 0, 0));
65 CanvasAndPost(mCanvas);
66 }
67 });
68
69 colorButton = (Button)ewById(utton);
70 //按钮监听
71 lickListener(new kListener()
72 {
73
74 @Override
75 public void onClick(View v)
76 {
77 // TODO Auto-generated method stub
78 Dialog mDialog = new r()
79 .setTitle("颜色设置")
80 .setSingleChoiceItems(new String[]{"红色","绿色","蓝色"}, whichColor, new kListener()
81 {
82
83 @Override
84 public void onClick(DialogInterface dialog, int which)
85 {
86 // TODO Auto-generated method stub
87 switch(which)
88 {
89 case 0:
90 {
91 //画笔的颜色
92 or();
93 whichColor = 0;
94 break;
95 }
96 case 1:
97 {
98 //画笔的颜色
99 or();
100 whichColor = 1;
101 break;
102 }
103 case 2:
104 {
105 //画笔的颜色
106 or();
107 whichColor = 2;
108 break;
109 }
110 }
111 }
112 })
113 .setPositiveButton("确定", new kListener()
114 {
115
116 @Override
117 public void onClick(DialogInterface dialog, int which)
118 {
119 // TODO Auto-generated method stub
120 s();
121 }
122 })
123 .create();
124 ();
125 }
126 });
127
128 }
129
130
131 @Override
132 public boolean onTouchEvent(MotionEvent event)
133 {
134 //获取x坐标
135 float x = ();
136 //获取y坐标(不知道为什么要减去一个偏移值才对得准屏幕)
137 float y = ()-50;
138
139 //第一次进来先不管
140 if(canDraw)
141 {
142 //获取触屏事件
143 switch(ion())
144 {
145 //如果是拖动事件
146 case _MOVE:
147 {
148
149 //锁定整个SurfaceView
150 Canvas mCanvas = nvas();
151 ne(x, y, oldX, oldY, mPaint);
152 CanvasAndPost(mCanvas);
153 //重新锁一次
154 nvas(new Rect(0, 0, 0, 0));
155 CanvasAndPost(mCanvas);
156 break;
157 }
158 }
159
160 }
161 //保存目前的x坐标值
162 oldX = x;
163 //保存目前的y坐标值
164 oldY = y;
165
166 canDraw = true;
167
168 return true;
169 }
170
171 }
复制代码
好了,在模拟器上运行效果如下:
在真机上运行效果如下:
呵呵,写得比较丑。
在获取了Y坐标后减去一个偏移值50,这个值是我直接猜出来的,没想到在模拟器和真机上定位得还蛮准的,哈哈。当然这个应用的功能不多,不过有兴趣的话可以再完善它,希望能起到抛砖引玉的作用。
发布者:admin,转转请注明出处:http://www.yc00.com/news/1688672904a161458.html
评论列表(0条)