废话不多说,直接上代码:
public class FiveTextView extends TextView {
/** 文字长度 */
private float textLength = 0f;
/** 滚动条长度 */
private float viewWidth = 0f;
/** 文本x轴 的坐标 */
private float tx = 0f;
/** 文本Y轴的坐标 */
private float ty = 0f;
/** 文本当前长度 */
private float temp_tx1 = 0.0f;
/** 文本当前变换的长度 */
private float temp_tx2 = 0x0f;
/** 文本滚动开关 */
private boolean isStarting = false;
/** 画笔对象 */
private Paint paint = null;
/** 显示的文字 */
private String text = "";
/** 文本滚动速度 **/
private float sudu;
private Paint strokPaint = null;
private int strokeWidth;
private int a;
private int r;
private int g;
private int b;
private int sa;
private int sr;
private int sg;
private int sb;
public FiveTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 初始化自动滚动条,每次改变文字内容时,都需要重新初始化一次
*
* 显示的内容
* @param
*
*/
private void init() {
strokPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
//设置抗锯齿
strokPaint.setAntiAlias(true);
//设置防抖动
strokPaint.setDither(true);
//设置填充方式
strokPaint.setStyle(Paint.Style.FILL_AND_STROKE);
}
public void initScrollTextView(WindowManager windowManager, String text,
float su,int a,int r,int g,int b,int height,String strokewidth,String strokecolor,int textsize) {
// 得到画笔,获取父类的textPaint
paint = this.getPaint();
init();
if (!"".equals(strokecolor)){
String substring = strokecolor.substring(strokecolor.indexOf("(")+1, strokecolor.indexOf(")"));
String trim = substring.replaceAll("\\s+","");
String[] split1 = trim.split(",");
this.sa = 255;
this.sr = Integer.parseInt(split1[0]);
this.sg = Integer.parseInt(split1[1]);
this.sb = Integer.parseInt(split1[2]);
}
this.a = a;
this.r = r;
this.g = g;
this.b = b;
if (!"".equals(strokewidth)){
this.strokeWidth = Integer.parseInt(strokewidth);
}
strokPaint.setTextSize(textsize);
// 得到文字
this.text = text;
this.sudu = su;
textLength = paint.measureText(text);// 获得当前文本字符串长度
viewWidth = this.getWidth();// 获取宽度return mRight - mLeft;
if (viewWidth == 0) {
// 获取当前屏幕的属性
Display display = windowManager.getDefaultDisplay();
viewWidth = display.getWidth();// 获取屏幕宽度 viewWidth 是滚动的开始位置,需要修改的
// 可再此入手
}
tx = textLength;
temp_tx1 = viewWidth + textLength;
temp_tx2 = viewWidth + new Double(textLength*2.8).intValue();// 自己定义,文字变化多少
ty = height/2+this.getTextSize()/2;
starScroll();
}
/**
* 开始滚动
*/
public void starScroll() {
// 开始滚动
isStarting = true;
this.invalidate();// 刷新屏幕
}
/**
* 停止方法,停止滚动
*/
public void stopScroll() {
// 停止滚动
isStarting = false;
this.invalidate();// 刷新屏幕
}
/** 重写onDraw方法 */
@Override
protected void onDraw(Canvas canvas) {
if (isStarting) {
strokPaint.setTextAlign(Paint.Align.CENTER);
strokPaint.setStrokeWidth(strokeWidth);
strokPaint.setARGB(sa,sr,sg,sb);
canvas.drawText(text, temp_tx1 - tx, ty, strokPaint);
// A-Alpha透明度/R-Read红色/g-Green绿色/b-Blue蓝色
strokPaint.setARGB(a,r,g,b);
strokPaint.setStrokeWidth(0);
canvas.drawText(text, temp_tx1 - tx, ty, strokPaint);
tx += sudu;
// 当文字滚动到屏幕的最左边
if (tx > temp_tx2) {
// 把文字设置到最右边开始
tx = textLength;
}
this.invalidate();// 刷新屏幕
}
// super.onDraw(canvas);
}
}
在activity中使用:
FiveTextView fiveTextView = new FiveTextView(MainActivity.this, null); fiveTextView.initScrollTextView(getWindowManager(),"会移动的描边字幕",3,255,98,0,238,350,"2","rgba(255,13,13,1)",30); mParams.x = 0; mParams.y = 0; mParams.width = 1000; mParams.height =350; wManager.addView(fiveTextView,mParams);
附加:
windowmanager初始化方法:
private void initWindowManager() {
wManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
mParams = new WindowManager.LayoutParams();
mParams.gravity = Gravity.TOP | Gravity.LEFT;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}else {
mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
mParams.format = PixelFormat.TRANSLUCENT;
mParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
}
完事,收工!