Android 为文本添加发亮的效果(LED时钟为例)

这篇博客介绍如何在Android应用中为文本添加类似LED时钟的发亮效果。通过下载特定字体并将其置于项目资源文件夹,然后创建一个自定义的LedTextView类,实现字体的设置,从而达到数字时钟的发光效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先我们要找到字体,字体下载链接在文章最下方

解压之后选一个字体ttf文件,将字体文件存放在如图所示的位置


创建继承自TextView的LedTextView类(目的:设置字体)

public class LedTextView extends TextView {
    public LedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        AssetManager assets = context.getAssets();
<span style="white-space:pre">	</span>//如下代码所示方式引用字体代码文件
        final Typeface font = Typeface.createFromAsset(assets, "font/digital-7.ttf");
        setTypeface(font);
    }
}
这里我们用两个TextView重合来实现88:88:88的阴影,两个TextView完全重叠,一个显示阴影,一个显示时间
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.darren.androidtest.MainActivity"
    tools:showIn="@layout/activity_main">
    />
    <!-- 用于显示阴影-->
    <view.LedTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="88:88:88"
        android:textColor="#3300FF00"
        android:textSize="80sp" />
    <!-- 用于显示时间-->
    <view.LedTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:shadowColor="#00FF00"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="10"
        android:text="09: 15:30"
        android:textColor="#00FF00"
        android:textSize="80sp"
        />
</RelativeLayout>

这样就基本实现了
参考资料:《打造高质量的Android应用》
字体下载链接:http://www.styleseven.com/data/font_digital-7.zip

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值