先看看效果效果图
仔细对比之后发现和今日头条的有点不一样,
第一字体是向右倾斜的
//设置字体向右倾斜
paint.setTextSkewX(0.25f);
更改后的
<com.example.administrator.meterialdesignpaintdemo.GridentText
android:text="你关心的才是头条"
android:textSize="40sp"
android:textColor="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="wrap_content" />"
public class GridentText extends AppCompatTextView {
private LinearGradient gradient;
private float charwidth;
private Matrix matric;
private float totalWidth;
public GridentText(Context context) {
super(context);
}
public GridentText(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TextPaint paint = getPaint();
//计算每一个字符的宽度
CharSequence text = getText();
totalWidth = paint.measureText(text.toString());
charwidth = totalWidth / text.length();
int[] color = new int[]{Color.GRAY, Color.WHITE, Color.GRAY};
gradient = new LinearGradient(0, 0, charwidth*2, charwidth, color, null, Shader.TileMode.CLAMP);
paint.setShader(gradient);
//设置字体向右倾斜
paint.setTextSkewX(0.25f);
matric = new Matrix();
}
float offset = 0;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//这里不断移动matrix的位置
if (offset <= totalWidth) {
matric.setTranslate(offset, 0);
gradient.setLocalMatrix(matric);
postInvalidate();
offset += 10;
}
}
}
。面用到了三阶矩阵Matrix,矩阵变换的功能很强大,比如再里面添加缩放,旋转等,待以后探索。