标签:
使用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:text="普通的TextView"android:textColor="#00f"android:textSize="30sp" /><com.bqt.myview.MyShimmerTextViewandroid:id="@+id/shimmer_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:background="#000"android:gravity="center"android:text="闪烁效果的TextView"android:textColor="#00f"android:textSize="30sp" /></LinearLayout>
代码
public class MyShimmerTextView extends TextView {/**渲染器,用于显示本例中的颜色效果*/private LinearGradient mLinearGradient;/**矩阵,用于确定渲染范围*/private Matrix mGradientMatrix;/**渲染的起始位置*/private int mViewWidth = 0;/**渲染的终止距离*/private int mTranslate = 0;/**是否启动动画*/private boolean mAnimating = true;/**多少毫秒刷新一次*/private int speed = 50;private Paint mPaint;public MyShimmerTextView(Context context, AttributeSet attrs) {super(context, attrs);mPaint = getPaint();mGradientMatrix = new Matrix();}@SuppressLint("DrawAllocation")@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);mViewWidth = getMeasuredWidth();//可以尝试一下,使用不同的模式可以得到不同的效果mLinearGradient = new LinearGradient(0, 0, mViewWidth, 0, new int[] { Color.BLACK, Color.WHITE, Color.BLACK }, null, TileMode.CLAMP);mPaint.setShader(mLinearGradient);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if (mAnimating && mGradientMatrix != null) {mTranslate += mViewWidth / 10;//每次移动屏幕的1/10宽if (mTranslate > 2 * mViewWidth) {mTranslate = -mViewWidth;}mGradientMatrix.setTranslate(mTranslate, 0);mLinearGradient.setLocalMatrix(mGradientMatrix);//在指定矩阵上渲染postInvalidateDelayed(speed);}}}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);Log.i("bqt", w + "--" + h);}
标签:
原文地址:http://www.cnblogs.com/baiqiantao/p/5468958.html