This article shares the specific code of Android neon flashing text effect for your reference. The specific content is as follows
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Created by apple on 2017/5/10. */ public class LinearGradientTextView extends TextView { private TextPaint mPaint; private LinearGradient mLinearGradient ; private Matrix mMatrix; private float mTranslate; private float DELTAX = 20; private int mGradientSize; public LinearGradientTextView(Context context) { super(context); } public LinearGradientTextView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } /** * Priority is given to onDraw execution, here we get the brush that system draws TextView, and then set a shader for this brush * In this way, when executing onDraw, you use a brush with a Shader effect. * @param w * @param h * @param oldw * @param oldh */ @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { (w, h, oldw, oldh); // ★Get the brush of the system TextView mPaint = getPaint(); // Get text String text = getText().toString(); // Use a brush to measure the length of the text float textWith = (text); // Width of 3 text mGradientSize = (int) (textWith / () * 3); // Start from the left-gradientSize, that is, the left-hand distance from the text gradientSize starts to change. // A gradient unit is: start from the distance between 3 characters before the first character and end before the first character //Then keep moving the gradient unit from left to right dx displacement until the rightmost side and move to the left // CLAMP means: Since you are only changing the length of 3 characters in the gradient, then the remaining space is filled with the edge color mLinearGradient = new LinearGradient(-mGradientSize,0,0,0,new int[]{ 0xffff0000, 0xff00ff00, 0xff00ffff},null, ); (mLinearGradient); } @Override protected void onDraw(Canvas canvas) { // Call super, which means to complete the textview drawing first according to the process of drawing the textview in the system (canvas); // I will draw some effects based on the system mTranslate += DELTAX; float textWidth = getPaint().measureText(getText().toString()); // When it reaches the boundary dx, reverse if(mTranslate > textWidth + mGradientSize || mTranslate < 0){ DELTAX = - DELTAX; } // Continuously translate to get a flashing effect mMatrix = new Matrix(); (mTranslate, 0); (mMatrix); // Delay repaint postInvalidateDelayed(50); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.