【IT168技术】 针对Android底层View的直接构造很多网友没有实战经验,本次Android开发网结合目前平台开源代码一起通过AnalogClock类来理解View的直接继承。AnalogClock就是Home Screen上的那个带有两根指针的表盘类。它的实现我们直接从开源代码可以了解到:
1 public class AnalogClock extends View {
2 private Time mCalendar;
3
4 private Drawable mHourHand; //时针
5 private Drawable mMinuteHand; //分针
6 private Drawable mDial; //表盘背景
7
8 private int mDialWidth; //表盘宽度
9 private int mDialHeight; //表盘高度
10
11 private boolean mAttached; //附着状态
12
13 private final Handler mHandler = new Handler(); //定一个Handler类实现更新时间
14 private float mMinutes;
15 private float mHour;
16 private boolean mChanged; //时间是否改变
17
18 public AnalogClock(Context context) {
19 this(context, null);
20 }
21
22 public AnalogClock(Context context, AttributeSet attrs) {
23 this(context, attrs, 0);
24 }
25
26 public AnalogClock(Context context, AttributeSet attrs,
27 int defStyle) {
28 super(context, attrs, defStyle);
29 Resources r = mContext.getResources();
30 TypedArray a =
31 context.obtainStyledAttributes(
32 attrs, com.android.internal.R.styleable.AnalogClock, defStyle, 0);
33
34 mDial = a.getDrawable(com.android.internal.R.styleable.AnalogClock_dial); //加载表盘资源
35 if (mDial == null) {
36 mDial = r.getDrawable(com.android.internal.R.drawable.clock_dial);
37 }
38
39 mHourHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_hour); //加载时针图片资源
40 if (mHourHand == null) {
41 mHourHand = r.getDrawable(com.android.internal.R.drawable.clock_hand_hour);
42 }
43
44 mMinuteHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_minute); //加载分针图片
45 if (mMinuteHand == null) {
46 mMinuteHand = r.getDrawable(com.android.internal.R.drawable.clock_hand_minute);
47 }
48
49 mCalendar = new Time(); //获取当前系统时间
50
51 mDialWidth = mDial.getIntrinsicWidth(); //获取表盘图片的宽度
52 mDialHeight = mDial.getIntrinsicHeight(); //高度,同上
53 }
54
55 @Override
56 protected void onAttachedToWindow() {
57 super.onAttachedToWindow();
58
59 if (!mAttached) {
60 mAttached = true;
61 IntentFilter filter = new IntentFilter(); //注册一个消息过滤器,获取时间改变、时区改变的action
62
63 filter.addAction(Intent.ACTION_TIME_TICK);
64 filter.addAction(Intent.ACTION_TIME_CHANGED);
65 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
66
67 getContext().registerReceiver(mIntentReceiver, filter, null, mHandler);
68 }
69
70 mCalendar = new Time();
71
72 onTimeChanged();
73 }
74
75 @Override
76 protected void onDetachedFromWindow() {
77 super.onDetachedFromWindow();
78 if (mAttached) {
79 getContext().unregisterReceiver(mIntentReceiver); //反注册消息过滤器
80 mAttached = false;
81 }
82 }
83
84 @Override
85 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
86
87 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
88 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
89 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
90 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
91
92 float hScale = 1.0f;
93 float vScale = 1.0f;
94
95 if (widthMode != MeasureSpec.UNSPECIFIED && widthSize < mDialWidth) {
96 hScale = (float) widthSize / (float) mDialWidth;
97 }
98
99 if (heightMode != MeasureSpec.UNSPECIFIED && heightSize < mDialHeight) {
100 vScale = (float )heightSize / (float) mDialHeight;
101 }
102
103 float scale = Math.min(hScale, vScale);
104
105 setMeasuredDimension(resolveSize((int) (mDialWidth * scale), widthMeasureSpec),
106 resolveSize((int) (mDialHeight * scale), heightMeasureSpec));
107 }
108
109 @Override
110 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
111 super.onSizeChanged(w, h, oldw, oldh);
112 mChanged = true;
113 }
114
115 主要的绘图重写View的onDraw方法,我们可以看到通过canvas实例直接屏幕
116
117 @Override
118 protected void onDraw(Canvas canvas) {
119 super.onDraw(canvas);
120
121 boolean changed = mChanged;
122 if (changed) {
123 mChanged = false;
124 }
125
126 int availableWidth = mRight - mLeft;
127 int availableHeight = mBottom - mTop;
128
129 int x = availableWidth / 2;
130 int y = availableHeight / 2;
131
132 final Drawable dial = mDial;
133 int w = dial.getIntrinsicWidth();
134 int h = dial.getIntrinsicHeight();
135
136 boolean scaled = false;
137
138 if (availableWidth < w || availableHeight < h) {
139 scaled = true;
140 float scale = Math.min((float) availableWidth / (float) w,
141 (float) availableHeight / (float) h);
142 canvas.save();
143 canvas.scale(scale, scale, x, y);
144 }
145
146 if (changed) {
147 dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
148 }
149 dial.draw(canvas);
150
151 canvas.save();
152 canvas.rotate(mHour / 12.0f * 360.0f, x, y); //计算时针旋转的角度,android123提示就是那个时针图片的旋转角度,直接反应的就是表盘上那个针的时间
153 final Drawable hourHand = mHourHand;
154 if (changed) {
155 w = hourHand.getIntrinsicWidth();
156 h = hourHand.getIntrinsicHeight();
157 hourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
158 }
159 hourHand.draw(canvas);
160 canvas.restore();
161
162 canvas.save();
163 canvas.rotate(mMinutes / 60.0f * 360.0f, x, y); //同理,分针旋转的角度
164
165 final Drawable minuteHand = mMinuteHand;
166 if (changed) {
167 w = minuteHand.getIntrinsicWidth();
168 h = minuteHand.getIntrinsicHeight();
169 minuteHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
170 }
171 minuteHand.draw(canvas);
172 canvas.restore();
173
174 if (scaled) {
175 canvas.restore();
176 }
177 }
178
179 private void onTimeChanged() { //获取时间改变,计算当前的时分秒
180 mCalendar.setToNow();
181
182 int hour = mCalendar.hour;
183 int minute = mCalendar.minute;
184 int second = mCalendar.second;
185
186 mMinutes = minute + second / 60.0f;
187 mHour = hour + mMinutes / 60.0f;
188 mChanged = true;
189 }
190
191 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { //监听获取时间改变action
192 @Override
193 public void onReceive(Context context, Intent intent) {
194 if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
195 String tz = intent.getStringExtra("time-zone");
196 mCalendar = new Time(TimeZone.getTimeZone(tz).getID());
197 }
198
199 onTimeChanged(); //获取新的时间
200
201 invalidate(); //刷新屏幕,强制类调用onDraw方法实现分针时针的走动
202 }
203 };
204
2 private Time mCalendar;
3
4 private Drawable mHourHand; //时针
5 private Drawable mMinuteHand; //分针
6 private Drawable mDial; //表盘背景
7
8 private int mDialWidth; //表盘宽度
9 private int mDialHeight; //表盘高度
10
11 private boolean mAttached; //附着状态
12
13 private final Handler mHandler = new Handler(); //定一个Handler类实现更新时间
14 private float mMinutes;
15 private float mHour;
16 private boolean mChanged; //时间是否改变
17
18 public AnalogClock(Context context) {
19 this(context, null);
20 }
21
22 public AnalogClock(Context context, AttributeSet attrs) {
23 this(context, attrs, 0);
24 }
25
26 public AnalogClock(Context context, AttributeSet attrs,
27 int defStyle) {
28 super(context, attrs, defStyle);
29 Resources r = mContext.getResources();
30 TypedArray a =
31 context.obtainStyledAttributes(
32 attrs, com.android.internal.R.styleable.AnalogClock, defStyle, 0);
33
34 mDial = a.getDrawable(com.android.internal.R.styleable.AnalogClock_dial); //加载表盘资源
35 if (mDial == null) {
36 mDial = r.getDrawable(com.android.internal.R.drawable.clock_dial);
37 }
38
39 mHourHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_hour); //加载时针图片资源
40 if (mHourHand == null) {
41 mHourHand = r.getDrawable(com.android.internal.R.drawable.clock_hand_hour);
42 }
43
44 mMinuteHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_minute); //加载分针图片
45 if (mMinuteHand == null) {
46 mMinuteHand = r.getDrawable(com.android.internal.R.drawable.clock_hand_minute);
47 }
48
49 mCalendar = new Time(); //获取当前系统时间
50
51 mDialWidth = mDial.getIntrinsicWidth(); //获取表盘图片的宽度
52 mDialHeight = mDial.getIntrinsicHeight(); //高度,同上
53 }
54
55 @Override
56 protected void onAttachedToWindow() {
57 super.onAttachedToWindow();
58
59 if (!mAttached) {
60 mAttached = true;
61 IntentFilter filter = new IntentFilter(); //注册一个消息过滤器,获取时间改变、时区改变的action
62
63 filter.addAction(Intent.ACTION_TIME_TICK);
64 filter.addAction(Intent.ACTION_TIME_CHANGED);
65 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
66
67 getContext().registerReceiver(mIntentReceiver, filter, null, mHandler);
68 }
69
70 mCalendar = new Time();
71
72 onTimeChanged();
73 }
74
75 @Override
76 protected void onDetachedFromWindow() {
77 super.onDetachedFromWindow();
78 if (mAttached) {
79 getContext().unregisterReceiver(mIntentReceiver); //反注册消息过滤器
80 mAttached = false;
81 }
82 }
83
84 @Override
85 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
86
87 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
88 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
89 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
90 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
91
92 float hScale = 1.0f;
93 float vScale = 1.0f;
94
95 if (widthMode != MeasureSpec.UNSPECIFIED && widthSize < mDialWidth) {
96 hScale = (float) widthSize / (float) mDialWidth;
97 }
98
99 if (heightMode != MeasureSpec.UNSPECIFIED && heightSize < mDialHeight) {
100 vScale = (float )heightSize / (float) mDialHeight;
101 }
102
103 float scale = Math.min(hScale, vScale);
104
105 setMeasuredDimension(resolveSize((int) (mDialWidth * scale), widthMeasureSpec),
106 resolveSize((int) (mDialHeight * scale), heightMeasureSpec));
107 }
108
109 @Override
110 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
111 super.onSizeChanged(w, h, oldw, oldh);
112 mChanged = true;
113 }
114
115 主要的绘图重写View的onDraw方法,我们可以看到通过canvas实例直接屏幕
116
117 @Override
118 protected void onDraw(Canvas canvas) {
119 super.onDraw(canvas);
120
121 boolean changed = mChanged;
122 if (changed) {
123 mChanged = false;
124 }
125
126 int availableWidth = mRight - mLeft;
127 int availableHeight = mBottom - mTop;
128
129 int x = availableWidth / 2;
130 int y = availableHeight / 2;
131
132 final Drawable dial = mDial;
133 int w = dial.getIntrinsicWidth();
134 int h = dial.getIntrinsicHeight();
135
136 boolean scaled = false;
137
138 if (availableWidth < w || availableHeight < h) {
139 scaled = true;
140 float scale = Math.min((float) availableWidth / (float) w,
141 (float) availableHeight / (float) h);
142 canvas.save();
143 canvas.scale(scale, scale, x, y);
144 }
145
146 if (changed) {
147 dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
148 }
149 dial.draw(canvas);
150
151 canvas.save();
152 canvas.rotate(mHour / 12.0f * 360.0f, x, y); //计算时针旋转的角度,android123提示就是那个时针图片的旋转角度,直接反应的就是表盘上那个针的时间
153 final Drawable hourHand = mHourHand;
154 if (changed) {
155 w = hourHand.getIntrinsicWidth();
156 h = hourHand.getIntrinsicHeight();
157 hourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
158 }
159 hourHand.draw(canvas);
160 canvas.restore();
161
162 canvas.save();
163 canvas.rotate(mMinutes / 60.0f * 360.0f, x, y); //同理,分针旋转的角度
164
165 final Drawable minuteHand = mMinuteHand;
166 if (changed) {
167 w = minuteHand.getIntrinsicWidth();
168 h = minuteHand.getIntrinsicHeight();
169 minuteHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
170 }
171 minuteHand.draw(canvas);
172 canvas.restore();
173
174 if (scaled) {
175 canvas.restore();
176 }
177 }
178
179 private void onTimeChanged() { //获取时间改变,计算当前的时分秒
180 mCalendar.setToNow();
181
182 int hour = mCalendar.hour;
183 int minute = mCalendar.minute;
184 int second = mCalendar.second;
185
186 mMinutes = minute + second / 60.0f;
187 mHour = hour + mMinutes / 60.0f;
188 mChanged = true;
189 }
190
191 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { //监听获取时间改变action
192 @Override
193 public void onReceive(Context context, Intent intent) {
194 if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
195 String tz = intent.getStringExtra("time-zone");
196 mCalendar = new Time(TimeZone.getTimeZone(tz).getID());
197 }
198
199 onTimeChanged(); //获取新的时间
200
201 invalidate(); //刷新屏幕,强制类调用onDraw方法实现分针时针的走动
202 }
203 };
204
看了本例根据,Android开发很简单吧,感兴趣的网友可以为本程序加入一个秒针,不过Android123提醒网友的是可能对于电池,以及系统运行效率产生一定的影响,不过作为练习大家可以试一试。