Android programming implements graffiti on bitmap _android

Source: Internet
Author: User
Tags clear screen gety

This article illustrates the effect of Android programming on bitmap. Share to everyone for your reference, specific as follows:

Layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
 android:layout_width=" fill_parent " 
 android:layout_height=" fill_parent " 
 android:o" rientation= "vertical" > 
 <linearlayout 
  android:id= "@+id/handwriteview" 
  Wrap_content " 
  android:layout_height=" wrap_content "/> 
 <linearlayout 
  android:layout_width=" Fill_parent " 
  android:layout_height=" fill_parent " 
  android:orientation=" Horizontal " 
  android: gravity= "Center_horizontal" > 
  <button 
   android:id= "@+id/clear" 
   android:layout_width= "200DP" 
   android:layout_height= "wrap_content" 
   android:text= "clear screen"/> 
 </LinearLayout> 
</ Linearlayout> 

Overridden View File:

public class Handwrite extends View {private Paint Paint = null; 
 Private Bitmap originalbitmap = null; 
 Private Bitmap new1bitmap = null; 
 Private Bitmap new2bitmap = null; 
 private float Clickx = 0,clicky = 0; 
 private float startx = 0,starty = 0; 
 Private Boolean ismove = true; 
 Private Boolean isclear = false; 
 private int color = Color.green; 
 private float strokewidth = 2.0f; 
  Public Handwrite (Context Context,bitmap B) {super (context); 
  Originalbitmap = Bitmap.createbitmap (b). Copy (Bitmap.Config.ARGB_8888, true); 
 New1bitmap = Bitmap.createbitmap (Originalbitmap); 
  public void Clear () {isclear = true; 
  New2bitmap = Bitmap.createbitmap (Originalbitmap); 
 Invalidate (); 
 public void SetStyle (float strokewidth) {this.strokewidth = Strokewidth; 
  } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); 
 Canvas.drawbitmap (Handwriting (New1bitmap), 0, 0,null); 
Public Bitmap Handwriting (Bitmap originalbitmap) {  Canvas Canvas = null; 
  if (isclear) {canvas = new canvas (NEW2BITMAP); 
  } else{canvas = new canvas (ORIGINALBITMAP); 
  } paint = new paint (); 
  Paint.setstyle (Style.stroke); 
  Paint.setantialias (TRUE); 
  Paint.setcolor (color); 
  Paint.setstrokewidth (Strokewidth); 
  if (ismove) {canvas.drawline (StartX, Starty, Clickx, clicky, paint); 
  } startx = Clickx; 
  Starty = clicky; 
  if (isclear) {return new2bitmap; 
 return originalbitmap; 
  @Override public boolean ontouchevent (Motionevent event) {Clickx = Event.getx (); 
  Clicky = Event.gety (); 
   if (event.getaction () = = Motionevent.action_down) {ismove = false; 
   Invalidate (); 
  return true; 
   else if (event.getaction () = = Motionevent.action_move) {Ismove = true; 
   Invalidate (); 
  return true; 
 Return Super.ontouchevent (event); 

 } 
}

Activity file:

The public class Handwritingactivity extends activity {/** called the ' when the ' is the ' is ' the ' activity ' is a-created private Linear 
 Layout handwrite = null; 
 Private Button clear = null;
 int requestwidth=116;
 int requestheight=173;
 int insamplesize; 
  @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  Setcontentview (r.layout.activity_hand_writing); 
  Handwrite = (linearlayout) Findviewbyid (R.id.handwriteview); 
  Clear = (Button) Findviewbyid (r.id.clear); 
 Clear.setonclicklistener (New Clearlistener ()); 
   Private class Clearlistener implements onclicklistener{public void OnClick (View v) {//Handwrite.clear ();
   Bitmapfactory.options opts = new Options (); Opts.injustdecodebounds = true;//Lets Bimapfactory parse this bitmap, get only the border information of the bitmap Bitmapfactory.decoderesource (Getresources (),
   R.drawable.cool, opts);
    if (Opts.outheight > Requestheight | | opts.outwidth > Requestwidth) {if (Opts.outwidth > Opts.outheight) { Insamplesize = Math.Round ((float) opts.outheight/(float) requestheight);
    else {insamplesize = Math.Round ((float) opts.outwidth/(float) requestwidth);
    } System.out.println ("width:" + opts.outwidth);
   System.out.println ("Height:" + opts.outheight);
   Opts.insamplesize = insamplesize;
   System.out.println (insamplesize); Opts.injustdecodebounds = false;//because it has been scaled, give way to the diagram factory really parse this bitmap//As we have parsed this input stream before, we need to reinitialize this input stream Bitmap B = Bitmapf
   Actory.decoderesource (Getresources (), R.drawable.cool, opts);
   Handwrite hw = new Handwrite (handwritingactivity.this, b);
   System.out.println (B.getwidth ());
  Handwrite.addview (HW); 

 } 
 } 
}

One of the integrated graffiti tool classes:

/** * Use Method: * 1. Create Tuyaview class instance * 2. Call Drawtuya method * 3. Parameter 1:context * 4. Parameter 2: byte[of image] byte array * 5. ImageView instance * 6.
Brush definition * **/import com.ziipin.lhdc.utils.ToastUtil;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Matrix;
Import Android.graphics.Paint;
Import android.graphics.BitmapFactory.Options;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.View.OnTouchListener;
Import Android.widget.ImageView;
 public class Tuyaview {//original picture private Bitmap Morignbitmap;
 Private Bitmap Meditbitmap;
 private int insamplesize;
 private int requestwidth = 500;
 private int requestheight = 700;
 /** Edit the canvas of the picture * * Private Canvas Mcanvas;
 Private ImageView image;
 Private Paint Mpaint;
  Public Bitmap Drawtuya (context context, byte[] _data, ImageView image, Paint mpaint) {this.image = image;
  This.mpaint = Mpaint; Morignbitmap = Bitmapfactory.decodeByteArray (_data, 0, _data.length);
 Return Showeditbitmap (context, _data, image); /** * Show edited picture/private Bitmap showeditbitmap (context, byte[] _data, ImageView image) {Morignbitmap =
  Getscalebitmap (_data, image);
  if (Morignbitmap = = null) {toastutil.show (context, edit error);
  } Meditbitmap = Morignbitmap.copy (Morignbitmap.getconfig (), true);
  Mcanvas = new Canvas (MEDITBITMAP);
  Mcanvas.drawbitmap (Morignbitmap, New Matrix (), New Paint ());
  Image.setimagebitmap (MEDITBITMAP);
  Image.setontouchlistener (Mtouchlistener);
 return meditbitmap; /** * Get results Zoom out of the picture * * @return/private Bitmap Getscalebitmap (byte[] _data, ImageView image) {Bitmapfacto Ry.
  Options opts = new options (); Opts.injustdecodebounds = true;//Let Bimapfactory parse this bitmap, get only the border information of the bitmap Bitmapfactory.decodebytearray (_data, 0, _
  Data.length, opts);
    if (Opts.outheight > Requestheight | | opts.outwidth > Requestwidth) {if (Opts.outwidth > Opts.outheight) { Insamplesize = Math.Round ((float) opts.outheight/(float) requestheight);
   else {insamplesize = Math.Round ((float) opts.outwidth/(float) requestwidth);
  } opts.insamplesize = Insamplesize; Opts.injustdecodebounds = false;//because it has been scaled, give way to the diagram factory really parse this bitmap//As we have parsed this input stream before, we need to reinitialize this input stream Bitmap BMP = Bitmapf
  Actory. Decodebytearray (_data, 0, _data.length, opts);
 return BMP;
  }//Touch event private Ontouchlistener Mtouchlistener = new Ontouchlistener () {int startx = 0;
  int starty = 0; @Override public boolean Ontouch (View v., motionevent event) {switch (event.getaction ()) {case Motionevent.action
    _down://fingers for the first time touch screen startx = (int) event.getx ();
    Starty = (int) event.gety ();
   Break
    Case Motionevent.action_move://Fingers on the imageview move int x = (int) event.getx ();
    int y = (int) event.gety ();
    Mcanvas.drawline (StartX, Starty, X, Y, mpaint);
    STARTX = (int) event.getx ();
    Starty = (int) event.gety (); Image.invalidatE ();
   Break
  return true;
}
 }; }

I hope this article will help you with your Android programming.

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.