Android for tablet and doodle functionality

Source: Internet
Author: User

Below is a copy of the Android Tablet and graffiti features, directly on the code:

Write_pad.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:greendroid= "http// Schemas.android.com/apk/res/com.cyrilmottier.android.gdcatalog "Android:layout_width=" Fill_parent "Android:         layout_height= "fill_parent" android:orientation= "vertical" > <framelayout android:id= "@+id/tablet_view" Android:layout_width= "Fill_parent" android:layout_height= "300DP" > </FrameLayout> <linear Layout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:background= "@a Ndroid:drawable/bottom_bar "android:paddingtop=" 4DP "> <button android:id=" @+id/write_pad_o K "android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:layou            t_weight= "1" android:text= "OK"/> <button android:id= "@+id/write_pad_clear" Android:layout_width= "Wrap_content" Androidoid:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "clear"/> <button Android:id= "@+id/write_pad_cancel" android:layout_width= "Wrap_content" android:layout_he ight= "Wrap_content" android:layout_weight= "1" android:text= "Cancel"/> </linearlayout></l Inearlayout>

This is the main layout file of the tablet, the part that can be handwritten is a framelayout. The OK, clear, and Cancel buttons below are used to save and erase signatures.

The main code logic is as follows:

Mainactivity.java

Package Com.jackie.handwriting;import Java.io.bytearrayoutputstream;import Java.io.file;import Java.io.fileoutputstream;import Java.io.ioexception;import Android.app.activity;import Android.graphics.Bitmap; Import Android.os.bundle;import Android.os.environment;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.imageview;import Android.widget.textview;public Class Mainactivity extends Activity {private ImageView mivsign;private TextView mtvsign;private Bitmap msignbitmap;/** called W Hen the activity is first created. */@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); mivsign = (ImageView) Findviewbyid (r.id.iv_sign); mtvsign = (TextView) Findviewbyid (r.id.tv_ sign); Mtvsign.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (view view) {Writepaddialog Mwritepaddialog = new Writepaddialog (mainactivity.this, New Writedialoglistener () {@Overridepublic voidOnpaintdone (Object object) {Msignbitmap = (Bitmap) object;createsignfile (); Mivsign.setimagebitmap (MSIGNBITMAP); Mtvsign.setvisibility (View.gone);}}); Mwritepaddialog.show ();}});}        Create signature file private void Createsignfile () {Bytearrayoutputstream BAOs = null;        FileOutputStream fos = null;          String path = null;        File file = null; try {path = environment.getexternalstoragedirectory () + File.separator + system.currenttimemillis () + ". jpg"             ;            File = new file (path);            FOS = new FileOutputStream (file);            BAOs = new Bytearrayoutputstream (); If set to Bitmap.compress (Compressformat.jpeg, S, FOS) the background of the picture is black msignbitmap.compress (Bitmap.CompressFormat.PNG              , BAOs);              Byte[] B = Baos.tobytearray ();             if (b! = null) {fos.write (b);          }} catch (IOException e) {e.printstacktrace (); } finally {try {if (FOS = null)                {Fos.close ();                } if (BAOs! = null) {baos.close ();              }} catch (IOException e) {e.printstacktrace (); }          }  }}

Paintview.java

