码迷,mamicode.com
首页 > 移动开发 > 详细

Android自定义View之组合控件 ---- LED数字时钟

时间:2014-08-20 14:08:42      阅读:355      评论:0      收藏:0      [点我收藏+]

标签:android   view   

先上图

bubuko.com,布布扣

LEDView效果如图所示。

之前看到一篇博客使用两个TextView实现了该效果,于是我想用自定义控件的方式实现一个LEDView,使用时即可直接使用该控件。

采用组合控件的方式,将两个TextView叠放在一起,再使用digital-7.ttf字体来显示数据,从而达到LED的效果。代码如下:

LEDView.class

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);
	}
}

ledview.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" >
      <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>


控件使用Demo

package ione.zy.demo;

import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class LEDActivity extends Activity {

	private LEDView ledView;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_led);
		ledView = (LEDView) findViewById(R.id.ledview);
		
		  ActionBar actionBar = getActionBar();  
		  actionBar.setDisplayHomeAsUpEnabled(true); 
	}

	@Override
	protected void onResume() {
		super.onResume();
		ledView.start();
	}

	@Override
	protected void onStop() {
		super.onStop();
		ledView.stop();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_led, menu);
		return true;
	}

}


activity_led.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LEDActivity"
    android:background="@color/black" >

   <ione.zy.demo.LEDView 
        android:id="@+id/ledview"
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_centerInParent="true"
        android:layout_gravity="center"  />  

</RelativeLayout>


下载Demo


Android自定义View之组合控件 ---- LED数字时钟,布布扣,bubuko.com

Android自定义View之组合控件 ---- LED数字时钟

标签:android   view   

原文地址:http://blog.csdn.net/yangysng07/article/details/38704935

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!