Android --- 跑马灯效果

article/2025/10/23 0:52:30

1、基于开发者文档的官方说明

跑马灯效果主要使用的控件为TextView,其中涉及的几个标签如下所示:

android:ellipsize
If set, causes words that are longer than the view is wide to be ellipsized instead of broken in the middle. You will often also want to set scrollHorizontally or singleLine as well so that the text as a whole is also constrained to a single line instead of still allowed to be broken onto multiple lines.
(如果设置,则会导致比视图长的单词被省略,而不是在在中间被打断。您通常还需要设置scrollHorizontal或singleLine,以便文本作为一个整体也被限制为单行,而不是仍然被允许拆分为多行。)
在这里插入图片描述

java代码中对应的方法:setEllipsize(TextUtils.TruncateAt)

    /*** Causes words in the text that are longer than the view's width* to be ellipsized instead of broken in the middle.  You may also* want to {@link #setSingleLine} or {@link #setHorizontallyScrolling}* to constrain the text to a single line.  Use <code>null</code>* to turn off ellipsizing.*/public void setEllipsize(TextUtils.TruncateAt where) {// TruncateAt is an enum. != comparison is ok between these singleton objects.if (mEllipsize != where) {mEllipsize = where;if (mLayout != null) {nullLayouts();requestLayout();invalidate();}}}//TruncateAt包含的属性值public enum TruncateAt {START,MIDDLE,END,MARQUEE,/*** @hide*/@UnsupportedAppUsageEND_SMALL}

android:focusableInTouchMode:布尔值,用于控制视图在触摸模式下是否可以对焦
android:singleLine:将文本限制为单个水平滚动行(当前已弃用,推荐使用maxLines)
android:clickable:定义此视图是否对单击事件做出反应

2、xml文件的方式

	<!--纯xml文件,需要点击后才能开始跑马灯的效果--><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/horse_race_lamp_value"android:textColor="@color/teal_700"android:ellipsize="marquee"android:focusableInTouchMode="true"android:singleLine="true"android:clickable="true"android:textSize="30sp"/>

3、xml文件+代码控制

    <!--使用xml文件+代码控制--><TextViewandroid:id="@+id/tv_horse_lamp"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/horse_race_lamp_value"android:singleLine="true"android:textSize="30sp"android:textColor="@color/purple_200"/>
    @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_horse_race_lamp);//通过代码控制,获取控件,添加mHorseLampTv = findViewById(R.id.tv_horse_lamp);mHorseLampTv.setEllipsize(TextUtils.TruncateAt.MARQUEE);mHorseLampTv.setSelected(true);}
//针对setSelected的源码解释,可以看到此方法中针对Ellipsize属性判断是否为MARQUEE(走马灯的条件之一)@Overridepublic void setSelected(boolean selected) {boolean wasSelected = isSelected();super.setSelected(selected);if (selected != wasSelected && mEllipsize == TextUtils.TruncateAt.MARQUEE) {if (selected) {startMarquee();} else {stopMarquee();}}}

4、xml文件+自定义TextView控件

package com.example.clientapplication;import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;import androidx.annotation.NonNull;
import androidx.annotation.Nullable;//自定义控件
public class MyHorseRaceLampView extends androidx.appcompat.widget.AppCompatTextView {public MyHorseRaceLampView(@NonNull Context context) {super(context);}public MyHorseRaceLampView(@NonNull Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public MyHorseRaceLampView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overridepublic boolean isFocused() {return true;}
}
    <!--使用自定义控件--><com.example.clientapplication.MyHorseRaceLampViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/horse_race_lamp_value"android:textColor="@color/cardview_dark_background"android:singleLine="true"android:ellipsize="marquee"android:textSize="30sp"/>

5、全部代码

  • activity_horse_race_lamp.xml
<?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"android:orientation="vertical"tools:context=".HorseRaceLampActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="走马灯效果"android:gravity="center"android:textSize="40sp"/>
<!--纯xml文件--><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/horse_race_lamp_value"android:textColor="@color/teal_700"android:ellipsize="marquee"android:focusableInTouchMode="true"android:singleLine="true"android:clickable="true"android:textSize="30sp"/><TextViewandroid:id="@+id/tv_horse_lamp"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/horse_race_lamp_value"android:singleLine="true"android:textSize="30sp"android:textColor="@color/purple_200"/><!--使用自定义控件--><com.example.clientapplication.MyHorseRaceLampViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/horse_race_lamp_value"android:textColor="@color/cardview_dark_background"android:singleLine="true"android:ellipsize="marquee"android:textSize="30sp"/>
</LinearLayout>
  • HorseRaceLampActivity
package com.example.clientapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;public class HorseRaceLampActivity extends AppCompatActivity {private TextView mHorseLampTv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_horse_race_lamp);//通过代码控制mHorseLampTv = findViewById(R.id.tv_horse_lamp);mHorseLampTv.setEllipsize(TextUtils.TruncateAt.MARQUEE);mHorseLampTv.setSelected(true);}
}
  • MyHorseRaceLampView
