还是对现有控件的自定义。
今天是闪动的文字,由于没有gif录制软件,只能这个效果了。
首先需要用到的事android中Pain的Shader渲染器,下面有介绍,通过设置不断变化的LinearGradient,并且带有该属性的Pain对象来绘制文字,onSizeChanged()初始化代码如下:
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (mViewWidth == 0) {
mViewWidth = getMeasuredWidth();
if (mViewWidth > 0) {
mPaint = getPaint();
mLinearGradient = new LinearGradient(
0,
0,
mViewWidth,
0,
new int[]{
Color.BLUE, 0xffffffff,
Color.BLUE},
null,
Shader.TileMode.CLAMP);
mPaint.setShader(mLinearGradient);
mGradientMatrix = new Matrix();
}
}
}
接着在onDraw()中用矩阵的方式来不断平移渐变效果:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mGradientMatrix != null) {
mTranslate += mViewWidth / 5;
if (mTranslate > 2 * mViewWidth) {
mTranslate = -mViewWidth;
}
mGradientMatrix.setTranslate(mTranslate, 0);
mLinearGradient.setLocalMatrix(mGradientMatrix);
postInvalidateDelayed(100);
}
}
可能需要用到的东西:
一.LinearGradient:
光看字面目测看不出来这个类是干什么的,Gradients有梯度的意思,这个类是android的线性渲染,讲的再直白点就是渐变的颜色。
先看下源码:
x0 float: The x-coordinate for the start of the gradient line** 渐变起始点x坐标**
y0 float: The y-coordinate for the start of the gradient line**渐变起始点y坐标**
x1 float: The x-coordinate for the end of the gradient line**渐变结束点x坐标**
y1 float: The y-coordinate for the end of the gradient line**渐变结束点y坐标**
colors int: The colors to be distributed along the gradient line**颜色 的int 数组**
positions float: May be null. The relative positions [0..1] of each corresponding color in the colors array. If this is null, the the colors are distributed evenly along the gradient line.**相对位置的颜色数组,可为null, 若为null,可为null,颜色沿渐变线均匀分布**
tile Shader.TileMode: The Shader tiling mode**渲染器平铺模式**
Shader.TileMode是什么?
还是先上一下源码:
Shader主要是渲染图像以及一些几何图形的类。
Shader有几个直接子类:
BitmapShader : 主要用来渲染图像
LinearGradient :用来进行线性渲染
RadialGradient : 用来进行环形渲染
SweepGradient : 扫描渐变---围绕一个中心点扫描渐变就像电影里那种雷达扫描,用来梯度渲染。
ComposeShader : 组合渲染,可以和其他几个子类组合起来使用。
1 BitmapShader
渲染器着色一个位图作为一个纹理。位图可以重复或设置模式。
[BitmapShader]( https://developer.android.com/reference/android/graphics/BitmapShader.html#BitmapShader(android.graphics.Bitmap, android.graphics.Shader.TileMode, android.graphics.Shader.TileMode))( Bitmap bitmap, Shader.TileMode tileX, Shader.TileMode tileY)
调用这个方法来产生一个画有一个位图的渲染器(Shader)。
bitmap 在渲染器内使用的位图
tileX The tiling mode for x to draw the bitmap in. 在位图上X方向花砖模式
tileY The tiling mode for y to draw the bitmap in. 在位图上Y方向花砖模式
TileMode:(一共有三种)
CLAMP :如果渲染器超出原始边界范围,会复制范围内边缘染色。
REPEAT :横向和纵向的重复渲染器图片,平铺。
MIRROR :横向和纵向的重复渲染器图片,这个和REPEAT重复方式不一样,他是以镜像方式平铺。
马丹夜深了,朕要就寝了,还有Matrix明天再搞。