Android自定义View例子_安卓LED数字时钟效果Demo
2014-08-26 11:37:35  By: shinyuu

介绍

相信大家对 LED 都不陌生吧、一般在街上都会看到很多做广告的

都是使用 LED 来做的、非常普遍、随处都可以看到这种东西

那么在 Android 系统里面怎么实现 LED 的效果呢

那么本文就为大家讲解一下怎么通过 Android 系统来实现 LED 的效果


源代码

1、Androic 系统本生是没有提供该功能的、所以我们得自定义 View 来实现

定义两个 TextView 一个做 背景的 88:88:88 一个做当前时间

代码如下


<?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" >
      <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 ="10"  
        android:textColor ="#00ff00"  
        android:textSize ="80sp" />  
        
    <TextView
        android:id ="@+id/ledview_clock_bg"   
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_centerInParent="true"
        android:layout_gravity="center"  
        android:text="@string/default_time"  
        android:textColor="#3300ff00"  
        android:textSize="80sp" />  
</RelativeLayout>


2、通过Java代码每秒钟去更新一下显示的时间

代码如下


package ione.zy.demo;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LEDView extends LinearLayout {

	private TextView timeView;
	private TextView bgView;
	private static final String FONT_DIGITAL_7 = "fonts" 
			+ File.separator
			+ "digital-7.ttf";

	private static final String DATE_FORMAT = "%02d:%02d:%02d";
	private static final int REFRESH_DELAY = 500;

	private final Handler mHandler = new Handler();
	private final Runnable mTimeRefresher = new Runnable() {

		@Override
		public void run() {
			Calendar calendar = Calendar.getInstance(TimeZone
					.getTimeZone("GMT+8"));
			final Date d = new Date();
			calendar.setTime(d);

			timeView.setText(String.format(DATE_FORMAT,
					calendar.get(Calendar.HOUR), calendar
						.get(Calendar.MINUTE),
					calendar.get(Calendar.SECOND)));
			mHandler.postDelayed(this, REFRESH_DELAY);
		}
	};

	@SuppressLint("NewApi")
	public LEDView(Context context, AttributeSet attrs, 
		int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public LEDView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public LEDView(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		LayoutInflater layoutInflater = LayoutInflater
			.from(context);

		View view = layoutInflater.inflate(R.layout
			.ledview, this);
		timeView = (TextView) view.findViewById(R.id
			.ledview_clock_time);
		bgView = (TextView) view.findViewById(R.id
			.ledview_clock_bg);
		AssetManager assets = context.getAssets();
		final Typeface font = Typeface
			.createFromAsset(assets, FONT_DIGITAL_7);
		timeView.setTypeface(font);// 设置字体
		bgView.setTypeface(font);// 设置字体

	}

	public void start() {
		mHandler.post(mTimeRefresher);
	}

	public void stop() {
		mHandler.removeCallbacks(mTimeRefresher);
	}
}
3、这样一个 LED 的数字时钟的自定义 View 就写好了、那么下面不是在 Activity 里面调用了


我就不做过多的讲解了、大家可以源代码中直接下载 Demo

源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1c0y849a 密码: nj16

若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力

想获取DD博客最新代码、你可以扫描下方的二维码、关注DD博客微信公众号(ddblogs)

或者你也可以关注我的新浪微博、了解DD博客的最新动态:DD博客官方微博(dwtedx的微博)

如对资源有任何疑问或觉得仍然有很大的改善空间、可以对该博文进行评论、希望不吝赐教

为保证及时回复、可以使用博客留言板给我留言: DD博客留言板(dwtedx的留言板)

感谢你的访问、祝你生活愉快、工作顺心、欢迎常来逛逛


快速评论


技术评论

    • MinganCai 2014-09-19 18:00:40  2 评  | 回复

      http://download.csdn.net/detail/melody8869/7943949 这是最近实现的demo


DD记账
top
+