Android-based tablet and graffiti Functions

Source: Internet
Author: User

Android-based tablet and graffiti Functions

The following code is used to simulate an Android tablet and graffiti:

Write_pad.xml

 

 
  
<Framelayout android: id = "@ + id/tablet_view" android: layout_height = "300dp" android: layout_width = "fill_parent"> </framelayout>
  
   
   
   
  
 

 

This is the main layout file of the tablet, and the hand-written part is a FrameLayout. The buttons "OK", "clear", and "cancel" are provided to save and erase the signature.

 

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 clas S MainActivity extends Activity {private ImageView mIVSign; private TextView mTVSign; private Bitmap mSignBitmap;/** Called when 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. set OnClickListener (new OnClickListener () {@ Overridepublic void onClick (View view) {WritePadDialog mWritePadDialog = new WritePadDialog (MainActivity. this, new WriteDialogListener () {@ Overridepublic void onPaintDone (Object object) {mSignBitmap = (Bitmap) object; createSignFile (); mIVSign. setImageBitmap (mSignBitmap); mTVSign. setVisibility (View. GONE) ;}}); mWritePadDialog. show () ;}}) ;}// create the private void crea signature file. TeSignFile () {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 it is set to Bitmap. compress (CompressFormat. JPEG, 100, fos) the image background is black mSignBitmap. co Mpress (Bitmap. CompressFormat. PNG, 100, 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 exte Nds View {private Paint mPaint; private Path mPath; private Bitmap mBitmap; private Canvas mCanvas; private int screenWidth, screenHeight; 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); // remove the Sawtooth 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 onTouchEve Nt (MotionEvent event) {float x = event. getX (); float y = event. getY (); switch (event. getAction () {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); break;} invalidate (); return true;} public Bitm Ap 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 originHeight = bitmap. getHeight (); float scaleWidth = (float) width)/originWidth; float scaleHeight = (float) height)/originHeight; Matrix matrix = new Matrix (); matrix. postScale (scale Width, scaleHeight); Bitmap resizedBitmap = Bitmap. createBitmap (bitmap, 0, 0, originWidth, originHeight, matrix, true); return resizedBitmap;} // clear the canvas 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 PaintView mPaintView; private FrameLayout comment; private Button mBtnOK, mBtnClear, mBtnCancel; public WritePadDialog (Context 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); // setContentView (R. layout. write_pad); mFrameLayout = (FrameLayout) findViewById (R. id. tablet_view); // obtain the screen size DisplayMetrics mDisplayMetrics = new DisplayMetrics (); getWindow (). getWindowManager (). getdefadisplay display (). 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 (); dismiss () ;}}); 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;/*** dialog box * @ author chengcj1 ***/public interface WriteDialogListener {public void onPaintDone (Object object Object );}

 

The effect is as follows:

 

 

 

 

 

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.