如何自定义一个带有首尾间距得跑马灯效果
作者:互联网
定义跑马灯效果我们使用Textview,根据画布得高宽和文字得高宽来设置跑马灯效果
直接上代码
public class HorizontalTextview extends AppCompatTextView {
private float textLength = 0f;
private float drawTextX = 0f;// 文本的横坐标
public boolean isStarting = false;// 是否开始滚动
private Paint paint = null;
private String text = "";
private long waitTime = 1000; //开始时等待的时间
private int scrollTile = 2; //文字的滚动速度
private int baseline;
private float baselinex;
private float drawTextx2;
private float distance=160;//间距
public HorizontalTextview(Context context) {
super(context);
initView(context);
}
public HorizontalTextview(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public HorizontalTextview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
@Override
public void invalidate() {
super.invalidate();
}
private void initView(Context context) {
setMaxWidth(context.getResources().getDisplayMetrics().widthPixels / 2); //因为需求需要所以设置了最大宽度,如果不需要此功能可以删除掉
paint = getPaint();
paint.setColor(getTextColors().getColorForState(getDrawableState(), 0));
text = getText().toString();
if (TextUtils.isEmpty(text)) {
return;
}
textLength = paint.measureText(text);
isStarting = true;
}
@Override
public void setTextColor(int color) {
super.setTextColor(color);
paint.setColor(color);
start();
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
this.text = text.toString();
this.textLength = getPaint().measureText(text.toString());
drawTextX = 0;//第一次跑马灯
drawTextx2=-textLength-distance;//第二次跑马灯
start();
}
public void start() {
isStarting = true;
invalidate();
}
@Override
public void setGravity(int gravity) {
super.setGravity(gravity);
}
public void stop() {
isStarting = false;
invalidate();
}
@Override
public void onDraw(Canvas canvas) {
final Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
baseline = (canvas.getHeight() - fontMetrics.bottom - fontMetrics.top) / 2;
if (textLength <= canvas.getWidth()) {
baselinex=canvas.getWidth()/2-textLength/2;
canvas.drawText(text, baselinex, baseline, paint);
return;
}
canvas.drawText(text, -drawTextX, baseline, paint);
canvas.drawText(text, -drawTextx2, baseline, paint);
if (!isStarting) {
return;
}
if (drawTextX == 0) {
postDelayed(() -> {
drawTextX = 1;
drawTextx2= -textLength-distance;
isStarting = true;
invalidate();
}, waitTime);
isStarting = false;
return;
}
drawTextX += scrollTile;
drawTextx2+=scrollTile;
//判断是否滚动结束
if (drawTextX > textLength) {
// drawTextX = -canvas.getWidth()-30;
drawTextX=-textLength-2*distance;
drawTextx2=-distance;
}
invalidate();
}
}
在xml直接引用即可
<com.example.xmltest.HorizontalTextview
android:id="@+id/roll_text"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignRight="@id/text_img"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/text_img"
android:text="1111" />
到这里简单得有首尾间距得跑马灯效果实现,谢谢支持!
标签:自定义,textLength,text,void,private,跑马灯,context,public,首尾 来源: https://blog.csdn.net/weixin_40138348/article/details/111604111