模拟时钟(AnalogClock)
模拟时钟(AnalogClock)
显示一个带时钟和分针的表面
会随着时间的推移变化
常用属性:
android:dial
可以为表面提供一个自定义的图片
下面我们直接看代码:
1.Activity
//模拟时钟
public class AnalogClockActivity extends Activity { private TextView timeTextView;
private Handler handler; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.analog_clock);
timeTextView = (TextView) findViewById(R.id.timeTextViewId);
// Handler机制发送消息
handler = new Handler() {
public void handleMessage(Message msg) {
timeTextView.setText(currentTime());
}
};
// 设置时间
timeTextView.setText(currentTime());
new CurrentTimeThread(handler).start();
} private String currentTime() {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
//(month < 10 ? "0" + month : month)三目运算法
String currentTime = year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day) + " "
+ (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute) + ":"
+ (second < 10 ? "0" + second : second);
return currentTime;
} }
//开启一个子线程
class CurrentTimeThread extends Thread {
private Handler handler; public CurrentTimeThread(Handler handler) {
this.handler = handler;
} public void run() {
while (true) {
try {
Thread.sleep(1000);
handler.sendEmptyMessage(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" > <DigitalClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp" />
<!-- 模拟时钟 -->
<AnalogClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:dial="@drawable/biao_pan" /> <TextView
android:id="@+id/timeTextViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp" /> </LinearLayout>
3.效果图显示

模拟时钟(AnalogClock)的更多相关文章
- 模拟时钟(AnalogClock)和数字时钟(DigitalClock)
Demo2\clock_demo\src\main\res\layout\activity_main.xml <LinearLayout xmlns:android="http://s ...
- android脚步---数字时钟和模拟时钟
时钟UI组件是两个非常简单的组件,分为Digitalclock 和Analogclock, main.xml文件,书中程序有问题,加了两个组件,一个Button和一个<Chronometer ...
- Android学习笔记(20):时钟(AnalogClock和TextClock)和计时器(Chronometer)
时钟文本TextClock继承自TextView.是用于显示当前时间的文本框. TextClock支持的XML属性和相关方法 XML属性 相关方法 说明 android:format12Hour se ...
- Android控件 之 TextClock & AnalogClock(模拟时钟)
TextClock •简介 关于时间的文本显示,Android 提供了 DigitalClock 和 TextClock. DigitalClock是Android第1版本发布,功能很简单,只显示时间 ...
- 时钟AnalogClock和DigitalClock
<AnalogClock android:layout_width="fill_parent" android:layout_height="fill_parent ...
- 一个模拟时钟的时间选择器 ClockPicker
最近开发的一个模拟时钟的时间选择器 ClockPicker,用于 Bootstrap,或者单独作为一个 jQuery 插件. 源代码托管在 GitHub 上: ClockPicker 最近项目中需要用 ...
- Java多线程之sleep方法阻塞线程-模拟时钟
package org.study2.javabase.ThreadsDemo.status; import java.text.SimpleDateFormat; import java.util. ...
- css模拟时钟
css模拟时钟 思路: 画时钟数字(x,y)坐标 x = x0 + r*cos(deg) y = y0 + r*sin(deg) 知识点: 创建元素: createElement 添加元素: appe ...
- 【CSS3】纯CSS代码实现模拟时钟,+js对时功能。
使用CSS3纯代码来实现模拟时钟,及指针动画功能. 在这里主要使用到css3一些基本元素: border-radius:圆角边框,画圆形:表盘 Transform:变换,旋转,扭曲:刻度盘,指针形状 ...
随机推荐
- centos安装python与jdk
安装python #压缩包安装 [root@china ~]# yum -y install zlib* Loaded plugins: fastestmirror, refresh-packagek ...
- js创建对象方法
1.对象字面量 let xys={ name:'xys', age:22, height:177 } console.log(xys.age) //22 2.使用new操作符和构造函数 1 ...
- 怎样优化调整innodb_log_buffer_size
官方文档并没有直接告诉如何调整 innodb_log_buffer_size 大小, 根据对mysql 的状态信息了解知道 innodb_log_buffer_size 跟 Innodb_os_lo ...
- centos7.2 get pid by process name with python3.6
centos7.2 get pid by process name with python3.6 #-*- encoding:UTF-8 -*- import os import sys import ...
- (转)AIX rootvg 镜像创建与磁盘更换
# prtconf | grep disk # chdev -l hdisk1 -a pv=yes # extendvg rootvg hdisk1 # chvg -Qn rootvg # lsvg ...
- 江苏公务员职位表导入MySQL
USE `database`; /*Table structure for table `post` */ DROP TABLE IF EXISTS `post`; CREATE TABLE `pos ...
- 软件魔方制作系统启动盘并安装win10系统
不多说,直接上干货! 推荐软件:软件魔方 http://mofang.ruanmei.com/ 这里,我想说的是,这个软件来制作系统盘,是真的方便和好处多多.具体我不多说,本人也是用过其他的如大白菜 ...
- antlr4 接触
1. 配置IDE(可选) 2. 加减法示例 编写expr.g4 grammar AddMinus; expr: Minus Number #minusNum | expr op=('+'|'-') e ...
- java编程常用的快捷键
Eclipse 常用快捷键 Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键. 1. [ALT+/] 此快捷键为用户 ...
- ruby字符串连接
malls = Mall.allcount = 0malls.each do |mall| count += 1 if mall.parent_ids[0] province ...