用TextView显示时间(数码管样式)

来源:互联网 发布:aveva软件购买 编辑:程序博客网 时间:2024/06/05 08:15

效果如下:
这里写图片描述

程序非常简单,下面是代码,解释在注释中。
新建一个工程,打开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

0 0