LED时钟显示

本文介绍如何使用自定义的LEDTextView实现类似LED时钟的样式,并通过设置不同的字体和阴影属性来调整视觉效果。此外,还提供了一个简单的时钟跳动效果的实现方法。

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

类似于LED 时钟的样式字体设置通过ttf设置,ttf为字体格式

1.新建assets文件夹,文件夹的的位置必须是和res平级


2.新建LedTextView 设置字体

    public class LEDTextView extends TextView {

    private static final String FONTS_FOLDER = "fonts";

    private static final String FONT_DIGITAL_7 = FONTS_FOLDER

            + File.separator + "digital-7.ttf";



    public LEDTextView(Context context) {

        super(context);

        init(context);

    }



    public LEDTextView(Context context, AttributeSet attrs) {

        super(context, attrs);

        init(context);

    }



    public LEDTextView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

        init(context);

    }



    private void init(Context context) {

        AssetManager assets = context.getAssets();

        final Typeface font = Typeface.createFromAsset(assets,

                FONT_DIGITAL_7);

        setTypeface(font);

    }

}


3.布局文件

<com.ddswez.lpf.myapplication.hack.hack11.LEDTextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_centerInParent="true"

    android:text="88:88:88"

    android:textColor="#33ff0000"

    android:textSize="80sp" />



<com.ddswez.lpf.myapplication.hack.hack11.LEDTextView

    android:id="@+id/main_clock_time"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_centerInParent="true"

    android:shadowColor="#ff0000"

    android:shadowDx="0"

    android:shadowDy="0"

    android:shadowRadius="10"

    android:textColor="#ff0000"

    android:textSize="80sp" />

修改 shadowDx  shadowDy  属性的值可以改变阴影与文本之间的偏移 , 指定 android:shadowRadius 属性可以产生一种文本更亮的错觉 .


4.时钟跳动的代码就是一个handler获取当前时间循环调用

private final Runnable mTimeRefresher = new Runnable() {

    @Override

    public void run() {

        final Date d = new Date();

        mTextView.setText(String.format(DATE_FORMAT, d.getHours(),

                d.getMinutes(), d.getSeconds()));

        mHandler.postDelayed(this, REFRESH_DELAY);

    }

};

显示效果












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值