【自定义View】自动滚动的TextView(跑马灯)——可获取完成一次滚动后的监听

本文介绍了一种基于Android的自定义View组件AutoRollTextViewByRunnable,用于实现文本的自动滚动效果。该组件通过Runnable接口和ObjectAnimator实现平滑滚动,并提供开始、停止滚动的方法及滚动完成监听。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AutoRollTextViewByRunnable类

class AutoRollTextViewByRunnable extends androidx.appcompat.widget.AppCompatTextView implements Runnable {
    String TAG = "MarqueeText";

    private int currentScrollX = 0;// 当前滚动的位置
    private int textWidth;
    private int viewWidth;
    private boolean isMeasure = false;
    private boolean isRoll = false;

    AgainListener againListener;

    //动画效果
    ObjectAnimator animator1;
    ObjectAnimator animator2;

    public AutoRollTextViewByRunnable(Context context) {
        super(context);
        init();
    }

    public AutoRollTextViewByRunnable(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AutoRollTextViewByRunnable(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    void init() {
        setGravity(Gravity.CENTER_VERTICAL);
        setSingleLine();
        setEllipsize(TextUtils.TruncateAt.MARQUEE);
        startScroll();
        addTextChangedListener(textWatcher);

        //动画效果
        animator1 = ObjectAnimator.ofFloat(this, "alpha", 0f);
        animator2 = ObjectAnimator.ofFloat(this, "alpha", 1f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!isMeasure) {
            textWidth = getTextWidth();
            viewWidth = getWidth();
            isMeasure = true;
        }
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            animator1.setDuration(0);
            animator2.setStartDelay(500);
            animator2.setDuration(500);
            animator2.setInterpolator(new LinearInterpolator());
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.playSequentially(animator1, animator2);
            animatorSet.start();
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            textWidth = getTextWidth();
        }
    };

    @Override
    public void run() {
        if (textWidth > viewWidth) {
            //需要滚动
            currentScrollX += 3;
            scrollTo(currentScrollX, 0);
            if (getScrollX() > textWidth) {
                againListener.Again();
                currentScrollX = -viewWidth;
                scrollTo(currentScrollX, 0);
            }
            postDelayed(this, 5);
        } else {
            //无需滚动
            scrollTo(0, 0);
            postDelayed(this, 500);
        }
    }

    public void startScroll() {
        post(this);
    }

    public void stopScroll() {
        this.removeCallbacks(this);
    }

    /**
     * 获取文字宽度
     */
    public int getTextWidth() {
        Paint paint = this.getPaint();
        String str = this.getText().toString();
        return (int) paint.measureText(str);
    }

    public AgainListener getAgainListener() {
        return againListener;
    }

    public void setAgainListener(AgainListener againListener) {
        this.againListener = againListener;
    }

    interface AgainListener {
        void Again();
    }
}

2、使用(xml忽略)

AutoRollTextViewByRunnable marqueeText;
//完成一次滚动的监听
marqueeText.setAgainListener(new AutoRollTextViewByRunnable.AgainListener() {
    @Override
    public void Again() {
        Log.d(TAG, "Again: ");
    }
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值