Learn about Android to add graffiti (text) (37)

Source: Internet
Author: User

Original:


:



The code is as follows:

public class Graffitiview extends View {private paint paint = null;     /* Source Map */private Bitmap originalbitmap = null;      /* * Pictures that require graffiti */private Bitmap new1bitmap = null;     /* * before graffiti */private Bitmap new2bitmap = null;    /* * x, y coordinates when touching */private float Clickx = 0;      private float clicky = 0;    /* * The starting point for each draw */private float StartX = 0;      private float starty = 0;      /* * Whether to draw */private Boolean ismove = true;      /* * Whether to empty */private Boolean isclear = false;     /* * Brush color */private int color = Color.green;          /* NIB Size */private float strokewidth = 2.0f;          Public Graffitiview (context context, AttributeSet Attrs) {Super (context, attrs);          Originalbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.hh). Copy (Bitmap.Config.ARGB_8888, true);      New1bitmap = Bitmap.createbitmap (Originalbitmap);              }       @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas);                Canvas.drawbitmap (Handwriting (new1bitmap,isclear), 0, 0,null);  }/** * Doodle pattern * @param originalbitmap * @param isclear empty * @return Doodle Pattern * * Public                    Bitmap Handwriting (Bitmap originalbitmap,boolean isclear) {canvas canvas = null;          if (isclear) {//Empty canvas = new canvas (NEW2BITMAP);          } else{//do not empty canvas = new canvas (ORIGINALBITMAP);          } paint = new paint ();          Paint.setstyle (Style.stroke);          Paint.setantialias (TRUE);          Paint.setcolor (color);          Paint.setstrokewidth (Strokewidth);          if (ismove) {//When moving, Draw Canvas.drawline (StartX, Starty, Clickx, clicky, paint);          }/* * The starting point for each draw */StartX = Clickx;                    Starty = clicky;    if (isclear) {//Empty        return new2bitmap;      } return Originalbitmap;          } @Override public boolean ontouchevent (Motionevent event) {Clickx = Event.getx ();          Clicky = Event.gety ();  if (event.getaction () = = Motionevent.action_down) {/* * When pressed, does not move, refreshes, but does not draw, just records when the coordinate point is pressed, according to (Ismove)             。              */Ismove = false;              Invalidate ();          return true;         The Else if (event.getaction () = = Motionevent.action_move) {/* * is drawn according to (Ismove) when moved, starting at the coordinate point that is pressed.              */Ismove = true;              Invalidate ();          return true;      } return Super.ontouchevent (event);          }/** * Empty */public void Clear () {isclear = true;          New2bitmap = Bitmap.createbitmap (Originalbitmap);      Invalidate (); }/** * Sets the width of the scribble line * @param strokewidth width */public void SetStyle (float strOkewidth) {this.strokewidth = Strokewidth;    }/** * Sets the brush color * @param color color */public void setcolor (int color) {this.color=color;   }     }



Activity_main.xml:

<?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" >    <com.example.tuya.view.graffitiview        android:id= "@+id/handwriteview"        Android:layout_width= "Fill_parent"        android:layout_height= "500DP"/>    <linearlayout        android: Layout_width= "Fill_parent"        android:layout_height= "fill_parent"        android:gravity= "Center_horizontal"        android:orientation= "Horizontal" >        <button            android:id= "@+id/clear"            android:layout_ Width= "200DP"            android:layout_height= "wrap_content"            android:text= "clear screen"/>    </LinearLayout> </LinearLayout>



Mainactivity:

public class Mainactivity extends Activity {private Graffitiview handwrite = null;private Button clear = null; @Overridepub LIC void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); handwrite = (Graffitiview) Findviewbyid (R.id.handwriteview); Handwrite.setstyle (n); Handwrite.setcolor ( color.red); clear = (Button) Findviewbyid (r.id.clear); Clear.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {handwrite.clear ();}});}}



Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/45643093 Emotional Control _





Learn about Android to add graffiti (text) (37)

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.