Android 自定义跑马灯文字

Android 跑马灯效果文字:

效果图(真实动画很流畅,这个转gif有问题,感觉有点卡):

 

代码:

/**
 * Created by wuguangliang on 2018/12/21
 *
 * 跑马灯效果文字
 */
public class MarqueeHorizontalTextView 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;

    public MarqueeHorizontalTextView(Context context) {
        super(context);
        initView(context);
    }

    public MarqueeHorizontalTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MarqueeHorizontalTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    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;
        start();
    }

    public void start() {
        isStarting = true;
        invalidate();
    }

    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()) {
            canvas.drawText(text, 0, baseline, paint);
            return;
        }
        canvas.drawText(text, -drawTextX, baseline, paint);

        if (!isStarting) {
            return;
        }
        if (drawTextX == 0) {
            postDelayed(() -> {
                drawTextX = 1;
                isStarting = true;
                invalidate();
            }, waitTime);
            isStarting = false;
            return;
        }
        drawTextX += scrollTile;
        //判断是否滚动结束
        if (drawTextX > textLength) {
            drawTextX = -canvas.getWidth();

        }
        invalidate();
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值