Android uses lineargradient for font gradients, as shown in the following illustration:
As shown above, a white light flashed, this effect is mainly using the LinearGradient class to carry out the
LinearGradient is also called linear rendering, and lineargradient is the effect of achieving a linear gradient of colors within a region.
It has two constructors
The code is as follows |
Copy Code |
Public lineargradient (float x0, float y0, float x1, float y1, int color0, int color1, Shader.tilemode tile) |
Where the parameter x0 represents the starting point x coordinate of the gradient, the parameter y0 represents the starting point Y coordinate of the gradient, the parameter X1 represents the endpoint x coordinate of the gradient, the parameter Y1 represents the end Y coordinate of the gradient, the color0 indicates the gradient start color, and the color1 indicates the gradient end color;
Shader.tilemode has 3 parameters to choose from, namely clamp, repeat and Mirror:
The clamp effect is that if the renderer exceeds the original bounds, the edge color is copied to the area out of range.
The role of repeat is to repeatedly render bitmaps in a tiled form, both horizontally and vertically
The role of mirror is to repeatedly render bitmaps in a mirrored manner, both horizontally and vertically.
Public lineargradient (float x0, float y0, float x1, float y1, int[] colors, float[] positions, shader.tilemode tile);
Where the parameter x0 represents the starting point x coordinate of the gradient, the parameter y0 represents the starting point Y coordinate of the gradient, the parameter X1 represents the endpoint x coordinate of the gradient, the parameter Y1 represents the end Y coordinate of the gradient, and the parameter colors represents the gradient's array of colors; The parameter tile represents the tile method. Typically, the parameter positions is set to NULL, indicating that the array of colors is evenly distributed in the form of a slope line.
The following code is copied directly from the project on Git.
The code is as follows |
Copy Code |
Package Com.example.shimmer; Import Android.content.Context; Import Android.graphics.Canvas; Import android.graphics.LinearGradient; Import Android.graphics.Matrix; Import Android.graphics.Paint; Import Android.graphics.Shader; Import Android.util.AttributeSet; Import Android.widget.TextView; public class Mytextview extends TextView { Private LinearGradient mlineargradient; Private Matrix Mgradientmatrix; Private Paint Mpaint; private int mviewwidth = 0; private int mtranslate = 0; Private Boolean manimating = true; Public Mytextview (context context, AttributeSet Attrs) { Super (context, attrs); } @Override protected void onsizechanged (int w, int h, int oldw, int oldh) { Super.onsizechanged (W, H, OLDW, OLDH); if (Mviewwidth = = 0) { Mviewwidth = Getmeasuredwidth (); if (Mviewwidth > 0) { Mpaint = Getpaint (); Mlineargradient = new LinearGradient (-mviewwidth, 0, 0, 0, New int[] {0x33ffffff, 0xFFFFFFFF, 0x33ffffff}, New float[] {0, 0.5f, 1}, Shader.TileMode.CLAMP); Mpaint.setshader (mlineargradient); Mgradientmatrix = new Matrix (); } } } @Override protected void OnDraw (Canvas Canvas) { Super.ondraw (canvas); if (manimating && mgradientmatrix!= null) { Mtranslate + = MVIEWWIDTH/10; if (Mtranslate > 2 * mviewwidth) { Mtranslate =-mviewwidth; } Mgradientmatrix.settranslate (mtranslate, 0); Mlineargradient.setlocalmatrix (Mgradientmatrix); Postinvalidatedelayed (50); } } } |
This code is mainly in two steps: One is in the onsizechanged () that is, the size of the change, the other is OnDraw () is mainly used to do the effect of animation,
First we're going to onsizechanged () the code in the code, which basically defines the lineargradient:
The code is as follows |
Copy Code |
Mlineargradient = new LinearGradient (-mviewwidth, 0, 0, 0, new int[] {0x33ffffff, 0xFFFFFFFF, 0x33ffffff},new float[] { 0, 0.5f, 1}, Shader.TileMode.CLAMP); |
This code can be so understood that it defines a set of gradient values of {0x33ffffff, 0xFFFFFFFF, 0X33FFFFFF}, which are respectively in the corresponding 0,0. 5,1 Display, 0 position corresponds to 0X33FFFFFF color, 0. The 5 position corresponds to the 0xffffffff,1 position corresponding to the 0X33FFFFFF, the initial position of this gradient is on the outside of the phone screen x= (-mviewwidth,0) is outside the screen
Finally, let's take a look at how this OnDraw () method is animated.
The code is as follows |
Copy Code |
Mtranslate + = MVIEWWIDTH/10; Simple to indicate the increment value of each movement
if (Mtranslate > 2 * mviewwidth) { Mtranslate =-mviewwidth; } |
So this is the end of the movement, and we'll put it in the following figure.
I compare lineargradient this to a rectangle, such as the image above is initialized in the cell phone screen on the leftmost, to move to the far right of the screen requires the length of 2*width.
The rest of the way is very well understood, and here no longer explains