android 横向滚动文字的实现


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Display;
import android.view.WindowManager;
import android.widget.TextView;

@SuppressLint("AppCompatCustomView")
public class MarqueeTextView 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;

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

    /**
     * 初始化自动滚动条,每次改变文字内容时,都需要重新初始化一次
     *
     * @param windowManager 获取屏幕
     * @param text          显示的内容
     * @param su            文字滚动的速度
     */
    public void initScrollTextView(WindowManager windowManager, String text, float su) {
        // 得到画笔,获取父类的textPaint
        paint = this.getPaint();
        // 得到文字
        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 + textLength * 2;// 自己定义,文字变化多少
        // // 文字的大小+距顶部的距离
        ty = this.getTextSize() + this.getPaddingTop();
    }

    /**
     * 开始滚动
     */
    public void starScroll() {
        // 开始滚动
        isStarting = true;
        this.invalidate();// 刷新屏幕
    }

    /**
     * 停止方法,停止滚动
     */
    public void stopScroll() {
        // 停止滚动
        isStarting = false;
        this.invalidate();// 刷新屏幕
    }

    /**
     * 重写onDraw方法
     */
    @Override
    protected void onDraw(Canvas canvas) {
        if (isStarting) {
            // A-Alpha透明度/R-Read红色/g-Green绿色/b-Blue蓝色
            //paint.setARGB(255, 200, 200, 200);
            canvas.drawText(text, temp_tx1 - tx, ty, paint);
            tx += sudu;
            // 当文字滚动到屏幕的最左边
            if (tx > temp_tx2) {
                // 把文字设置到最右边开始
                tx = textLength;
            }
            this.invalidate();// 刷新屏幕
        }
        super.onDraw(canvas);
    }
}

 

使用:

添加布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="youli.com.example.administrator.paomadeng.MainActivity">

    <youli.com.example.administrator.paomadeng.MarqueeTextView
        android:id="@+id/five_text__view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
     />

</LinearLayout>

代码中使用:

        MarqueeTextView marqueeTextView = findViewById(R.id.five_text__view);
        //参数 3 是滚动速度
        marqueeTextView.initScrollTextView(getWindowManager(), "今天星期五_滚动字", 2);
        marqueeTextView.setText("显示文字");
        marqueeTextView.starScroll();

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值