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 World</string>
    <string name="tv_one">ICY Hunter ICY Hunter ICY Hunter ICY Hunter</string>
</resources>

values/colors.xml

配置颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
</resources>

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">

    <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>

在这里插入图片描述
点击运行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;
    }
}

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>

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

出现一样的效果。

参考

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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Icy Hunter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值