• Android移动应用开发之TextView实现阴影跑马灯文字效果


    前言

    首先需要已经安装好了Andorid Studio并且配置好了虚拟手机并能够运行开机。

    创建项目

    创建一个project
    在这里插入图片描述
    选empty即可
    在这里插入图片描述
    点击next

    在这里插入图片描述
    自己调整项目路径
    注意不要出现中文
    这里Android选择5.0说可以支持大概98.8%的设备,蛮多了。
    点击finish完成创建

    然后他会自己下载配置文件,需要联网耐心等待。

    下载完成后
    在这里插入图片描述
    点击project选择project可以展现工程的全部文件

    点击Android就展现
    Android目录下的文件
    在这里插入图片描述

    配置文件简介

    ##
    主要用户来放需要的java类

    在这里插入图片描述
    layout放置布局文件

    在这里插入图片描述
    values放颜色、文字、样式等配置文件。

    阴影走马灯文字

    values/string.xml

    配置文字

    <resources>
        <string name="app_name">Hunter Worldstring>
        <string name="tv_one">ICY Hunter ICY Hunter ICY Hunter ICY Hunterstring>
    resources>
    
    • 1
    • 2
    • 3
    • 4

    values/colors.xml

    配置颜色

    
    <resources>
        <color name="purple_200">#FFBB86FCcolor>
        <color name="purple_500">#FF6200EEcolor>
        <color name="purple_700">#FF3700B3color>
        <color name="teal_200">#FF03DAC5color>
        <color name="teal_700">#FF018786color>
        <color name="black">#FF000000color>
        <color name="white">#FFFFFFFFcolor>
    resources>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    layout/activity_main.xml

    配置布局文件

    
    <LinearLayout
        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_one"
            android:layout_width="match_parent"
            android:layout_height="282dp"
            android:text="@string/tv_one"
            android:textColor="@color/black"
            android:textStyle="normal"
            android:textSize="100sp"
            android:background="@color/purple_200"
            android:layout_gravity="center_vertical"
            android:shadowColor="@color/purple_700"
            android:shadowRadius="3.0"
            android:shadowDx="15.0"
            android:shadowDy="10.0"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:clickable="true"
            >
            <requestFocus/>
        TextView>
    
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    在这里插入图片描述
    点击运行app
    然后虚拟机就会出现如下:
    在这里插入图片描述
    出现滚动的带阴影的跑马灯的效果。

    法2

    对代码进行如下添加和修改:

    main/java

    main/java的保重创建一个java类(MyTextView)
    在这里插入图片描述
    MyTextView:

    package com.example.hunter;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    import androidx.annotation.Nullable;
    
    public class MyTextView extends TextView {
    
        public MyTextView(Context context) {
            super(context);
        }
    
        public MyTextView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public boolean isFocused() {
            return true;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    layout/activity_main.xml

    用上自己写的类

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <com.example.hunter.MyTextView
            android:id="@+id/tv_one"
            android:layout_width="match_parent"
            android:layout_height="282dp"
            android:text="@string/tv_one"
            android:textColor="@color/black"
            android:textStyle="normal"
            android:textSize="100sp"
            android:background="@color/purple_200"
            android:layout_gravity="center_vertical"
            android:shadowColor="@color/purple_700"
            android:shadowRadius="3.0"
            android:shadowDx="15.0"
            android:shadowDy="10.0"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:clickable="true"
            />
    
    
    
    </LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    在这里插入图片描述
    然后点击重新运行

    出现一样的效果。

    参考

    https://www.bilibili.com/video/BV1Jb4y187C4?p=10&spm_id_from=pageDriver

  • 相关阅读:
    AB Test实验设计
    JavaScript进阶 第二天笔记
    Python中的del用法
    《Webpack 5 基础配置》- 禁止在出现编译错误或警告时,覆盖浏览器全屏显示
    vue组件间的通讯方式
    AWS 学习总结(附思维导图链接)
    CentOS环境下使用Docker部署SpringBoot应用流程
    MySQL-三大日志
    梨花带雨音乐播放器3.91源码开源(网站添加背景音乐)
    [计算机网络]IP协议
  • 原文地址:https://blog.csdn.net/qq_52785473/article/details/126849502