先上图

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数字时钟的更多相关文章

  1. Android开发技巧——自定义控件之组合控件

    Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...

  2. 【转】android UI进阶之自定义组合控件

    [源地址]http://blog.csdn.net/notice520/article/details/6667827 好久没写博客了.实在是忙不过来,不过再不总结总结真的不行了.慢慢来吧,有好多需要 ...

  3. Android开发学习笔记-自定义组合控件的过程

    自定义组合控件的过程 1.自定义一个View 一般来说,继承相对布局,或者线性布局 ViewGroup:2.实现父类的构造方法.一般来说,需要在构造方法里初始化自定义的布局文件:3.根据一些需要或者需 ...

  4. Android开发学习笔记-自定义组合控件

    为了能让代码能够更多的复用,故使用组合控件.下面是我正在写的项目中用到的方法. 1.先写要组合的一些需要的控件,将其封装到一个布局xml布局文件中. <?xml version="1. ...

  5. Android自己定义组件之日历控件-精美日历实现(内容、样式可扩展)

    需求 我们知道.Android系统本身有自带的日历控件,网络上也有非常多开源的日历控件资源.可是这些日历控件往往样式较单一.API较多.不易于在实际项目中扩展并实现出符合详细样式风格的,内容可定制的效 ...

  6. Android自己定义实现循环滚轮控件WheelView

    首先呈上效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  7. Android 手机卫士--自定义组合控件构件布局结构

    由于设置中心条目中的布局都很类似,所以可以考虑使用自定义组合控件来简化实现 本文地址:http://www.cnblogs.com/wuyudong/p/5909043.html,转载请注明源地址. ...

  8. Android中自定义组合控件

    Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...

  9. 自定义控件之--组合控件(titlebar)

    自定义控件相关知识从郭霖等大神身上学习,这里只不过加上自己的理解和实践,绝非抄袭.   组合控件是自定义控件中最简单的方式,但是是入门自定义控件和进阶的过程: 那么常见的组合控件有那些? 比如titl ...

随机推荐

  1. Visual Studio写Cuda代码

    1. 正常新建一个项目   2. 在项目中右键, build 选项中选择 CUDA 编译器   3. 项目属性中设置 CUDA 链接库 和 头文件 编译参数等   4. 完成     cu cuh 文 ...

  2. 鸟哥的Linux私房菜笔记第六章(一)

    目录与路径 相对路径与绝对路径 上一章简单的提到绝对路径和相对路径 绝对路径:路径的写法一定是由根目录(/)写起的,例如:/home/user 这个目录 相对路径:路径的写法不是由根目录(/)写起,例 ...

  3. golang图片压缩

    package main import (    //"encoding/json"    "fmt"    //"os"    //&qu ...

  4. python3遍历选中文件夹下的文件【GUI编程】

    功能介绍 如标题 使用截图 1.第一步:运行python程序 2.第二步:点击按钮选中文件夹 3.运行结束: 1. 显示选中文件夹路径 2. 遍历打印文件夹内文件 代码 import os #程序功能 ...

  5. ListView修改快速滑动的滑块

    1:如图 2: ListView加入快速滑动属性 <ListView android:id="@+id/listView" android:layout_width=&quo ...

  6. 配置DCOM中excel权限

    ASP.NET 导出Excel 错误解决备忘 网站项目要用到导出为excel文件的功能,程序运行时报错:"检索 COM 类工厂中 CLSID 为{000-0000-0000-C0046} 的 ...

  7. JeeSite 4.0 规划(二)

    ==== 点击放大查看 ==== ==== 点击放大查看 ====

  8. 第一个TensorFlow程序

    第一个TensorFlow程序 TensorFlow的运行方式分为如下4步: (1)加载数据及定义超参数 (2)构建网络 (3)训练模型 (4)评估模型和进行预测 import tensorflow ...

  9. (4.33)sql server2014内存数据库(内存中OLTP(In-Memory OLTP))

    查看文章:https://blog.51cto.com/ultrasql/1626514

  10. nyoj56-阶乘因式分解(一)

    56-阶乘因式分解(一) 内存限制:64MB时间限制:3000msSpecial Judge: No accepted:6submit:7 题目描述: 给定两个数m,n,其中m是一个素数. 将n(0& ...