Package Com.jackie.handwriting;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmap.config;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.matrix;import Android.graphics.paint;import Android.graphics.path;import Android.graphics.porterduff;import Android.view.motionevent;import Android.view.view;public class PaintView extends View {private Paint mpaint;private Path mpath;private Bitmap mbitmap;private Canvas mcanvas;private int screenwidth, scree nheight;private float CurrentX, currenty;public paintview (context context, int screenwidth, int screenheight) {super ( context); this.screenwidth = Screenwidth;this.screenheight = Screenheight;init ();} private void Init () {mpaint = new Paint (); Mpaint.setantialias (true);//anti-aliasing mpaint.setstrokewidth (5); Mpaint.setstyle ( Paint.Style.STROKE); Mpaint.setcolor (color.black); mPath = new Path (); mbitmap = Bitmap.createbitmap (ScreenWidth, ScreenHeight, config.argb_8888); Mcanvas = new CAnvas (Mbitmap);//mcanvas.drawcolor (Color.White);} @Overrideprotected void OnDraw (canvas canvas) {canvas.drawbitmap (mbitmap, 0, 0, null); Canvas.drawpath (MPath, mpaint);} @Overridepublic boolean ontouchevent (Motionevent event) {float x = Event.getx (); Float y = event.gety (); switch (Event.geta Ction ()) {case MotionEvent.ACTION_DOWN:currentX = X;currenty = Y;mpath.moveto (CurrentX, currenty); break;case MotionEvent.ACTION_MOVE:currentX = X;currenty = Y;mpath.quadto (CurrentX, CurrentY, x, y); Draw line Break;case MotionEvent.ACTION_UP:mCanvas.drawPath (MPath, mpaint); Invalidate (); return true;} Public Bitmap Getpaintbitmap () {return resizeimage (Mbitmap, 320, 480);} Public Path GetPath () {return mPath;} Scale public static Bitmap Resizeimage (Bitmap Bitmap, int width, int height) {int originwidth = bitmap.getwidth (); Int Origi nheight = Bitmap.getheight (), Float scalewidth = ((float) width)/originwidth;float ScaleHeight = ((float) height)/Origi nheight; Matrix matrix = new Matrix (); Matrix.postscale (scalEwidth, ScaleHeight); Bitmap Resizedbitmap = Bitmap.createbitmap (Bitmap, 0, 0, originwidth,originheight, Matrix, True); return resizedbitmap;} Clear artboard public void clear () {if (Mcanvas! = null) {Mpath.reset (); Mcanvas.drawcolor (Color.transparent,            PorterDuff.Mode.CLEAR); Invalidate ();}}}

Writepaddialog.java

Package Com.jackie.handwriting;import Android.app.dialog;import Android.content.context;import android.os.Bundle; Import Android.util.displaymetrics;import Android.view.view;import Android.view.window;import Android.widget.button;import Android.widget.framelayout;import Android.widget.toast;public class WritePadDialog Extends Dialog {private Context mcontext;private writedialoglistener mwritedialoglistener;private paintview Mpaintview ;p rivate framelayout mframelayout;private Button Mbtnok, Mbtnclear, Mbtncancel;public writepaddialog (context context, Writedialoglistener Writedialoglistener) {super (context); This.mcontext = Context;this.mwritedialoglistener = Writedialoglistener;} @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Untitled Setcontentview (r.layout.write_pad); mframelayout = (framelayout) Findviewbyid (R.id.tablet_view);// Get screen size displaymetrics mdisplaymetrics = new Displaymetrics (); GetWindow (). Getwindowmanager (). Getdefaultdisplay (). Getmetrics (mdisplaymetrics); int screenwidth = Mdisplaymetrics.widthpixels;int screenheight = Mdisplaymetrics.heightpixels;mpaintview = new PaintView (MContext, ScreenWidth, ScreenHeight); Mframelayout.addview (Mpaintview); Mpaintview.requestfocus (); MBtnOK = (Button) Findviewbyid (R.ID.WRITE_PAD_OK); Mbtnok.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {if (Mpaintview.getpath (). IsEmpty ()) {Toast.maketext (mcontext, "please write down your name", Toast.length_short). Show (); return;} Mwritedialoglistener.onpaintdone (Mpaintview.getpaintbitmap ());d Ismiss ();}); Mbtnclear = (Button) Findviewbyid (r.id.write_pad_clear); Mbtnclear.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {mpaintview.clear ();}}); Mbtncancel = (Button) Findviewbyid (r.id.write_pad_cancel); Mbtncancel.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {cancel ();}});}}

Writediloglistener.java

Package com.jackie.handwriting;/** * Listening Trackpad dialog box * @author chengcj1 *  */public interface Writedialoglistener {public voi D Onpaintdone (Object object);}

The effect is as follows:






Android for tablet and doodle functionality

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.