package com.example.clientapplication;import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;import androidx.annotation.NonNull;
import androidx.annotation.Nullable;public class MyHorseRaceLampView extends androidx.appcompat.widget.AppCompatTextView {public MyHorseRaceLampView(@NonNull Context context) {super(context);}public MyHorseRaceLampView(@NonNull Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public MyHorseRaceLampView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overridepublic boolean isFocused() {return true;}
}

6、效果展示

请添加图片描述


http://chatgpt.dhexx.cn/article/WFXEoLjc.shtml

相关文章

html跑马灯效果

该笔记记录 <marquee> 标签&#xff0c;跑马灯除了使用js实现&#xff0c;还可以使用 marquee标签&#xff0c;自带跑马灯效果。&#xff08;marquee已经 Obsolete了&#xff0c;但是它很好用&#xff09; 基本用法 <!-- marquee是块元素&#xff0c;直接在其内部放…

跑马灯效果如何实现?Smartbi一文助你掌握

大数据时代&#xff0c;人的注意力越来越成为一种稀缺资源&#xff0c;让信息去匹配读者注意力&#xff0c;显然优于让人的注意力去适应信息。因此我们就需要在数据呈现上进行创新。那么如何让数据变得动态化&#xff0c;可视化呢&#xff1f;或许可以尝试一下跑马灯效果。 跑…

滚动动画(跑马灯动画)的几种实现方式

在大屏可视化应用中&#xff0c;滚动动效&#xff08;跑马灯效果&#xff09;也是常见的一种数据展现方式&#xff0c;本章节针对字幕滚动和列表滚动效果做一个小小的总结&#xff0c;结合vue框架&#xff0c;具体展示效果如下&#xff0c;从左至右选型技术分别为&#xff1a;m…

本地KMS虚拟服务器,搭建kms本地服务器

搭建kms本地服务器 内容精选 换一换 Kerberos是一种计算机网络认证协议&#xff0c;它允许某实体在非安全网络环境下通信&#xff0c;向另一个实体以一种安全的方式证明自己的身份。它也指由麻省理工实现此协议&#xff0c;并发布的一套免费软件。软件设计上采用客户端/服务器模…

Ubuntu搭建kms服务器

Key Management Service&#xff08;简称:KMS&#xff09;&#xff0c;这个功能是在Windows Vista之后的产品中的一种新型产品激活机制。我们可以利用手里闲置的VPS安装vlmcsd来搭建KMS激活服务器&#xff0c;这篇文章以Ubuntu为例&#xff0c;当然vlmcsd并不局限限于Ubuntu&am…

linux系统安装kms,Linux安装部署KMS服务器

Loading... # Linux 安装部署 KMS 服务器 ## 一、强制关闭kms-server服务 killall kms-server ## 二、下载 kms 服务文件 ### 1、下载 32bit包 如果你要安装 32位包&#xff0c;请直接下载 bash wget https://dl.wxlost.com/2017/01/Linux/vlmcsd-x86-musl-static -O /usr/bi…

centos7快速搭建KMS服务器

1、下载最新的vlmcsd服务端 https://github.com/Wind4/vlmcsd/releases/tag/svn1113 2、解压&#xff0c;并找到binaries/Linux/intel/static/vlmcsd-x64-musl-static并复制到到/usr/bin/vlmcsd 3、赋予执行权限 chmod x /usr/bin/vlmcsd 4、添加开机启动/etc/rc.local中添…

Windows搭建kms服务器

文章目录 使用Windows系统搭建kms服务器前言准备正式开始 使用Windows系统搭建kms服务器 前言 网上有许多搭建的教程&#xff0c;但是使用的都是Linux系统&#xff0c;而我们正常使用的则是Windows系统。对Linux不熟悉的同学看着那左一个代码命令右一个代码命令的教程如同天书…

kms服务器搭建及测试

vlmcsd代码仓库&#xff1a;https://github.com/Wind4/vlmcsd 搭建步骤 本次操作环境centos7&#xff0c;通过内网进行搭建 1.安装依赖环境 yum install gcc git make -y 2.下载源码&#xff0c;编译安装 cd /var/opt/ git clone https://github.com/Wind4/vlmcsd.git cd vlmcs…

kms的服务器改回系统默认,指定kms服务器地址后还原

指定kms服务器地址后还原 内容精选 换一换 您可以通过本节内容解决如下问题:用户在管理控制台执行弹性云服务器相关操作后出现异常,针对管理控制台提示的异常信息,应该如何处理?用户参见《弹性云服务器接口参考》调用云服务器相关的API接口时,如果返回错误码,应该如何处理…

Windows部署KMS服务器

下载安装vlmcsd 地址&#xff1a;https://github.com/Wind4/vlmcsd/releases/download/svn1111/binaries.tar.gz 解压 binaries.tar.gz 文件&#xff0c;找到 \binaries\Windows\intel 下的 vlmcsd-Windows-x86.exe 或者 vlmcsd-Windows-x64.exe &#xff08;根据操作系统选择…

虚拟机如何搭建kms服务器,linux 搭建kms服务器

linux 搭建kms服务器 内容精选 换一换 用户通过管理控制台创建或者导入密钥对后,在购买弹性云服务器时,登录方式选择密钥对,并选择创建或者导入的密钥对。用户购买弹性云服务器成功后,可使用密钥对的私钥登录弹性云服务器。使用的登录工具(如PuTTY、Xshell)与待登录的弹性云…

查看kms服务器信息,查看kms服务器地址

查看kms服务器地址 内容精选 换一换 可以一次添加一台服务器,也可以一次添加同一网段连续IP的多台服务器。进入任务中心可以查看状态信息,如果状态为成功,说明服务器已添加成功。 查询指定后端云服务器组的后端云服务器。支持过滤查询和分页查询。如未特殊说明,匹配规则为精…

kms服务器搭建

kms服务器搭建 1. 下载vlmcsd 地址&#xff1a;https://github.com/Wind4/vlmcsd/releases 下载最终版压缩包: binaries.tar.gz 解压后&#xff0c;可看到这个压缩包适了各种操作系统 以windows为例&#xff1a; cd C:\Users\admin\Desktop\kms\binaries\Windows\intel #create…

kms服务器修改,kms服务器ip地址修改

kms服务器ip地址修改 内容精选 换一换 以NTP服务器、DNS服务器的操作系统均为SUSE为例:登录Linux弹性云服务器。执行以下命令,切换至root用户。sudo su -sudo su -执行以下命令,编辑ntp.conf文件。vim /etc/ntp.confvim /etc/ntp.conf添加以下语句,配置NTP服务器。server N…

KMS服务器

1. 一键安装包&#xff08;本脚本安装完成后&#xff0c;会将 KMS 服务加入开机自启动&#xff09; wget --no-check-certificate https://github.com/teddysun/across/raw/master/kms.sh && chmod x kms.sh && ./kms.sh如果是国内的服务器可能无法下载成功&a…

稳定kms服务器,kms服务器

kms服务器 内容精选 换一换 请您在购买前确保已完成注册和充值。详细操作请参见如何注册公有云管理控制台的用户&#xff1f;登录管理控制台。单击管理控制台左上角的&#xff0c;选择区域和项目。选择“计算 > 弹性云服务器”。单击“购买弹性云服务器”。系统进入购买页。…

利用VLMCSD部署本地KMS服务器(Windows + CentOS7)

利用KMS激活Windows操作系统和Office早已不是什么新鲜事&#xff0c;但是呢&#xff0c;不信任来路不明的激活工具&#xff0c;外部服务器又不稳定&#xff0c;随时可能停服&#xff0c;所以&#xff0c;还是发扬自己动手、丰衣足食的优良作风&#xff0c;求人不如求己&#xf…

自己搭建 KMS激活服务器

1、C盘建KMS文件夹&#xff0c;把vlmcsd-Windows-x64.exe复制到kms文件夹内 2、以管理员运行cmd,在cmd切换到C盘KMS文件下&#xff0c;先输入cd c:/ 回车&#xff0c;再输入cd kms&#xff0c;这两个命令的意思是进入到C盘的kms文件夹下&#xff0c; 3、输入 vlmcsd-Windows-x6…

moTzxx-CMS —— [一个基于PHP代码的后台管理系统(ThinkPHP5.1.40)]

☁ 前言 【重要】 如果你先前已经下载了源码&#xff0c;后期发现存在些许问题时 请及时反馈给我&#xff0c;以便及时更新&#xff1b; 或者回来参考我更新的内容&#xff0c;尤其是 “使用指导” 部分&#xff0c;或许这时我已经自测并做了补充信息哦. 框架版本&#xff1a;…