Android glitter font Effect

Source: Internet
Author: User

Facebook open-source Shimmer, a loading effect tool, enables the font to shine as follows:


The link here is ghost (no object-c, ghost ZR), so I searched Shimmer android. Someone actually did it. Here, https://github.com/romainpiel/shimmer-android. The following is

VcnBtq + release/Y1sajrM/Cw + a + zcC01/release + release = "brush: java;"> 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) ;}@ Overrideprotected 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, 0 xffffffff, 0x33ffffff}, new float [] {0, 0.5f, 1}, Shader. tileMode. CLAMP); mPaint. setShader (mLinearGradient); mGradientMatrix = new Matrix () ;}}@ Overrideprotected 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 );}}}
Inherit a TextView. Because the gradient translation requires the width of the view, each required element is initialized in onSizeChanged, and the color and position of the gradient are defined in linearGradient, then set the paint for the drawing. The onDraw () method controls the calculation of the Offset. Here, the draw time of each frame is set in every 1/10 of the moving width and postInvalidateDelay (), that is, the speed of the flashing is controlled. The effect is as follows:



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.