Android Application Development Example (1)-----Simple Doodle Board

Source: Internet
Author: User
Tags clear screen

Link Address: http://www.cnblogs.com/lknlfy/archive/2012/03/03/2378328.html

I. Overview

This time to do a simple Doodle board application, previously implemented on QT, suddenly think of it to achieve on Android, Hehe, is both simple and interesting.

Second, the realization

New Project Mywall, modify the/res/layout/main.xml file, add a Surfaceview and two buttons inside, use the relativelayout layout, the complete Main.xml file as follows:

1 <?xml version= "1.0" encoding= "Utf-8"?>
2
3 <relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
4 Android:layout_width= "Fill_parent"
5 android:layout_height= "Fill_parent"
6 android:orientation= "Vertical"
7 >
8
9 <surfaceview
Ten android:id= "@+id/surfaceview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:layout_above= "@+id/line"
Android:layout_alignparenttop= "true"
/>
16
<linearlayout
Android:id= "@+id/line"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:layout_alignparentbottom= "true"
>
23
<button
Android:id= "@+id/flushbutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "Clear Screen"
/>
31
<button
Android:id= "@+id/colorbutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
Panax notoginseng android:text= "Color"
/>
</LinearLayout>
</RelativeLayout>

Then, modify the Mywallactivity.java file, the most important is to overwrite the ontouchevent () function, in this function filter out the touch screen drag event, and then get its corresponding coordinates and draw lines, about the use of Surfaceview in the basic article is discussed. The complete contents are as follows:

1 package Com.nan.wall;
2
3 Import android.app.Activity;
4 Import Android.app.AlertDialog;
5 Import Android.app.Dialog;
6 Import Android.content.DialogInterface;
7 Import Android.graphics.Canvas;
8 Import Android.graphics.Color;
9 Import Android.graphics.Paint;
Ten import Android.graphics.Rect;
Import Android.os.Bundle;
Import android.view.MotionEvent;
Import Android.view.SurfaceHolder;
Import Android.view.SurfaceView;
Android.view.View import;
Android.widget.Button import;
17
public class Mywallactivity extends Activity
19 {
Private Surfaceview Msurfaceview = null;
$ private Surfaceholder Msurfaceholder = null;
The private Button Cleanbutton = null;
Colorbutton = null for the private Button;
24
-Private float oldx = 0f;
+ Private float OldY = 0f;
27
Candraw private Boolean = false;
Private Paint mpaint = null;
30//used to record which color is currently
-private int whichcolor = 0;
32
/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate)
36 {
Panax Notoginseng super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
39
Msurfaceview = (Surfaceview) This.findviewbyid (R.id.surfaceview);
Msurfaceholder = Msurfaceview.getholder ();
42
Mpaint = new Paint ();
44//Color of brush
Mpaint.setcolor (color.red);
46//thickness of brush
Mpaint.setstrokewidth (2.0f);
48
Cleanbutton = (Button) This.findviewbyid (R.id.flushbutton);
50//Button Monitor
Wuyi Cleanbutton.setonclicklistener (New View.onclicklistener ()
52 {
53
@Override
The public void OnClick (View v)
56 {
//TODO auto-generated method stub
58//Lock the entire Surfaceview
Mcanvas Canvas = Msurfaceholder.lockcanvas ();
Mcanvas.drawcolor (Color.Black);
61//Draw complete, submit changes
Msurfaceholder.unlockcanvasandpost (Mcanvas);
63//re-lock once
Msurfaceholder.lockcanvas (New Rect (0, 0, 0, 0));
Msurfaceholder.unlockcanvasandpost (Mcanvas);
66}
67});
68
Colorbutton = (Button) This.findviewbyid (R.id.colorbutton);
70//Button monitor
Colorbutton.setonclicklistener (New View.onclicklistener ()
72 {
73
@Override
-public void OnClick (View v)
76 {
/TODO auto-generated Method stub
Dialog Mdialog = new Alertdialog.builder (mywallactivity.this)
Settitle ("Color settings")
Setsinglechoiceitems (New string[]{"Red", "green", "Blue"}, Whichcolor, new Dialoginterface.onclicklistener ()
81 {
82
@Override
Dialoginterface public void OnClick (dialog, int which)
85 {
+//TODO auto-generated method stub
Switch (which)
88 {
Case 0:
90 {
91//Color of brush
Mpaint.setcolor (color.red);
Whichcolor = 0;
94 break;
95}
1:
97 {
98//Color of brush
Mpaint.setcolor (Color.green);
Whichcolor = 1;
101 break;
102}
103 Case 2:
104 {
105//Color of brush
106 Mpaint.setcolor (Color.Blue);
107 Whichcolor = 2;
108 break;
109}
110}
111}
112})
113. Setpositivebutton ("OK", new Dialoginterface.onclicklistener ()
114 {
115
@Override
117 public void OnClick (Dialoginterface dialog, int which)
118 {
119//TODO auto-generated method stub
Dialog.dismiss ();
121}
122})
123. Create ();
124 Mdialog.show ();
125}
126});
127
128}
129
130
131 @Override
Ontouchevent Public Boolean (Motionevent event)
133 {
134//Get X coordinate
135 float x = Event.getx ();
136//Get y-coordinate (do not know why to subtract an offset value in order to get the screen)
137 Float y = event.gety ()-50;
138
139//First time come in first
if (Candraw)
141 {
142//Get touch screen Event
143 switch (event.getaction ())
144 {
145//If the drag event
146 Case Motionevent.action_move:
147 {
148
149//Lock the entire Surfaceview
Canvas Mcanvas = Msurfaceholder.lockcanvas ();
151 Mcanvas.drawline (x, Y, Oldx, OldY, Mpaint);
Msurfaceholder.unlockcanvasandpost (Mcanvas);
153//re-lock once
154 Msurfaceholder.lockcanvas (New Rect (0, 0, 0, 0));
155 Msurfaceholder.unlockcanvasandpost (Mcanvas);
156 break;
157}
158}
159
160}
161//Save current x-coordinate value
162 OLDX = x;
163//Save current y-coordinate value
164 OldY = y;
165
166 Candraw = true;
167
168 return true;
169}
170
171}

Well, the effect of running on the simulator is as follows:

The effect of running on the real machine is as follows:

Oh, it is rather ugly to write.

After obtaining the y-coordinate minus an offset value of 50, this value is I guessed directly, did not expect in the simulator and the real machine positioning is quite accurate, haha. Of course, the application of the function is not much, but it is interesting to improve it, hoping to play a role.

Android Application Development Example (1)-----Simple Doodle Board

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.