Android --- 跑马灯效果

这篇具有很好参考价值的文章主要介绍了Android --- 跑马灯效果。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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,以便文本作为一个整体也被限制为单行,而不是仍然被允许拆分为多行。)
Android --- 跑马灯效果

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
         */
        @UnsupportedAppUsage
        END_SMALL
    }

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

2、xml文件的方式

	<!--纯xml文件,需要点击后才能开始跑马灯的效果-->
    <TextView
        android: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文件+代码控制-->
    <TextView
        android: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"/>
    @Override
    protected 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(走马灯的条件之一)
    @Override
    public 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);
    }

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

    <!--使用自定义控件-->
    <com.example.clientapplication.MyHorseRaceLampView
        android: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">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="走马灯效果"
        android:gravity="center"
        android:textSize="40sp"/>
<!--纯xml文件-->
    <TextView
        android: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"/>
    <TextView
        android: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.MyHorseRaceLampView
        android: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;

    @Override
    protected 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);
    }

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

6、效果展示

Android --- 跑马灯效果文章来源地址https://www.toymoban.com/news/detail-401694.html

到了这里,关于Android --- 跑马灯效果的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • JavaFx 实现水平滚动文本(跑马灯效果)

    原文地址: JavaFx 实现水平滚动文本(跑马灯效果) - Stars-One的杂货小窝 本文是以TornadoFx框架进行编写,各位使用JavaFx可以参考 代码已经封装在common-controls库中 实现原理就是利用了JavaFx里的动画效果去修改scrollpane的translateX属性,原本想在text上改造的,发现文字过多就不行了,最终还

    2023年04月27日
    浏览(38)
  • CSS中如何实现文字跑马灯效果?

    前端入门之旅:探索Web开发的奇妙世界 欢迎来到前端入门之旅!感兴趣的可以订阅本专栏哦!这个专栏是为那些对Web开发感兴趣、刚刚踏入前端领域的朋友们量身打造的。无论你是完全的新手还是有一些基础的开发者,这里都将为你提供一个系统而又亲切的学习平台。在这个

    2024年02月09日
    浏览(35)
  • 使用Vue.js实现文字跑马灯效果

    实现文字跑马灯效果,首先用到 substring()截取 和 setInterval计时器 clearInterval()清除计时器 效果如下: 实现代码如下: 以上是实现文字跑马灯效果,如有不足的地方,欢迎在评论区留言。

    2023年04月19日
    浏览(32)
  • 探究前端的跑马灯效果是如何用css实现的

    💖 作者简介:大家好,我是阿牛,全栈领域优质创作者😜 📝 个人主页:馆主阿牛🔥 🎉 支持我:点赞👍+收藏⭐️+留言📝 📣 系列专栏:前端实用小demo🍁 💬格言:迄今所有人生都大写着失败,但不妨碍我继续向前!🔥 无意见看到了一个网站的一个动画的跑马灯效果

    2024年04月10日
    浏览(31)
  • 基于Verilog的跑马灯设计

    设计一个能够有多种工作模式控制的8个灯亮灭的电路。 工作模式1:按照从左到右的方向,依次点亮每一盏灯,然后依次熄灭每一盏灯; 工作模式2:分成两组灯,前四个灯为1组,后四个为2组,1组灯按从左到右依次点亮,同时2组灯按从右到左依次点亮,然后两组灯按各自点

    2024年02月08日
    浏览(32)
  • 基于51单片机8位竞赛抢答器_倒计时可调+LED跑马灯

    (程序+proteus仿真+报告) Proteus仿真版本:proteus 7.8 程序编译器:keil 4/keil 5 编程语言:C语言 设计编号:Q006 资料下载链接 1、以单片机位核心,设计一个8位抢答器:同时供8名选手比赛,分别用6个按键表示; 2、无人抢答时,8个跑马灯循环点亮,数码管显示00; 3、设置一个

    2024年02月09日
    浏览(26)
  • 单片机(3)跑马灯,按钮控制的跑马灯(2种编程)

     先上电路图(图示的是高电平点亮的跑马灯)  这个是程序截图(keil5):我的建议是是先自己打一遍,边打边试着理解程序的意思。 下面的是没有注释的代码 下一个是另外一种编程思路  

    2024年02月11日
    浏览(28)
  • STM32F407ZGT6正点原子F4探索者开发板 -- 跑马灯例程

    LED0 - PF9 LED1 - PF10 PF9 = 0, LED0 亮,PF9 = 1,LED0 灭 PF10 = 0, LED1 亮,PF10 = 1,LED1 灭

    2024年02月15日
    浏览(45)
  • 跑马灯实验

             1.熟悉龙芯实验开发板、熟悉 VIVADO 的编译环境及操作流程。         2.掌握 FPGA 编程入门知识、利用门级方法实现简单逻辑电路。         3.继续学习 Verilog HDL 语法、掌握跑马灯的设计、熟悉调试过程。          本次实验用 Verilog HDL 语言来描述 6 个不

    2024年02月04日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包