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 ...
随机推荐
- C#解除某类警告。。。。。。。。。。
C#预处理器指令取消不必要的警告 今天将自己写的一个类库生成一个DLL后,想把注释也加进去.... 方法:在属性->生成选项卡->XML文档文件(勾选)(生成的文件名不能修改,使用时必须跟 ...
- VCRuntime静默安装
批处理脚本: "%~dp0\VC_X64Runtime\VC_x64Runtime.exe" /q"%~dp0\VC_X86Runtime\VC_X86Runtime.e ...
- unity3d 鼠标事件
using UnityEngine; using System.Collections; public class mouse : MonoBehaviour { //private Vector3 ...
- 在windows系统下虚拟机和ubuntu系统的安装和卸载
一.安装 之前有装过双系统,但是吧,一直用ubuntu系统,很久没进windows之后的某一天,自己再进windows,发现windows系统崩了,我也不知道为什么,找了很多方法没有解决,最后只好重装 ...
- 高德地图修改gps定位点样式
效果图 navi_map_gps_locked.png 图片1 图片2 修改 MyLocationStyle myLocationStyle = new MyLocationStyle();//初始化 ...
- 01-初学总结之《谭浩强C程序设计》
注:个人针对于课本的易错点进行了相关的整理.整理的不专业,多多见谅. C语言中的易出错的点 这个笔记综合了 0. 常量&变量 常量 整型常量 -345,1000,0 实型常量 1) 十进制 ...
- springboot-注解讲解
@Configuration:声明我们JdbcConfig是一个配置类 @PropertySource:指定属性文件的路径是:classpath:jdbc.properties 通过@Value为属性 ...
- The socket is closed!
关闭mongodb /usr/local/app/mongidb//bin/mongod --shutdown --dbpath /usr/local/data/mongo/data/ 然 ...
- 【Git教程】Git教程及使用命令
Git是目前世界上最先进的分布式版本控制系统,可以自动记录和管理文件的改动,还可以团队写作编辑,也就是帮助我们对不同的版本进行控制.2008年,GitHub网站上线,为开源项目提供免费存储,迅速发 ...
- 容器化haproxy+keepalived
# 拉取haproxy镜像 docker pull haproxy:1.7.8-alpine mkdir /etc/haproxy cat >/etc/haproxy/haproxy.cfg&l ...