Android uses LinearGradient for font gradient

Source: Internet
Author: User

Android uses LinearGradient for font gradient
LinearGradient is also called linear rendering. LinearGradient implements linear gradient of colors in a region. It has two constructors: public LinearGradient (float x0, float y0, float x1, float y1, int color0, int color1, Shader. tileMode tile) Where x0 indicates the starting point x coordinate of the gradient, y0 indicates the starting point y coordinate of the gradient, and x1 indicates the end point x coordinate of the gradient; the y1 parameter indicates the y coordinate of the end point of the gradient. color0 indicates the start color of the gradient. color1 indicates the end color of the gradient. the tile parameter indicates the tile mode. Shader. tileMode has three options: CLAMP, REPEAT, and MIRROR: CLAMP. If the Renderer is out of the original boundary range, the edge color will be copied to color the area out of the range. The REPEAT function is to repeatedly render the bitmap MIRROR in the form of tile in the horizontal and vertical directions. The role of the MIRROR is to REPEAT the image in the horizontal and vertical directions. rendering bitmap public LinearGradient (float x0, float y0, float x1, float y1, int [] colors, float [] positions, Shader. tileMode tile). The x0 parameter indicates the x coordinate of the gradient start point. The y0 parameter indicates the y coordinate of the gradient start point. The x1 parameter indicates the x coordinate of the gradient end point; the y1 parameter indicates the y coordinate of the end of the gradient. The colors parameter indicates the color array of the gradient. The positions parameter specifies the relative position of the color array. the tile parameter indicates the tile mode. Generally, the positions parameter is set to null, indicating that the color array is evenly distributed in the form of a diagonal line. The following code copies package com directly from the project on git. 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; pr Ivate 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, 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 (m ViewWidth> 0) {mPaint = getPaint (); mLinearGradient = new LinearGradient (-mViewWidth, 0, 0, 0, new int [] {0x33ffffffff, 0 xffffffff, 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) ;}} copy the code in two steps: one is when onSizeChanged () is the size of the code changed, the other one is onDraw (), which is mainly used for animation. First, let's start with the code in onSizeChanged (). In this code, we mainly define LinearGradient: mLinearGradient = new LinearGradient (-mViewWidth, 0, 0, 0, new int [] {0x33ffffff, 0 xffffffff, 0x33ffffff}, new float [] {0, 0.5f, 1 }, shader. tileMode. CLAMP); this code can be understood in this way, it defines a set of gradient values: {0x33ffffff, 0 xffffffff, 0x33ffffffff}, which are respectively in the corresponding 0, 0.5, 1 shows that 0 corresponds to 0x33ffffff color, 0.5 corresponds to 0 xffffffff, 1 corresponds to 0x33ffffff, the initial position of this gradient is outside the mobile phone screen x = (-mViewWidth, 0) at the end of the screen, let's take a look at how the onDraw () method is used to make an animated mTranslate + = mViewWidth/10. It is very simple to indicate the incremental value of each movement if (mTranslate> 2 * mViewWidth) {mTranslate =-mViewWidth ;}

Related Article

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.