Android Development Example Simple Doodle Board _android

Source: Internet
Author: User
Tags clear screen stub

Developing small applications on Android can accumulate both knowledge and fun, unlike task-based development, so it's a good idea to implement a simple doodle board on an android practicing.

code implementation of Graffiti Board application

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

xml/html Code

<?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= "clear screen"/> <button android:id= "@+id/" 
Colorbutton "android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:layout_weight=" "1"  android:text= "Color"/> </LinearLayout> </RelativeLayout>

Then, to modify the Mywallactivity.java file, the most important thing is to rewrite the ontouchevent () function, in this function filter out the touch screen drag event, and then get its corresponding coordinates and draw lines. The complete contents are as follows:

Java code

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;    
   
Used to record which color is currently private int whichcolor = 0; /** called the activity is a. * * @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 ();    
The color of the Brush Mpaint.setcolor (color.red);   
The thickness of the brush mpaint.setstrokewidth (2.0f);    
Cleanbutton = (Button) This.findviewbyid (R.id.flushbutton); Button Listener Cleanbutton.setonclicklistener (new View.onclicklistener () {@Override public void OnClick (View    
V) {//TODO auto-generated Method Stub//Lock entire surfaceview Canvas Mcanvas = Msurfaceholder.lockcanvas ();    
Mcanvas.drawcolor (Color.Black);    
Draw complete, submit modify Msurfaceholder.unlockcanvasandpost (Mcanvas);    
Re-lock the Msurfaceholder.lockcanvas (new Rect (0, 0, 0, 0));    
Msurfaceholder.unlockcanvasandpost (Mcanvas);     
   
}   
});   
Colorbutton = (Button) This.findviewbyid (R.id.colorbutton); Button Monitor Colorbutton.setonClicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {//TODO auto-g enerated method Stub Dialog mdialog = new Alertdialog.builder (mywallactivity.this). Settitle ("Color settings"). Setsi Nglechoiceitems (New string[]{"Red", "green", "Blue"}, Whichcolor, new Dialoginterface.onclicklistener () {@Override Pub    
LIC void OnClick (dialoginterface dialog, int which) {//TODO auto-generated Method stub switch (which) {    
Case 0: {//Brush color Mpaint.setcolor (color.red);    
Whichcolor = 0;    
Break   
Case 1: {//Brush color Mpaint.setcolor (color.green);   
Whichcolor = 1;   
Break   
Case 2: {//color of the Brush Mpaint.setcolor (Color.Blue);                 
Whichcolor = 2;                 
Break }}}). Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Override public void oncli CK (dialoginterface dialog, int which) {//TODO Auto-generated method Stub Dialog.dismiss ();   
). Create ();   
Mdialog.show ();   
   
   
}   
});   
@Override public boolean ontouchevent (Motionevent event) {//get x coordinate float x = Event.getx ();   
Get the y-coordinate (don't know why you subtract an offset value to get to the right screen) float y = event.gety ()-50; First time come in first regardless of if (Candraw) {//Get touchscreen event switch (event.getaction ()) {//If drag event case Motionevent.action_mo     
VE: {//Lock entire surfaceview Canvas Mcanvas = Msurfaceholder.lockcanvas ();   
Mcanvas.drawline (x, Y, Oldx, Oldy, Mpaint);   
Msurfaceholder.unlockcanvasandpost (Mcanvas);   
Re-lock the Msurfaceholder.lockcanvas (new Rect (0, 0, 0, 0));   
Msurfaceholder.unlockcanvasandpost (Mcanvas);   
Break   
}///Save the current x coordinate value OLDX = x;   
   
Save the current y-coordinate value oldy = y;   
   
Candraw = true;   
return true;    }   
   
}

Application test

Running this application on the emulator has the following effect:

The effect of running on Android phones is this:

The handwriting is a bit ugly, but the function is realized. After getting the y-coordinate minus an offset value of 50, this value is guessed, did not expect in the simulator and the real machine positioning is pretty accurate.

It's easy to use, but you can enrich its functionality on this basis to make it a decent Android app.

The above is a simple example of Android simple graffiti, follow-up to continue to collate relevant information, thank you for the support of this site!

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.