Android uses lineargradient for font gradient effects

Source: Internet
Author: User

There is such an effect that a string of characters has a glow of white light from the top of the font. As shown:

As shown above the effect of a bunch of white light flashed, this effect is mainly used in the LinearGradient class to do

lineargradient is also called linear rendering, The function of LinearGradient is to realize the linear gradient effect of color in an area.

It has two constructors

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 end point x coordinate of the gradient, the parameter Y1 represents the end point Y coordinate of the gradient, andcolor0 represents the gradient start color;color1 Represents the gradient end color, and the parameter tile represents the tiling method.

The Shader.tilemode has 3 parameters to choose from, clamp, repeat, and mirror, respectively:

The effect of clamp is that if the renderer exceeds the original boundary range, the edge color is copied to render 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 end point x coordinate of the gradient, the parameter Y1 represents the end point Y coordinate of the gradient, the parameter colors the color array for the gradient, and the parameter positions to specify the relative position of the color array ; The parameter tile represents the tiling method. Typically, the parameter positions is set to NULL, which indicates that the color array is evenly distributed as a ramp line.

The following code is copied directly from the project on Git.

 PackageCom.example.shimmer;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;Importandroid.graphics.LinearGradient;ImportAndroid.graphics.Matrix;ImportAndroid.graphics.Paint;ImportAndroid.graphics.Shader;ImportAndroid.util.AttributeSet;ImportAndroid.widget.TextView; Public classMytextviewextendsTextView {Privatelineargradient mlineargradient; PrivateMatrix Mgradientmatrix; PrivatePaint Mpaint; Private intMviewwidth = 0; Private intMtranslate = 0; Private BooleanManimating =true;  PublicMytextview (Context context, AttributeSet attrs) {Super(context, attrs); } @Overrideprotected voidOnsizechanged (intWintHintOLDW,intOLDH) {        Super. Onsizechanged (W, H, OLDW, OLDH); if(Mviewwidth = = 0) {Mviewwidth=getmeasuredwidth (); if(Mviewwidth > 0) {Mpaint=Getpaint (); Mlineargradient=NewLinearGradient (-mviewwidth, 0, 0, 0,                        New int[] {0x33ffffff, 0xFFFFFFFF, 0X33FFFFFF },                        New float[] {0, 0.5f, 1}, Shader.TileMode.CLAMP);                Mpaint.setshader (mlineargradient); Mgradientmatrix=NewMatrix (); } }} @Overrideprotected voidOnDraw (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 onsizechanged () that is the size of the change, the other is the OnDraw () is mainly used to animate the effect,

First, let's start with the code inside the onsizechanged () , which in this code is primarily defined by LinearGradient:

      New LinearGradient (-mviewwidth, 0, 0, 0newint[] {0x33ffffff, 0xFFFFFFFF, 0x33ffffff },  newfloat[] {0, 0.5f, 1}, Shader.TileMode.CLAMP);


This code can be understood, it defines a set of values of the gradient is {0x33ffffff, 0xFFFFFFFF, 0x33ffffff}, this set of values respectively in the corresponding 0,0. 5,1 Display, 0 position corresponding 0x33ffffff color , 0. 5 position corresponds to 0xffffffff, 1 position corresponds to 0x33ffffff, the initial position of this gradient is on the outside of the screen of the phone x= (-mviewwidth,0) is outside the screen

Finally, take a look at how the OnDraw () method is animated.

Mtranslate + = MVIEWWIDTH/10; it is simple to indicate the increment value of each movement.

            if (Mtranslate > 2 * mviewwidth) {                =-mviewwidth;            }

This is the end point of the movement, we put the above remark as

I will compare lineargradient to a rectangle, as the location of the initial position on the left of the mobile screen, to move to the far right of the screen requires 2*width length.

The rest of the way is well understood, it's no longer explained

Android uses lineargradient for font gradient effects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.