Overview:
Very early on to study the android graffiti, in fact, it is not the study, after all, are some relatively simple knowledge points. The following is the canvas and touch events (ontouchevent) to achieve graffiti and scratch music.
Reference:
http://blog.csdn.net/lmj623565791/article/details/40162163
This person's blog is really good, want to learn the students can also refer to this Daniel's other blog.
http://blog.csdn.net/t12x3456/article/details/10432935
Sample Analysis:
Here are two simple introductory examples: graffiti technology and some simple analysis and effect impressions of scratch music.
1. GraffitiThinking Analysis and code display
The students who have studied canvas should know that we can leave the track of touch screen on canvas by covering a canvas on a view and realizing the Ontouchevent method of view, and there is a class for track record that needs to understand--path. For more information on canvas, please click here to view.
When you draw the interface, Android gets the size and position of the controls in the layout and then draws them. And here we just draw by Onmeasure and OnDraw, not onlayout because there is only one control, there is not too much dynamic layout to handle. Records for paths require a ontouchevent implementation.
Measurement size:
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) { super.onmeasure (Widthmeasurespec, HEIGHTMEASURESPEC); int width = getmeasuredwidth (); int height = getmeasuredheight (); Initialize Bitmap mbitmap = bitmap.createbitmap (width, height, config.argb_8888); Mcanvas = new Canvas (MBITMAP); }
Draw:
protected void OnDraw (canvas canvas) { drawpath (); Canvas.drawbitmap (mbitmap, 0, 0, null); }
Path drawing:
We keep track of the path we touch through path. As follows:
private void DrawPath () { mfingerpaint.setstyle (Paint.Style.STROKE); Mcanvas.drawpath (MPath, mfingerpaint); }
Touch events:
A very important and non-negligible class for touch events is motionevent. It has the following three commonly used action events:
1.MotionEvent. Action_down //Touch When pressed
2.MotionEvent. Action_move //Touch in the process of moving
3.MotionEvent. action_up //Touch away
Here's a look at the implementation of the Ontouchevent event:
public boolean ontouchevent (Motionevent event) { int action = Event.getaction (); int x = (int) event.getx (); int y = (int) event.gety (); Switch (action) {case Motionevent.action_down: actionmotioneventdown (x, y); break; Case Motionevent.action_move: actionmotioneventmove (x, y); break; } Invalidate (); return true; }
In the above code, we implement the logic of the press when we press it and implement the move logic when the finger moves on the screen.and don't forget the invalidate ()。 The main function of the invalidate () function is to request that the view tree be redrawn, and if you do not call it, the result is that nothing will happen.
2. Scraping MusicThinking Analysis and code display
Analysis: In fact, scraping the idea of the implementation of music and graffiti very much like, are in a place blind painting, and leave traces (joking, but also justified. ^_^). But one thing that's different is that the brush passes through the process of scraping, and it becomes transparent. Here you might say, that's easy, not just to cover two layers of pictures, to draw the touch path, just the color of the touch path is transparent. Is that really the case? You can give it a try. Of course, this is not feasible, about the final effect of practice you can try it on their own. The key point here is that we're going to erase the top layer and keep the bottom. This is where the graphic mixing technique is used.
Graphics mixing Technology A name is not the feeling is very advanced, but it is really very cow technology, but Java has been packaged for us, we just know how to use it, and use it is not so difficult.
For a detailed description of the graphics mix, you can refer here, I will not repeat the production of wheels. But I'd like to briefly introduce one of the Xfermode three sub-classes: Porterduffxfermode. This is a very powerful conversion mode, and using it, you can use any of the 16 porter-duff rules of the image composition to control how paint interacts with existing canvas images.
The Porter-duff rules are as follows:
Porterduff.mode is an enumeration class, with a total of 16 enumeration values:
1.porterduff.mode.CLEAR
The drawing is not submitted to the canvas.
2.porterduff.mode.SRC
Show Top Draw picture
3.porterduff.mode.DST
Show Lower drawing pictures
4.porterduff.mode.Src_over
Normal drawing shows that the upper and lower layers are drawn with overlapping covers.
5.porterduff.mode.Dst_over
The upper and lower layers are displayed. The lower level is shown on the home.
6.porterduff.mode.src_in
Draw the intersection of two layers. Displays the upper layer.
7.porterduff.mode.dst_in
Draw the intersection of two layers. Displays the lower layer.
8.porterduff.mode.Src_out
Draw the non-intersecting portion of the upper layer.
9.porterduff.mode.Dst_out
Remove the layer to draw the non-intersecting part.
10.porterduff.mode.Src_atop
Remove the non-intersecting part of the layer from the upper intersection section
11.porterduff.mode.Dst_atop
Take the upper non-intersecting part and the lower part of the intersection
12.porterduff.mode.XOR
XOR: Remove the two Layer intersection section
13.porterduff.mode.Darken
Take two layers of all areas, the intersection part of the color deepened
14.porterduff.mode.Lighten
Take two layers all, light the intersection part color
15.porterduff.mode.MULTIPLY
Take two layer intersection part overlay color
16.porterduff.mode. Screen
Take two layers of all areas, the intersection part becomes transparent color
What we need is: dstout this one. This is what we do in the code:
private void DrawPath () { mfingerpaint.setstyle (Paint.Style.STROKE); Sets the pattern at the intersection of two pictures (remove layer to draw non-intersecting portions) mfingerpaint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.DST_OUT)); Mcanvas.drawpath (MPath, mfingerpaint); }
The measurement and drawing process is as follows:
@Override protected void OnDraw (canvas canvas) {canvas.drawtext (MText, GetWidth () /2-mtextbound.width ()/2, GetHeight ()/2 + mtextbound.height ()/2, mbackpint); if (!iscomplete) {DrawPath (); Canvas.drawbitmap (mbitmap, 0, 0, NULL); }} @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widt Hmeasurespec, Heightmeasurespec); int width = getmeasuredwidth (); int height = getmeasuredheight (); Initialize Bitmap Mbitmap = bitmap.createbitmap (width, height, config.argb_8888); Mcanvas = new Canvas (MBITMAP); Draw cover Layer Mfingerpaint.setstyle (Paint.Style.FILL); Mcanvas.drawroundrect (New RECTF (0, 0, width, height), +, mfingerpaint); Mcanvas.drawbitmap (Bitmapfactory.decoderesource (Getresources (), r.drawable.mask), NULL, new RECTF (0, 0, width, height ), NULL); }
There is also a blog that uses this technology, click here to view.
Source Download:
http://download.csdn.net/detail/u013761665/8737527
Android graffiti technology and scraping music sample analysis