Android simple to implement drawing function _android

Source: Internet
Author: User
Tags stub

How do you draw on the picture? Here has written a demo, for everyone to refer to
first, take a look at the structure of the project
Engineering Structure:

Two, custom view
This custom view implements the function of preserving the trajectory, as follows

Package Picturegame.view; 
Import Android.content.Context; 
Import Android.graphics.Bitmap; 
Import Android.graphics.BitmapFactory; 
Import Android.graphics.Canvas; 
Import Android.graphics.Color; 
Import Android.graphics.Paint; 
Import Android.graphics.Paint.Style; 
Import Android.util.AttributeSet; 
Import android.view.MotionEvent; 
Import Android.view.View; 
 
Import COM.WINTON.PICTUREGAME.R; public class Gameview extends view{private Paint Paint = null;//private Bitmap Originalbitmap = null;//Original 
   
  Figure private Bitmap new1bitmap = null; 
   
  Private Bitmap new2bitmap = null; 
   
  private float Clickx = 0; 
   
  private float clicky=0; 
   
  private float startx=0; 
   
  private float starty=0; 
   
  Private Boolean ismove = true; 
   
  Private Boolean isclear = false; private int color =color.red;//default brush color is red private float strokewidth =2.0f;//default brush bold public Gameview (Cont 
    Ext context) {this (context,null); TODO auto-generated constructor stub} public Gameview (context Context,attributeset Atts) {this (context,atts,0); TODO auto-generated Constructor stub} public Gameview (context Context,attributeset Atts,int Defstyle) {sup 
    ER (context,atts,defstyle); TODO auto-generated constructor stub originalbitmap = Bitmapfactory.decoderesource (Getresources (), R.DRAWABL e.default_pic). Copy (Bitmap.Config.ARGB_8888, true);//Load a background new1bitmap=originalbitmap.createbitmap ( 
  ORIGINALBITMAP); 
    }//Purge function public void clear () {isclear =true; 
    New2bitmap=originalbitmap.createbitmap (ORIGINALBITMAP); 
    Invalidate ()//overload} public void Setstrokewidth (float width) {this.strokewidth=width; 
  Initpaint (); @Override protected void OnDraw (Canvas Canvas) {//TODO auto-generated Method Stub Super.ondraw (Canvas) 
    ; 
     
  Canvas.drawbitmap (writer (new1bitmap), 0,0, NULL); @Override public Boolean Ontouchevent(Motionevent event) 
     
    {//TODO auto-generated method Stub 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); /** * @Title: Writer * @Description: TODO (Generate bitmap) * @param @param pic * @param settings file * @ 
     
    return Bitmap type * @throws/public Bitmap writer (Bitmap pic) {initpaint (); 
    Canvas Canvas =null; 
    if (isclear) {canvas=new canvas (new2bitmap); 
    }else{canvas=new canvas (pic); 
     
    } if (Ismove) {canvas.drawline (StartX, Starty, Clickx, clicky, paint);/dash} startx = Clickx; 
    Starty =clicky; 
    if (isclear) {return new2bitmap; } rEturn pic; 
     
    private void Initpaint () {paint = new paint ();//New Brush Paint.setstyle (Style.stroke);//set to draw line Paint.setantialias (TRUE);//You can make the line smooth some paint.setcolor (color);/Set the brush color Paint.setstrokewidth (s Trokewidth)//Set the thickness of the brush line}/** * @Title: SetColor * @Description: TODO (Set the external interface for line color) * @param @param colo 
    R Set File * @return void return type * @throws */public void setcolor (int color) {this.color=color; 
  Initpaint ();  } 
   
   
}


Three, home page layout file
Home page layout file

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" android:gravity= "center_horizontal" android:orientation= "vertical" > & Lt LinearLayout android:layout_width= "match_parent" android:layout_height= "50DP" android:orientation= "Horizo" 
      Ntal "> <linearlayout android:layout_width=" 0dp "android:layout_height=" Match_parent " android:layout_weight= "1" android:gravity= "center" > <textview android:id= "@+id/ Tv_30 "android:layout_width=" 30DP "android:layout_height=" 30DP "android:background=" @drawable/b 
      G_notifaction "/> </LinearLayout> <linearlayout android:layout_width=" 0DP " 
      android:layout_height= "Match_parent" android:layout_weight= "1" android:gravity= "Center" >  
       <textview Android:id= "@+id/tv_25" android:layout_width= "25DP" android:layout_height= "25DP" android:backg round= "@drawable/bg_notifaction"/> </LinearLayout> <linearlayout Android:lay Out_width= "0DP" android:layout_height= "Match_parent" android:layout_weight= "1" android:gravity= "Cente R "> <textview android:id=" @+id/tv_20 "android:layout_width=" 20DP "Android 
     
    : layout_height= "20DP" android:background= "@drawable/bg_notifaction"/> </LinearLayout> <linearlayout android:layout_width= "0DP" android:layout_height= "Match_parent" Android:lay 
        out_weight= "1" android:gravity= "center" > <textview android:id= "@+id/tv_15" 
        Android:layout_width= "15DP" android:layout_height= "15DP" android:background= "@drawable/bg_notifaction" /> </linearlayout> <linearlayout android:layout_width= "0DP" android:layout_height= "Match_paren T "android:layout_weight=" 1 "android:gravity=" center "> <textview android:id=" @+id/tv_10 "android:layout_width=" 10DP "android:layout_height=" 10DP "android:background=" @drawa 
      Ble/bg_notifaction "/> </LinearLayout> <linearlayout android:layout_width=" 0DP " 
      android:layout_height= "Match_parent" android:layout_weight= "1" android:gravity= "Center" > 
        <textview android:id= "@+id/tv_5" android:layout_width= "5DP" android:layout_height= "5DP" android:background= "@drawable/bg_notifaction"/> </LinearLayout> <linearlay 
      Out android:layout_width= "0DP" android:layout_height= "Match_parent" android:layout_weight= "1" android:gravity= "Center"> <textview android:id=" @+id/tv_2 "android:layout_width=" 2DP "Android:la yout_height= "2DP" android:background= "@drawable/bg_notifaction"/> </LinearLayout> < /linearlayout> <picturegame.view.gameview android:layout_width= "Match_parent" Android:layout_heigh 
    t= "300DP" android:id= "@+id/gameview"/> <button android:layout_width= "200DP" android:layout_height= "80DP" android:text= "clear" android:textcolor= "@color/black" android:id= "@+id/btn_cl  Ear "/> </LinearLayout>

Four, main activity code

Package com.winton.picturegame; 
Import Picturegame.view.GameView; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.widget.Button; 
 
Import Android.widget.TextView; 
 
Import com.winton.basemodule.BaseActivity; 
  public class Mainactivity extends Baseactivity implements Onclicklistener {private Gameview gameview = null; 
   
  Private Button clear = null; 
 
  Private TextView TV30,TV25,TV20,TV15,TV10,TV5,TV2; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub Super.oncre 
  Ate (savedinstancestate); @Override public void Initview () {//TODO auto-generated Method stub Setcontentview (r.layout.activity 
     
    _main); 
    gameview= (Gameview) Findviewbyid (R.id.gameview); 
    Clear = (Button) Findviewbyid (r.id.btn_clear); 
    tv30= (TextView) Findviewbyid (r.id.tv_30); 
    tv25= (TextView) Findviewbyid (r.id.tv_25); tv20= (TextView) FINDVIEWBYid (R.ID.TV_20); 
    tv15= (TextView) Findviewbyid (r.id.tv_15); 
    tv10= (TextView) Findviewbyid (R.ID.TV_10); 
    tv5= (TextView) Findviewbyid (r.id.tv_5); 
  Tv2= (TextView) Findviewbyid (r.id.tv_2); @Override public void Initlistener () {//TODO auto-generated method stub clear.setonclicklistener (thi 
    s); 
    Tv30.setonclicklistener (this); 
    Tv25.setonclicklistener (this); 
    Tv20.setonclicklistener (this); 
    Tv15.setonclicklistener (this); 
    Tv10.setonclicklistener (this); 
    Tv5.setonclicklistener (this); 
     
  Tv2.setonclicklistener (this); @Override public void InitData () {//TODO auto-generated a stub} @Override public Voi 
      D OnClick (View v) {//TODO auto-generated Method stub if (v==clear) {gameview.clear (); 
    Return 
      } if (V==TV30) {gameview.setstrokewidth (30f); 
    Return 
      } if (v==tv25) {gameview.setstrokewidth (25f); 
    Return } if (V==TV{gameview.setstrokewidth (20f); 
    Return 
      } if (v==tv15) {gameview.setstrokewidth (15f); 
    Return 
      } if (V==TV10) {gameview.setstrokewidth (10f); 
    Return 
      } if (V==TV5) {gameview.setstrokewidth (5f); 
    Return 
      } if (V==TV2) {gameview.setstrokewidth (2f); 
    Return  } 
     
  } 
    
}

Five, the effect
The Operation effect chart is as follows

Vi. questions
When the line becomes thicker, the line appears as a discontinuity in the above image. May I ask the master how to deal with this? I guess I should run the algorithm, but I don't know how to run it yet.

The article for everyone to introduce to this, in fact, there are many knowledge points small series is not very clear, I hope you can carry out the expansion of common progress.

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.