您的位置:首页 > 移动开发 > Android开发

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
。欢迎有志做移动开发的同行加入!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: