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

来源:互联网 发布:mac 更新ruby版本 编辑:程序博客网 时间:2024/05/16 04:34

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

解压之后选一个字体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

0 0
原创粉丝点击