Android学习笔记_S01_E02 AnalogClock和DigitalClock(时钟)的功能和用法

本文介绍了如何在Android应用中实现简易的模拟时钟组件,包括AnalogClock和DigitalClock两个部分,详细解释了各组件的基本定义、属性及实现效果,并提供了源代码示例。

一、基本定义

  AnalogClock和DigitalClock是两个非常简单的组件,而DigitalClock本身就是文本框,只是它里面显示的内容是时间,但DigitalClock的android:text属性没什么作用。而AnalogClock则继承了View组件,重写了View的OnDraw方法,在View上绘制模拟时钟。

二、属性

   AnalogClock支持的XML属性的说明。

XML属性说明
android:dial设置该模拟时钟的表盘使用的图片
android:hand_hour设置该模拟时钟的时针表盘使用的图片
android:hand_minute设置该模拟时钟的分针使用的图片

三、实现效果

四、编写过程

  AnalogClock和DigitalClock的编写比较简单,只需要在XML文件中放入相应的控件的即可,在根据属性引用相关图片。

  1、在XML文件中新建DigitalClock控件。

1 <DigitalClock 
2         android:id="@+id/digitalClock"
3         android:layout_width="wrap_content"
4         android:layout_height="wrap_content"
5         android:textSize="30sp"
6         android:isScrollContainer="true"/>
View Code

  2、新建AnalogClock控件。dial属性控制表盘样式,通过“@Drawable/图片"找到;hand_hour和minute控制指针样式,通过"@Drawable/图片"找到。

1 <AnalogClock 
2         android:id="@+id/analogClock"
3         android:layout_width="wrap_content"
4         android:layout_height="wrap_content"
5         android:dial="@drawable/watch"
6         android:hand_hour="@drawable/hand"
7         android:hand_minute="@drawable/hand"/>
View Code

 五、源代码

  xml文件,其他代码不用变

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7 
 8     <DigitalClock 
 9         android:id="@+id/digitalClock"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:textSize="30sp"
13         android:isScrollContainer="true"/>
14     
15     <AnalogClock 
16         android:id="@+id/analogClock"
17         android:layout_width="wrap_content"
18         android:layout_height="wrap_content"
19         android:dial="@drawable/watch"
20         android:hand_hour="@drawable/hand"
21         android:hand_minute="@drawable/hand"/>
22 
23 </LinearLayout>
View Code 

转载于:https://www.cnblogs.com/Red-Shark/p/4130711.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值