简单实现跑马灯

1.//可以自己实现自定义view


@SuppressLint({"ViewConstructor", "AppCompatCustomView"})
public class MarqueeTestView extends TextView implements Runnable {
    private static final String TAG = "MarqueeTextView";
    // 设置跑马灯重复的次数,次数
    private int circleTimes = 1000;
    //记录已经重复了多少遍
    private int hasCircled = 0;
    private int currentScrollPos = 0;
    // 跑马灯走一遍需要的时间(秒数)
    private int circleSpeed = 10;
    // 文字的宽度
    private int textWidth = 0;

    private boolean isMeasured = false;
    // Handler机制
    private Handler handler;
    private boolean flag = false;

    // 构造方法
    public MarqueeTestView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.removeCallbacks(this);
        setFocusableInTouchMode(true);
        post(this);
    }
    /**
     * 画笔工具
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (!isMeasured) {
            getTextWidth();
            isMeasured = true;
        }
    }

    @Override
    public void setVisibility(int visibility) {
        // 二次进入时初始化成员变量
        flag = false;
        isMeasured = false;
        this.hasCircled = 0;
        super.setVisibility(visibility);
    }

    @Override
    public void run() {
        // 起始滚动位置
        currentScrollPos += 1;
        scrollTo(currentScrollPos, 0);
        // Log.i(TAG, "pos"+currentScrollPos);
        // 判断滚动一次
        if (currentScrollPos >= textWidth) {
            // 从屏幕右侧开始出现
            currentScrollPos = -this.getWidth();
            //记录的滚动次数大设定的次数代表滚动完成,这个控件就可以隐藏了
            if (hasCircled >= this.circleTimes) {
                this.setVisibility(View.GONE);
                flag = true;
            }
            hasCircled += 1;
        }

        if (!flag) {
            // 滚动时间间隔
            postDelayed(this, circleSpeed);
        }
    }

    /**
     * 获取文本显示长度
     */

    private void getTextWidth() {
        Paint paint = this.getPaint();
        String str = this.getText().toString();
        Log.i(TAG, str);
        if (str == null) {
            textWidth = 0;
        }
        textWidth = (int) paint.measureText(str);
    }

    /**
     * 设置滚动次数,达到次数后设置不可见
     *
     * @param circleTimes
     */
    public void setCircleTimes(int circleTimes) {
        this.circleTimes = circleTimes;
    }

    public void setSpeed(int speed) {
        this.circleSpeed = speed;
    }

    public void startScrollShow() {
        if (this.getVisibility() == View.GONE)
            this.setVisibility(View.VISIBLE);
        this.removeCallbacks(this);
        post(this);
    }

    private void stopScroll() {
        handler.removeCallbacks(this);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

//布局
  1. 第三方依赖
//导入依赖
 implementation 'com.gongwen:marqueelibrary:1.1.3'



//在Activity或Fragment中写
 final List<String> datas = Arrays.asList("恭喜强哥购买了一件衣服");
  SimpleMarqueeView<String> marqueeView = (SimpleMarqueeView) get(R.id.simpleMarqueeView);
        SimpleMF<String> marqueeFactory = new SimpleMF(context);
        marqueeFactory.setData(datas);
        marqueeView.setMarqueeFactory(marqueeFactory);
        marqueeView.startFlipping();



//布局文件
  <com.gongwen.marqueen.SimpleMarqueeView
        android:id="@+id/simpleMarqueeView"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@+id/grid_home"
        android:flipInterval="2500"
        android:inAnimation="@anim/in_top"
        android:outAnimation="@anim/out_bottom"
        app:marqueeAnimDuration="2000"
        app:smvTextColor="@color/colorAccent"
        app:smvTextEllipsize="end"
        app:smvTextGravity="left"
        app:smvTextSingleLine="true"
        app:smvTextSize="15sp" />


//解决重影

@Override
public void onStart() {
    super.onStart();
    marqueeView.startFlipping();
}

@Override
public void onStop() {
    super.onStop();
    marqueeView.stopFlipping();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值