类似于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);
}
};
显示效果