android textView特效
2014-05-06 13:46
519 查看
/** * @Title: TwinkleTextView.java */ package com.zero.view; 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; /** * @ClassName: TwinkleTextView * @Description: 自定义移动闪光textview * @author ZeRo_Ci * @date 2014-5-6 下午12:54:46 * */ public class TwinkleTextView extends TextView { private LinearGradient mLinearGradient; private Matrix mMatrix; private Paint mPaint; private int mViewWidth = 0; private int mTranslate = 0; private boolean mAnimating = true; /** * @Description: 自定义移动闪光textview * @author ZeRo_Ci * @date 2014-5-6 下午12:55:05 */ public TwinkleTextView(Context context, AttributeSet attributeSet) { super(context, attributeSet); } protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); if (mViewWidth == 0) { // 获取textView的宽度 mViewWidth = getMeasuredWidth(); if (mViewWidth > 0) { mPaint = getPaint(); // ↓↓↓关于LinearGradient的使用参考一下博客↓↓↓: // http://www.apihome.cn/api/android/LinearGradient.html // http://blog.csdn.net/q445697127/article/details/7865504 mLinearGradient = new LinearGradient(-mViewWidth, 0, 0, 0, new int[] { 0x33ffffff, 0xffffffff, 0x33ffffff }, new float[] { 0, 0.5f, 1 }, Shader.TileMode.CLAMP); // 设置对象 mPaint.setShader(mLinearGradient); mMatrix = new Matrix(); } } } protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mAnimating && mMatrix != null) { mTranslate += mViewWidth / 10; if (mTranslate > 2 * mViewWidth) { mTranslate = -mViewWidth; } // 设置一个矩阵 mMatrix.setTranslate(mTranslate, 0); mLinearGradient.setLocalMatrix(mMatrix); // 设置循环并指定时间 postInvalidateDelayed(50); } } } // Shader类源码 /** * Shader is the based class for objects that return horizontal spans of colors * during drawing. A subclass of Shader is installed in a Paint calling * paint.setShader(shader). After that any object (other than a bitmap) that is * drawn with that paint will get its color(s) from the shader. */ // public abstract class Shader { // // private final Matrix mMatrix = new Matrix(); // // public enum TileMode { // /** // * replicate the edge color if the shader draws outside of its // * original bounds // */ // CLAMP(0), // /** // * repeat the shader's image horizontally and vertically // */ // REPEAT(1), // /** // * repeat the shader's image horizontally and vertically, // * alternating mirror images so that adjacent images always seam // */ // MIRROR(2); // // TileMode(int nativeInt) { // this.nativeInt = nativeInt; // } // // final int nativeInt; // } // // /** // * Return true if the shader has a non-identity local matrix. // * // * @param localM // * If not null, it is set to the shader's local matrix. // * @return true if the shader has a non-identity local matrix // */ // public boolean getLocalMatrix(Matrix localM) { // if (localM != null) { // localM.set(mMatrix); // } // // return !mMatrix.isIdentity(); // } // // /** // * Set the shader's local matrix. Passing null will reset the shader's // * matrix to identity // * // * @param localM // * The shader's new local matrix, or null to specify identity // */ // public void setLocalMatrix(Matrix localM) { // if (localM != null) { // mMatrix.set(localM); // } else { // mMatrix.reset(); // } // } // // /** // * Returns a java.awt.Paint object matching this shader. // */ // abstract java.awt.Paint getJavaPaint(); // }
源码下载:
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
android textview特效
安卓开发、ios开发、QT移动开发技术交流学习群 347131054。欢迎有志做移动开发的同行加入!
相关文章推荐
- 玩转Android 之 TextView常用特效
- Android 适用于QA页面的textView 伸缩动画特效
- android常用控件应用之文本框(TextView)特效
- Android之TextView的跑马灯特效
- Android TextView 限制行数并添加滚动条
- Android用AutoCompleteTextView实现搜索历史记录提示
- Android TextView学习
- 自定义android控件——TextView 并添…
- android获取textview的行数
- Android TextView : “Do not concatenate text displayed with setText”
- android中TextView设置连接及关闭软键盘
- android 动态添加TextView 设置margin
- android中TextView分段显示不同颜色,字体,时间
- <TextView>标签中必须加android:layout_height和android:layout_width属性
- Android系列教程之TextView小组件的使用--附带超链接和跑马灯效果
- 关于Android使TextView可以滚动的设置
- Android开发学习笔记:TextView的属性详解
- android TextView 垂直自动滚动字幕实现
- android-修改TextView中部分文字的颜色
- Android TextView 实现跑马灯效果