3 Star 3 Fork 1

zhang dongling / Android_Util_Class

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LinearGradientView.java 3.49 KB
一键复制 编辑 原始数据 按行查看 历史
zhang dongling 提交于 2016-09-06 11:30 . 文本渐变字体
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.TextView;
/**
*文本渐变字体
*/
public class LinearGradientView extends TextView {
public LinearGradientView(Context context) {
super(context);
}
boolean aBoolean;
public LinearGradientView(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZdyTest, 0, 0);
aBoolean = a.getBoolean(R.styleable.ZdyTest_is_select, false);
}
}
@Override
protected void onDraw(Canvas canvas) {
mGradientMatrix.setTranslate(mTranslate, 0);
mLinearGradient.setLocalMatrix(mGradientMatrix);
super.onDraw(canvas);
}
//白色左推红色
public void setTranslate3(float ds) {
mTranslate = -mViewWidth * ds + mViewWidth * 0.5f;
if (mTranslate >= mViewHeight * 1.5f)
mTranslate = mViewHeight * 1.5f;
postInvalidate();
}
//红色左推白色
public void setTranslate2(float ds) {
mTranslate = -mViewWidth * ds + mViewWidth * 1.5f;
if (mTranslate >= mViewHeight * 1.5f)
mTranslate = mViewHeight * 1.5f;
postInvalidate();
}
//白色右推红色
public void setTranslate(float ds) {
mTranslate = mViewWidth * ds + mViewWidth / 2;
if (mTranslate >= mViewHeight * 1.5f)
mTranslate = mViewHeight * 1.5f;
postInvalidate();
}
//红色右推白色
public void setTranslate1(float ds) {
mTranslate = mViewWidth * ds - mViewHeight * 0.5f;
if (mTranslate <= -mViewHeight * 0.5f)
mTranslate = -mViewHeight * 0.5f;
postInvalidate();
}
public void setSelect(boolean isSelect) {
if (!isSelect) {
mTranslate = mViewWidth / 2;
} else {
mTranslate = -mViewWidth / 2;
}
postInvalidate();
}
private LinearGradient mLinearGradient;
private Matrix mGradientMatrix;
private Paint mPaint;
private int mViewWidth = 0;
private float mTranslate = 0;
private int mViewHeight = 0;
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (mViewWidth == 0 || mViewHeight == 0) {
mViewWidth = getMeasuredWidth();
mViewHeight = getMeasuredWidth();
if (mViewWidth > 0) {
mPaint = getPaint();
mLinearGradient = new LinearGradient(0, mViewHeight / 2, mViewWidth, mViewHeight / 2,
new int[]{Color.RED, Color.WHITE},
new float[]{0.5f, 0.5f}, Shader.TileMode.MIRROR);
mPaint.setShader(mLinearGradient);
mGradientMatrix = new Matrix();
if (aBoolean) {
mTranslate = -mViewWidth / 2;
} else {
mTranslate = mViewWidth / 2;
}
mGradientMatrix.setTranslate(mTranslate, 0);
mLinearGradient.setLocalMatrix(mGradientMatrix);
}
}
}
}
Android
1
https://gitee.com/jason/Android_Util_Class.git
git@gitee.com:jason/Android_Util_Class.git
jason
Android_Util_Class
Android_Util_Class
master

搜索帮助