What skills can we get to achieve scraping cards?
* The realization principle of circular fillet picture
* Double Buffering Technology Drawing
* Bitmap get pixel value data
* Gets the long width of the drawn text
* Control of Custom View
* Get screen density
* Typevalue.applydemension
* Some rendering methods of canvas
* Some common properties of the paint
* Some methods of path
The realization principle diagram of scraping and scraping card
This pattern is used for the dstout in the 13 pattern.
For this picture, first DST is drawn, the Xfermode is set, and the SRC is drawn.
The realization principle step of scraping and scraping card
1. Draw the text that shows the winning
2. Draw Layer Area/foreground picture
3. Set Xfermode
4. Draw Path
5. Clear all Layer area scheme
6. Custom Properties
Code explanation
The first part: Draw the Lottery information
public class Guaguale extends view{/** winning text variable/private String mtext;//display text private Paint mtextpaint;//Draw text Brush Priva
Te Rect mtextbounds;//Text's Boundary public Guaguale (context, NULL);
Public Guaguale (context, AttributeSet attrs) {Super (context, attrs);
Initview (); /** Initialize some instance variables/private void Initview () {settextmessage ();//Some variables of instantiated text} private void Settextmessage () {Mtext
= "Thank you for your patronage";
Mtextbounds = new Rect ();
Mtextpaint = new Paint ();
Mtextpaint.setantialias (TRUE);
Mtextpaint.setcolor (Color.dkgray);
Mtextpaint.settextsize (22); Mtextpaint.gettextbounds (Mtext,0,mtext.length (), mtextbounds),//Get to draw the text of the long width} @Override protected void onmeasure (int w
Idthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, Heightmeasurespec); @Override protected void OnDraw (Canvas Canvas) {//Draw text Canvas.drawtext (Mtext,getwidth ()/2-mtextbounds.width ()/2,
GetHeight ()/2 + mtextbounds.height ()/2,mtextpaint);
}
}
The following figure:
Part Two: Draw Layer area/foreground picture
/** Artboard Variable * * Private Canvas mcanvas;//cushion artboard Private Bitmap mlayerbimtap;//layer picture Private Bitmap mbitmap;//drawing paper Private P Aint mpaint;//brush @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (wid
Thmeasurespec, Heightmeasurespec); Setcanvasmessage (Getmeasuredwidth (), Getmeasuredheight ());//Instantiate some variables of the cache artboard}/*** * Double buffering technology * 1. Create a Canvas object that is consistent with the screen drawing area * 2. Draw the graphic into memory first * 3. Once again, copy the graph on the object to the screen * @param width * @param height */private void setcanvasmessage (int width,int Heig
HT) {mpaint = new Paint ();
Mpaint.setantialias (TRUE);
Mpaint.setstyle (Paint.Style.FILL);
Mpaint.setstrokewidth (3);
Mpaint.setcolor (Color.parsecolor ("#c0c0c0")); Mbitmap = Bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888);//Create a blank bitmap Mcanvas = new Canvas (MBITMAP);// Paint on this bitmap//mcanvas.drawcolor (Color.parsecolor ("#c0c0c0"))//Draw background color mcanvas.drawroundrect (new RECTF (0, 0, width, Height), mpaint, and/or drawing rounded rectangles Mlayerbimtap = Bitmapfactory.decoderesourCE (getresources (), R.mipmap.fg_guaguaka); Mcanvas.drawbitmap (Mlayerbimtap, NULL, new RECTF (0, 0, width, height), null);//Draw drawing layer picture @Override protected void OnDraw (C Anvas canvas) {//Draw text Canvas.drawtext (Mtext,getwidth ()/2-mtextbounds.width ()/2,getheight ()/2 + mtextbounds.height ()
/2,mtextpaint);
Draw Layer//canvas.drawbitmap (mbitmap,0,0,null);
Draw Layer Picture Canvas.drawbitmap (mbitmap,0,0,null);
}
}
The following figure (layer color):
The following figure (layer picture):
Part III: Setting up Xfermode and drawing path
/** PATH Variable * * Private path MPath;
Private Paint Mpathpaint; private int mlastx;//record last x coordinate private int mlasty;//record last Y coordinate/** initialize some instance variable * * private void Initview () {settextmessage () ;//Some variables of the instantiated text Setpathmessage ();//Some variables of the instantiated path} @Override public boolean ontouchevent (Motionevent event) {int action
= Event.getaction ();
Gets the coordinates of the finger on the screen int x = (int) event.getx ();
int y = (int) event.gety ();
Switch (action) {Case MotionEvent.ACTION_DOWN:mLastX = x;
Mlasty = y;
Mpath.moveto (Mlastx,mlasty);
Break
Case MotionEvent.ACTION_MOVE:int dx = math.abs (X-MLASTX);
int dy = Math.Abs (y-mlasty);
if (dx > 3 | | dy >3) {mpath.lineto (x, y);
} MLASTX = x;
Mlasty = y;
Break
Case MotionEvent.ACTION_UP:break;
Default:break; Invalidate ()//main thread refresh return true;//intercept event down distribution} @Override protected void OnDraw (Canvas Canvas) {//Draw text Canvas.dr
Awtext (Mtext,getwidth ()/2-mtextbounds.width ()/2,getheight ()/2 + mtextbounds.height ()/2,mtextpaint);
Draw Path DrawPath ();
Draw picture "layer background color"//canvas.drawbitmap (mbitmap,0,0,null);
Draw picture "layer picture" Canvas.drawbitmap (mbitmap, 0, 0, NULL); ///Return to System canvas is a bitmap with a path private void DrawPath () {//This was previously set up layer picture Mpathpaint.setxfermode (new Porterduffxfermode (Port
ErDuff.Mode.DST_OUT));
Mcanvas.drawpath (Mpath,mpathpaint);
}
The following figure
Part Three: Clear all layer area schemes
/** Erase whether completed * * private volatile Boolean iscomplete;
/** Callback Interface * * Private Oncompletelistener Moncompletelistener;
public interface oncompletelistener{void complete (); The public method of the call to the external setting is publicly void Setmoncompletelistener (Oncompletelistener moncompletelistener) {This.moncompletelistener
= Moncompletelistener; Case MOTIONEVENT.ACTION_UP://Open a thread to compute the area of the erase new thread (new Runnable () {@Override public void run () {in
T width = getwidth ();
int height = getheight ();
int area = 0;//records the range size of the number of scratches int percent;
for (int i=0;i<width;i++) for (int j = 0;j
The following figure:
Part five: Customizing the View's properties
Define <declare-styleable name= "Guaguale" > <attr name= "Text" format= "string" in Values/attrs.xml ></attr > <attr name= "textsize" format= "Dimension" ></attr> <attr name= "textcolor" format= "Color" ></ attr> </declare-styleable> define attributes in the layout file <zlll.bg.com.example.cn.myapplication.view.guaguale android:id= " @+id/guaguale "android:layout_width=" 350DP "android:layout_height=" 200DP "app:text=" $5,000 "app:textcolor=" #c02040 "App:textsize=" 22SP "android:layout_centerinparent=" true/> Defines the text text in the main interface public class Mainactivity extends Appco
mpatactivity {@Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Guaguale Guagualeview = (guaguale) Findviewbyid (R.id.guaguale);
Registered an observer Guagualeview.setmoncompletelistener (new Guaguale.oncompletelistener () {@Override public void complete () { Toast.maketext (Mainactivity.this, "the user draws most of it", Toast.lenGth_short). Show ();
}
});
Guagualeview.settextbyuser ("Android new skill get"); }/** winning text variable/private String mtext;//display text private Paint mtextpaint;//drawing text's brush private Rect mtextbounds;//text boundaries//below Two are the new private int mtextsize;//text size private int mtextcolor;//text color//external settings called setting text expose method public void Settextbyuser (String
Text) {mtext = text; Mtextpaint.gettextbounds (Mtext,0,mtext.length (), mtextbounds);//recalculate range of text} Define public Guaguale in the constructor
, AttributeSet Attrs) {Super (context, attrs);
Gets the value of the custom property TypedArray a = Context.obtainstyledattributes (Attrs,r.styleable.guaguale);
Mtext = a.getstring (R.styleable.guaguale_text);
Mtextcolor = A.getcolor (R.styleable.guaguale_textcolor, Color.parsecolor ("#c0c0c0")); Inside the typedvalue.applydimension is the conversion of SP,DP to px mtextsize = a.getdimensionpixelsize (r.styleable.guaguale_textsize, int
) Typedvalue.applydimension (Typedvalue.complex_unit_sp,22,getresources (). Getdisplaymetrics ()));
A.recycle ();
Initview ();
}
The following figure:
The following figure is the user's own set of text
Through the above several steps, can complete quack card realization effect.
To sum up:
(1) The principle of circular fillet picture
1. Drawing pictures
2.paint.setxfermode (Dstin); – The first layer to draw the intersection of the main DST is the background picture
3. Draw a circular picture
1. Draw a circular picture
2.paint.setxfermode (srcin); -the layer that is drawn after the intersection is the main src is the structure picture
3. Drawing pictures
(2) Double buffering technology drawing "drawing in memory, and then a one-time bitmap copy to the screen"
1. Create a canvas in memory that is the same width as the canvas
2. Drawing in Buffer
3. Copy the buffer bitmap to the current canvas
4. Free buffer
Your understanding is that you define an artboard in memory, then draw on it, put the drawing results in a bitmap, and finally let the system's canvas draw this bitmap
(3) Bimtap get pixel data (way one)
Bm.getpixels (X,y) directly obtains the pixel data at the x,y coordinates
(4) Get the width of the drawing text (mode one)
1. Compute the rectangle in which the text is located, and get the width height [Rect]
Rect Rect = new Rect ();
Paint.gettextbounds (Str,0,str,length,rect);
int w = rect.width;
int h = rect.height;
(5) Custom View
1. New Attrs.xml file in the values directory
2. Set properties have Text,textcolor,textsize
3. Referencing in a layout file
4. Get custom properties in the constructor
5. Provide a way for the user to set the winning information dynamically
(6) Get the screen density (mode one)
Displaymetrics DM = GetResource (). Getdisplaymetrics ();
float density = dm.density; – The pixel scale (0.75/1.0/1.5/2.0/3.0/4.0) shows the ratio of PX/DP
int densitydpi = dm.densitydpi; – per inch pixel (120/160/240/320/480/640)
int screenwidth = Dm.widthpixels; – Wide Pixel
int screenheight = Dm.heightpixels; – High Pixel
(7) Some methods of Canva
Drawcolor Draw Background Color
DrawText Draw Text
Drawbitmap Drawing Pictures
"Drawbitmap (Bitmap,x,y,paint) – Bring the drawing to the specified coordinates"
"Drawbitma (Bitmap,srcrect,dstrect,paint) – Specifies the area of the diagram whose parameters are four vertices, top left, bottom right."
Drawroundrect Draw rounded rectangles
"Drawroundrect (Rectf,rx,ry,paint) –rx (fillet radius in x direction) ry (fillet radius in y direction)"
(8) Some common properties of paint
Setcolor/setargb Set Paint color
Setalpha Set Drawing transparency
Setantialias Setting Smoothing
Setdither Set Image jitter
SetStyle set the style of a brush
Setstrokewidth set the width of the brush
Setxfermode How to handle when graphics overlap
Setstrokejoin to set the way to combine graphics
Settextsize sets the size of the text,
(9) Some methods of path (connecting n dots in a view to a "path" and then calling Canvas's DrawPath to draw graphics)
Reset Clear Path
MoveTo (X,y) moves the starting point to the x,y coordinate point
LineTo Draw a line
Quadto Drawing Curves
The above is the entire content of this article, you won the lottery? Hope to help everyone's study.