Android programming to achieve hand-painted and saved as a picture of the method (with demo source download) _android

Source: Internet
Author: User
Tags stub

This article describes the Android programming implementation of hand-painted and saved as a picture of the method. Share to everyone for your reference, specific as follows:

Run Effect Chart preview:

Should be yzuo_08 required to do this demo, with the previous tablet demo is different from the canvas can be saved as a picture of the content.

Attach Key code:

Mainview.java

Package com.tszy.views; 
Import Java.io.File; 
Import java.io.FileNotFoundException; 
Import Java.io.FileOutputStream; 
Import java.io.IOException; 
Import Android.content.Context; 
Import Android.graphics.Bitmap; 
Import Android.graphics.Bitmap.CompressFormat; 
Import Android.graphics.Bitmap.Config; 
Import Android.graphics.Canvas; 
Import Android.graphics.Color; 
Import Android.graphics.Paint; 
Import Android.graphics.Path; 
Import Android.util.AttributeSet; 
Import android.view.MotionEvent; 
Import Android.view.View; 
 public class Mainview extends View {private Paint Paint; 
 Private Canvas Cachecanvas; 
 Private Bitmap Cachebbitmap; 
 private path Path; 
 private int CLR_BG, CLR_FG; 
  Public Mainview (context, AttributeSet attrs) {Super (context, attrs); 
  CLR_BG = Color.White; 
  CLR_FG = Color.cyan; 
  Paint = new paint (); Paint.setantialias (TRUE); Anti-aliasing paint.setstrokewidth (3); Line width paint.setstyle (Paint.Style.STROKE); Draw Contour Paint.setcolor (CLR_FG); Color path = new Path (); 
  Create a bitmap of the screen size, as a buffer Cachebbitmap = Bitmap.createbitmap (config.argb_8888); 
  Cachecanvas = new Canvas (CACHEBBITMAP); 
 Cachecanvas.drawcolor (CLR_BG); 
 Public Mainview {Super (context); 
  } @Override protected void OnDraw (Canvas Canvas) {canvas.drawcolor (CLR_BG); 
  Draw the previous one, otherwise incoherent canvas.drawbitmap (cachebbitmap, 0, 0, NULL);
 Canvas.drawpath (path, paint); 
  /** * Empty canvas/public void Clear () {path.reset (); 
  Cachecanvas.drawcolor (CLR_BG); 
 Invalidate (); /** * Saves the contents of the canvas to the file * @param filename * @throws filenotfoundexception */public void SaveToFile (String fil 
  ename) throws FileNotFoundException {file F = new file (filename); if (f.exists ()) throw new RuntimeException ("file: + filename +" already exists!) 
  "); 
  FileOutputStream fos = new FileOutputStream (new File (filename)); 
  Compress the bitmap into other formats of the picture data cachebbitmap.compress (compressformat.png, FOS); try {fos.clOSE (); 
  catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); 
 } private float cur_x, cur_y; 
 Private Boolean ismoving; @Override public boolean ontouchevent (Motionevent event) {//TODO auto-generated method stub float x = Event.getx 
  (); 
  Float y = event.gety (); 
    Switch (event.getaction ()) {case Motionevent.action_down: {cur_x = x; 
    cur_y = y; 
    Path.moveto (cur_x, cur_y); 
    Ismoving = true; 
   Break 
    Case Motionevent.action_move: {if (!ismoving) break; 
    Two times curve way to draw path.quadto (cur_x, cur_y, x, y); 
    The following method looks like the above//Path.lineto (x, y); 
    cur_x = x; 
    cur_y = y; 
   Break 
    Case MOTIONEVENT.ACTION_UP: {//mouse bounce Save last State cachecanvas.drawpath (path, paint); 
    Path.reset (); 
    Ismoving = false; 
   Break 
  }//Notification Refresh interface invalidate (); 
 return true;

 } 
}

Activity code:

@Override public void OnClick (View v) {//TODO auto-generated a stub switch (V.getid ()) {case R.id.iv_b 
    Tn_clear:view.clear (); 
   Break Case R.id.iv_btn_save: {try {String sdstate = environment.getexternalstoragestate ()//To determine if SD card exists//check Check if the SD card is available if (!sdstate.equals (Android.os.Environment.MEDIA_MOUNTED)) {Toast.maketext (this, "SD card is not ready!") 
      ", Toast.length_short). Show (); 
     Break 
     //Get the system picture storage path, File path = Environment.getexternalstoragepublicdirectory (environment.directory_pictures); 
     Make sure the Pictures directory exists. 
     Path.mkdirs (); 
     Generates a picture name based on the current time Calendar C = calendar.getinstance (); String name = "" + c.get (calendar.year) + c.get (calendar.month) + c.get (calendar.day_of_month) + c.get (Cal Endar. 
     Hour_of_day) + c.get (calendar.minute) + c.get (Calendar.second) + ". png"; 
 Synthetic full path, note/delimiter string = Path.getpath () + "/" + name;    View.savetofile (string); Toast.maketext (This, "Save success!) 
    \ n file is guaranteed to exist: "+ String, Toast.length_long. Show (); catch (FileNotFoundException e) {Toast.maketext (this, "Save failed!) 
    \ n "+ E, Toast.length_long). Show (); 
   } break;

 } 
  } 
}

No difficulty, mainly will be bitmap to PNG pictures there, find a Canvas not directly or indirectly save the method, just here I use double buffering, another piece of canvas content bitmap created, it is natural to think of this canvas bitmap saved as a file.

Then look at the Bitmap there is a compress (compressformat format, int quality,outputstream stream) method, it is obvious that the output of the file passed to this method is OK

@Override public void OnClick (View v) {//TODO auto-generated a stub switch (V.getid ()) {case R.id.iv_b 
    Tn_clear:view.clear (); 
   Break Case R.id.iv_btn_save: {try {String sdstate = environment.getexternalstoragestate ()//To determine if SD card exists//check Check if the SD card is available if (!sdstate.equals (Android.os.Environment.MEDIA_MOUNTED)) {Toast.maketext (this, "SD card is not ready!") 
      ", Toast.length_short). Show (); 
     Break 
     //Get the system picture storage path, File path = Environment.getexternalstoragepublicdirectory (environment.directory_pictures); 
     Make sure the Pictures directory exists. 
     Path.mkdirs (); 
     Generates a picture name based on the current time Calendar C = calendar.getinstance (); String name = "" + c.get (calendar.year) + c.get (calendar.month) + c.get (calendar.day_of_month) + c.get (Cal Endar. 
     Hour_of_day) + c.get (calendar.minute) + c.get (Calendar.second) + ". png"; 
  Synthetic full path, note/delimiter string = Path.getpath () + "/" + name;   View.savetofile (string); Toast.maketext (This, "Save success!) 
    \ n file is guaranteed to exist: "+ String, Toast.length_long. Show (); catch (FileNotFoundException e) {Toast.maketext (this, "Save failed!) 
    \ n "+ E, Toast.length_long). Show (); 
   } break;

 } 
  } 
}

Full instance code click here to download the site.

I hope this article will help you with the Android program.

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.