To achieve this feature, you need the following steps first:
1, to achieve a Drawingview class, this class inherits view;
2, rewrite the construction method of its parent class, and initialize the paint brush, canvas brush and path;
3, rewrite the OnDraw method, in this method is the main graffiti painting work;
4, rewrite the Ontouchevent event, in this method to calculate the current brush to the left, planning the path of the brush, call the Invalidate () method to constantly call the OnDraw method to draw;
5, custom Setpattern method to set the brush stroke effect;
6. Introduce a custom Drawdingview object in mainactivity and its layout file;
The code is as follows:
public class Drawingview extends View {private Path drawpath; Private Paint Drawpaint; Private Paint Canvaspaint; private int paintcolor = 0xff000000; Private Bitmap Canvasbitmap; Private Canvas Drawcanvas; Public Drawingview (Context context) {super (context,null); } @Override protected void onsizechanged (int w, int h, int oldw, int oldh) {super.onsizechanged (W, H, OLDW, OLDH); Canvasbitmap = Bitmap.createbitmap (w,h, Bitmap.Config.ARGB_8888); Drawcanvas = new Canvas (CANVASBITMAP); } public Drawingview (context context, AttributeSet Attrs) {Super (context, attrs); Setupdrawing (); } private void Setupdrawing () {drawpaint = new Paint (); DrawPath = new Path (); Drawpaint.setcolor (Paintcolor); Drawpaint.setantialias (TRUE); Drawpaint.setstrokewidth (50); Drawpaint.setstyle (Paint.Style.STROKE); Drawpaint.setstrokejoin (Paint.Join.ROUND); Drawpaint.setstrokECap (Paint.Cap.ROUND); Canvaspaint = new Paint (Paint.dither_flag); } @Override protected void OnDraw (canvas canvas) {canvas.drawbitmap (canvasbitmap,0,0,canvaspaint); Canvas.drawpath (Drawpath,drawpaint); } @Override public boolean ontouchevent (Motionevent event) {Float Touchx = Event.getx (); Float touchy = event.gety (); Switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:drawPath.moveTo (touchx,touchy); Case MotionEvent.ACTION_MOVE:drawPath.lineTo (Touchx,touchy); Case MotionEvent.ACTION_UP:drawPath.lineTo (Touchx, touchy); Drawcanvas.drawpath (DrawPath, drawpaint); Drawpath.reset (); break; Default:return false; } invalidate (); return true; } public void Setpattern (String newpattern) {invalidate (); int patternid = Getresources (). Getidentifier (Newpattern, "drawable", "Com.example.lucky.drawapp"); Bitmap patternbmp = Bitmapfactory.decoderesource (Getresources (), Patternid); Bitmapshader Patternbmpshader = new Bitmapshader (patternbmp, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); Drawpaint.setcolor (0xFFFFFFFF); Drawpaint.setshader (Patternbmpshader); }}Layout file:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "a Ndroid:background= "#FFCCCCCC" android:orientation= "vertical" tools:context= ". Mainactivity "> <com.example.lucky.drawapp.drawingview android:id=" @+id/draw_view "a Ndroid:layout_width= "Fill_parent" android:layout_height= "0DP" android:layout_marginbottom= "3DP" android:layout_marginleft= "5DP" android:layout_marginright= "5DP" android:layout_margintop= "3d P "android:layout_weight=" 1 "android:background=" #FFFFFFFF "/> <linearlayout Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_gravity= "Center" android:orientation= "vertical" > <!-- Top row---<linearlayout android:layout_width= "Wrap_content" Android:layo ut_height= "wrap_content" android:orientation= "Horizontal" > <imagebutton Android:layout_width= "30DP" android:layout_height= "30DP" Android:layout_margin = "2DP" android:background= "@drawable/pattern1" android:contentdescription= "pattern" android:onclick= "paintclicked" android:tag= "Pattern1"/> <image Button android:layout_width= "30DP" android:layout_height= "30DP" a Ndroid:layout_margin= "2DP" android:background= "@drawable/pattern2" android:contentde scription= "pattern" android:onclick= "paintclicked" android:tag= "pattern2"/> <imagebuttOn android:layout_width= "30DP" android:layout_height= "30DP" Andro Id:layout_margin= "2DP" android:background= "@drawable/pattern3" Android:contentdescri ption= "pattern" android:onclick= "paintclicked" android:tag= "Pattern3"/> <imagebutton android:layout_width= "30DP" android:layout_height= "30DP" Android:layout_margin= "2DP" android:background= "@drawable/pattern4" a ndroid:contentdescription= "pattern" android:onclick= "paintclicked" android:tag= "Patt Ern4 "/> </LinearLayout> <!--bottom row-to-<linearlayout Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:orientat ion= "Horizontal" > <imagebutton android:layout_width= "30DP" android:layout_height= "30DP "Android:layout_margin=" 2DP "android:background=" @drawable/pattern5 " android:contentdescription= "pattern" android:onclick= "paintclicked" Android:tag = "Pattern5"/> <imagebutton android:layout_width= "30DP" Android : layout_height= "30DP" android:layout_margin= "2DP" android:background= "@drawable/patt Ern6 "android:contentdescription=" pattern "android:onclick=" paintclicked " android:tag= "Pattern6"/> <imagebutton android:layout_width= "30DP" android:layout_height= "30DP" android:layout_margin= "2DP" android:backgr ound= "@drawable/pattern7"android:contentdescription=" pattern "android:onclick=" paintclicked " android:tag= "Pattern7"/> <imagebutton android:layout_width= "30DP" android:layout_height= "30DP" android:layout_margin= "2DP" Android:background = "@drawable/pattern8" android:contentdescription= "pattern" android:onclick= "paintcli Cked "android:tag=" Pattern8 "/> </LinearLayout> </LinearLayout> </LinearLayout>
Code in activity:
public class Mainactivity extends Activity {private Drawingview drawview; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Drawview = (Drawingview) Findviewbyid (R.id.draw_view); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio n Bar if it is present. Getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent Activity in Androidmanifest.xml. int id = item.getitemid (); if (id = = r.id.action_settings) {return true; } return super.onoptionsitemselected (item); } public void paintclicked (view view) { String pattern = View.gettag (). toString (); Drawview.setpattern (pattern); }}As follows:
So random Android Doodle Tool