效果如下:
程序非常簡單,下面是代碼,解釋在註釋中。
新建一個工程,打開activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000" >
<!--
android:shadowColor 陰影顏色
android:shadowDx 陰影的水平偏移量
android:shadowDy 陰影的垂直偏移量
android:shadowRadius 陰影的範圍
-->
<TextView
android:id="@+id/ledview_clock_time"
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="20"
android:textColor="#00ffff"
android:textSize="80sp" />
<TextView
android:id="@+id/ledview_clock_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#3300ff00"
android:textSize="80sp" />
</RelativeLayout>
在MainActivit.java中
public class MainActivity extends Activity {
// tv1用來顯示時間,tv2是用來做個背景的
private TextView tv1, tv2;
// 想調用字體需要使用這個Typeface
private Typeface typeface;
// 設置一個常量,這裏就是我們的數碼管字體文件
private static final String FONT_DIGITAL_7 = "fonts" + File.separator
+ "digital-7.ttf";
// Handler裏用
private static final int SHOW_TIME = 1;
private Timer timer = new Timer();
private TimerTask timerTask = new TimerTask() {
@Override
public void run() {
// 調用控件不能在子線程,所以這裏需要在handler中調用
handler.sendEmptyMessage(SHOW_TIME);
}
};
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case SHOW_TIME:
Calendar calendar = Calendar.getInstance();
// 顯示時間
tv1.setText(String.format("%02d:%02d:%02d",
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
calendar.get(Calendar.SECOND)));
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 去掉標題欄
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.ledview_clock_time);
tv2 = (TextView) findViewById(R.id.ledview_clock_bg);
// 準備字體
typeface = Typeface.createFromAsset(getAssets(), FONT_DIGITAL_7);
// 設置字體
tv1.setTypeface(typeface);
tv2.setTypeface(typeface);
tv2.setText("88:88:88");
// 0毫秒後執行timerTask,並且以後每隔1000毫秒執行一次timerTask
timer.schedule(timerTask, 0, 1000);
}
}
備註: %02d:%02d:%02d
2是寬度,如果整數不夠2列就補上0
比如:printf(“%02d” ,3); 結果就是 03
如果大於2則沒有影響:printf(“%02d”,1234); 結果就是 1234
還有就是數碼管字體是我們自己添加的,自己把digital-7.ttf添加到圖中這個文件夾裏,沒有就創建,這個ttf文件後面鏈接裏有下載。
最後是項目代碼下載鏈接:
http://download.csdn.net/detail/zhang5690800/9405665