TextView设置跑马灯效果

2022-05-05 20:33:31 浏览数 (1)

TextView设置跑马灯效果

  • 需求
  • 分析
  • 实现效果
  • 具体实现
    • android.xml代码
      • 关键属性介绍
    • android.xml用到的background资源
    • .java业务代码

需求

实现视频上方文字滚动效果

https://live.csdn.net/v/embed/204299 android跑马灯效果

分析

可使用TextView来实现这个跑马灯效果

实现效果

文字底部设置灰色透明背景显示,名称以及奖励金额使用高亮色号显示

https://live.csdn.net/v/embed/204304 TextView跑马灯效果示例

具体实现

android.xml代码

代码语言:javascript复制
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <TextView
        android:id="@ id/tv_head_effect"
        android:layout_width="200dp"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:text="恭喜玩家XXX成功提现100元"
        android:textColor="#FFFFFF"
        android:textSize="12sp"
        android:textStyle="bold"
        android:background="@drawable/head_effect_shape"/>

</FrameLayout>

关键属性介绍

代码语言:javascript复制
android:ellipsize="marquee" //设置跑马灯显示效果
android:singleLine="true" //必须单行显示
android:marqueeRepeatLimit="marquee_forever" //实现无限循环

android.xml用到的background资源

Android实现页面渐变效果

.java业务代码

代码语言:javascript复制
package com.fungame.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;
import com.fungame.R;
import androidx.annotation.NonNull;

public class HeadEffectActivity extends BaseActivity {

    private TextView tv_head_effect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_head_effect);

        tv_head_effect = findViewById(R.id.tv_head_effect);

        String[] userNameArr = {"依依", "丫丫", "张三", "尼古拉斯赵四", "虎虎生威",
                "六六大顺", "琪琪", "八八大发", "九九归一", "十全十美"};
        String[] moneyArr = {"100", "200", "300", "400", "500",
                "600", "700", "800", "900", "1000"};

        String str = "";
        String s = "";
        SpannableString spannableStr = null;
        SpannableString spannableS = null;
        SpannableStringBuilder spannableStringBuilder = null;
        SpannableStringBuilder ssb = new SpannableStringBuilder();

        for (int i = 0; i < userNameArr.length; i  ) {
            spannableStringBuilder = new SpannableStringBuilder("恭喜玩家");
            str = userNameArr[i];
            s = moneyArr[i];
            spannableStr = new SpannableString(str);
            spannableS = new SpannableString(s);
            spannableStr.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                }

                // 重写显示状态
                @Override
                public void updateDrawState(@NonNull TextPaint ds) {
                    ds.setColor(Color.parseColor("#FF00FF"));
                }
            }, 0, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            spannableS.setSpan(new ClickableSpan() {
                @Override
                public void onClick(@NonNull View widget) {
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    ds.setColor(Color.parseColor("#FF00FF"));
                }
            }, 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            spannableStringBuilder.append(spannableStr);
            spannableStringBuilder.append("成功提现");
            spannableStringBuilder.append(spannableS);
            spannableStringBuilder.append("元");
            spannableStringBuilder.append("                                                                  ");
            ssb.append(spannableStringBuilder);
        }

        tv_head_effect.setSelected(true);
        tv_head_effect.setText(ssb);
        tv_head_effect.invalidate();
    }
}

0 人点赞