Android自己定义View之组合控件 ---- LED数字时钟
先上图
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>
Android自己定义View之组合控件 ---- LED数字时钟的更多相关文章
- Android开发技巧——自定义控件之组合控件
Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...
- 【转】android UI进阶之自定义组合控件
[源地址]http://blog.csdn.net/notice520/article/details/6667827 好久没写博客了.实在是忙不过来,不过再不总结总结真的不行了.慢慢来吧,有好多需要 ...
- Android开发学习笔记-自定义组合控件的过程
自定义组合控件的过程 1.自定义一个View 一般来说,继承相对布局,或者线性布局 ViewGroup:2.实现父类的构造方法.一般来说,需要在构造方法里初始化自定义的布局文件:3.根据一些需要或者需 ...
- Android开发学习笔记-自定义组合控件
为了能让代码能够更多的复用,故使用组合控件.下面是我正在写的项目中用到的方法. 1.先写要组合的一些需要的控件,将其封装到一个布局xml布局文件中. <?xml version="1. ...
- Android自己定义组件之日历控件-精美日历实现(内容、样式可扩展)
需求 我们知道.Android系统本身有自带的日历控件,网络上也有非常多开源的日历控件资源.可是这些日历控件往往样式较单一.API较多.不易于在实际项目中扩展并实现出符合详细样式风格的,内容可定制的效 ...
- Android自己定义实现循环滚轮控件WheelView
首先呈上效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- Android 手机卫士--自定义组合控件构件布局结构
由于设置中心条目中的布局都很类似,所以可以考虑使用自定义组合控件来简化实现 本文地址:http://www.cnblogs.com/wuyudong/p/5909043.html,转载请注明源地址. ...
- Android中自定义组合控件
Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...
- 自定义控件之--组合控件(titlebar)
自定义控件相关知识从郭霖等大神身上学习,这里只不过加上自己的理解和实践,绝非抄袭. 组合控件是自定义控件中最简单的方式,但是是入门自定义控件和进阶的过程: 那么常见的组合控件有那些? 比如titl ...
随机推荐
- 关于作者&情况
本校第一次做信奥 , 如有错误, 见谅 本人之前从未接触编程, 选择信奥也只是因为怕被其他奥赛给淘汰... 这应该是懦弱吧...... 但自从接触编程以来, 虽然算不上极大的热爱, 但发自内心地喜欢它 ...
- docker(部署常见应用):docker部署redis
上节回顾:docker(部署常见应用):docker部署mysql docker部署redis:4.0 # 下载镜像 docker pull redis:4.0 # 查看下载镜像 docker ima ...
- centos6.5 + Nat网络模式 +SecureCRT 的相关设置
步骤1:先去查看子网掩码和子网ip 提示:打开后先不要关闭,后边还会使用 步骤2:查看本机名 输入: hostname 步骤3:修改本机名 vi /etc/sysconfig/network 在”Ho ...
- 模拟试题A
模拟试题A 一.单项选择题(2′*12=24′) 1.下面各种坐标变换中,会产生变换前后维度的改变的是( ) A)建模变换 B)观察变换 C)投影变换 D)视口变换 2.下列描述深度缓冲消隐算法的特点 ...
- Rx (Reactive Extensions)介绍
Reactive Extensions (Rx) 原来是由微软提出的一个综合了异步和基于事件驱动编程的库包,使用可观察序列和LINQ-style查询操作. 使用Rx, 开发者可以用Observable ...
- python的小数据池和深浅拷贝
小数据池 一种数据缓存机制,也称驻留机制 在同一代码块,相同的值不会开辟新的内存 特殊字符除外 小数据池只针对:在控制台时! 数字 :-5到256间的整数会被缓存 布尔值:都会缓存8 字符串 小于等于 ...
- MindManager 2019新版上市 ,了解一下!
所有的等待都是值得的!MindManager在蓄力一年后,给各位思维导图爱好者带来了全新的MindManager 2019 for Windows.全新的版本包含英语.德语.法语.俄语.中文.日语,新 ...
- Intellij IDEA中Mybatis Mapper自动注入警告的6种解决方案
点关注,不迷路:持续更新Java架构相关技术及资讯热文!!! 相信使用Mybaits的小伙伴们一定会经常编写类似如下的代码: 可以看到 userMapper 下有个红色警告.虽然代码本身并没有问题,能 ...
- Ubuntu Server 与 Ubuntu Desktop区别
今天有位朋友问我,Ubuntu Server 与 Ubuntu Desktop的区别在哪里!区别如下: SERVER没有GUI SERVER没有一堆的桌面软件 SERVER在编译时使用的参数不一样,会 ...
- 【BZOJ3065】带插入区间k小值
题意: 带插入,修改的区间k小值在线查询 原序列长度<=35000,插入个数<=35000,修改个数<=70000,0<=权值<=70000 题解: Orz vfleak ...