Android Basics Getting Started tutorial--8.3.8 Paint API--xfermode and Porterduff details (v)
tags (space delimited): Android Basics Getting Started Tutorial
Introduction to this section:
Well, in the previous section, we wrote another example of the Xfermode picture Mix--the demo of wiping beautiful clothes, plus the front
Using Xfermode to achieve rounded corners or round imageview, I believe that Xfermode is no longer as unfamiliar as before, or
Say a little familiar with, well, this section we're going to write the last example of Xfermode, through Xfermode's proterduff. src_in
Mode to achieve the effect of text loading! Still have to put down the Proterduff pattern diagram:
Examples of this section are from: Android paint Setxfermode porterduffxfermode Explanation
Well, don't talk much, start this section ~
1. To achieve and implement the process analysis:
to achieve :
Implement process Analysis :
Step 1. First, a text picture (transparent background)
Step 2. Initialize brush, background picture (DST), Rectangle rect (SRC)
Step 3. Save the layer first, then draw the background map, set the blending mode, then draw the rect, clear the Mixed mode
Then reply to the saved layer, modify the bottom rect area height, call invalidate () to let the view redraw!
If the process analysis a bit do not understand, directly look at the code, super simple ~
2. Code implementation:
First is the screen tool class, Screenutil.java, here is not affixed, the previous sections have been posted!
Then our custom view class:Loadtextview.java:
/** * Created by Jay on 2015/10/26 0026. * * Public class loadtextview extends View { PrivatePorterduffxfermode Mxfermode =NewPorterduffxfermode (PorterDuff.Mode.SRC_IN);PrivateBitmap Backbitmap;PrivatePaint Mpaint;Private intMBITW, Mbith;Private intMCURW, Mcurh, mcurtop;PrivateRect Mdynamicrect; Public Loadtextview(Context context) { This(Context,NULL); } Public Loadtextview(context context, AttributeSet attrs) {Super(context, attrs); MCURW = SCREENUTIL.GETSCREENW (context); Mcurh = screenutil.getscreenh (context); Init (); } Public Loadtextview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); }Private void Init() {//Brush initialization:Mpaint =NewPaint (Paint.anti_alias_flag); Mpaint.setfilterbitmap (true); Mpaint.setdither (true); Mpaint.setcolor (color.red);//initialization of the picture on the backBackbitmap = Bitmapfactory.decoderesource (Getresources (), r.mipmap.img_string); Mbith = Backbitmap.getheight (); MBITW = Backbitmap.getwidth ();//Set the current heightMcurtop = Mbith; Mdynamicrect =NewRect (0, Mbith, MBITW, Mbith);//Initialize original artwork}@Override protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas);intSavelayercount = Canvas.savelayer (0,0, MCURW, Mcurh, Mpaint, Canvas.all_save_flag); Canvas.drawbitmap (Backbitmap,0,0, Mpaint);//Draw the target graphMpaint.setxfermode (Mxfermode);//Set Mixed modeCanvas.drawrect (Mdynamicrect, mpaint);//Draw source MapMpaint.setxfermode (NULL);//Clear Mixed ModeCanvas.restoretocount (Savelayercount);//Restore a saved layer //Change the Rect area ifMcurtop-=2;if(Mcurtop <=0) {mcurtop = Mbith; } mdynamicrect.top = Mcurtop; Invalidate ();// Redraw}}
Well, no, on top of this code, the implementation of the effect, is not very simple.
Want to coder-pig the picture of the font, paste down ~
3. This section of the code sample download:
Xfermodedemo3.zip
This section summarizes:
OK, in this section we also use Porterduff's src_in mode to write a text loading effect, plus the previous:
dst_in mode realizes round and rounded corners ImageView, and dst_out mode to achieve erase beauty clothes, believe
The use of Xfermode has been taken into consideration, of course, these examples are not very significant, the actual development will not
Use, but very easy to understand ~ is like practicing kung Fu, the teacher led the door, practice on their own! The basic tutorial is just a guide
, to really master and learn from the need to rely on your own, read more people's excellent code, and more hands-on!
All right, that's all, thank you.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started tutorial--8.3.8 Paint API--xfermode and Porterduff details (v)