700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 自定义控件 闪动文字FlickeringTextView

自定义控件 闪动文字FlickeringTextView

时间:2021-12-07 05:48:44

相关推荐

自定义控件 闪动文字FlickeringTextView

自定义控件,闪动文字FlickeringTextView

本控件是在TextView的基础上实现文字闪烁的效果,实现方法很简单,使用Paint对象的Shader渲染器,创建一个LinearGradient(线性渐变渲染,继承自Shader)对象,将该渲染器设置给用于绘制文字的Paint对象即可。

代码

import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.LinearGradient;import android.graphics.Matrix;import android.graphics.Shader;import android.text.TextPaint;import android.util.AttributeSet;import android.util.Log;import android.widget.TextView;/*** 闪烁的TextView* Created by Wood on /7/5.*/public class FlickeringTextView extends TextView {private static final String LOG_TAG = "FlickeringTextView";private int mViewWidth;private TextPaint mPaint;private LinearGradient mLinearGradient;private Matrix mGradientMatrix;private int mTranslate;private int textColor = 0xFFFFFFFF;//文字颜色private int flickeringColor = 0xFF000000;//闪烁颜色private static final int[] ATTRS = new int[]{android.R.attr.textColor,};public FlickeringTextView(Context context) {this(context, null);}public FlickeringTextView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public FlickeringTextView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);//先获取系统属性,文字颜色TypedArray a = context.obtainStyledAttributes(attrs, ATTRS);textColor = a.getColor(0, textColor);a.recycle();//再获取自定义属性,闪烁颜色a = context.obtainStyledAttributes(attrs, R.styleable.FlickeringTextView);flickeringColor = a.getColor(R.styleable.FlickeringTextView_flickeringColor, flickeringColor);a.recycle();Log.e(LOG_TAG, "textColor:" + textColor + ";flickeringColor:" + flickeringColor);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if (mGradientMatrix != null) {mTranslate += mViewWidth / 5;if (mTranslate > mViewWidth * 2) {mTranslate = -mViewWidth;}mGradientMatrix.setTranslate(mTranslate, 0);mLinearGradient.setLocalMatrix(mGradientMatrix);postInvalidateDelayed(100);}}@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(0,//渐变起初点坐标x位置0,//渐变起初点坐标y位置mViewWidth,//渐变终点0,//渐变终点new int[]{textColor, flickeringColor, textColor},//参与渐变效果的颜色集合null,//每个颜色处于的渐变相对位置Shader.TileMode.CLAMP//平铺方式);mPaint.setShader(mLinearGradient);mGradientMatrix = new Matrix();}}}}

属性获取了自带的颜色属性,又添加了一个自定义的闪动颜色attr.xml

<!-- FlickeringTextView --><declare-styleable name="FlickeringTextView"><attr name="flickeringColor" format="color" /></declare-styleable>

示例

<com.example.wood.samplevideo.FlickeringTextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:gravity="center"android:text="夜空中最亮的星,\n 请照亮我前行。"android:textColor="#303F9F"android:textSize="35sp"app:flickeringColor="#FF4081" />

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。