For investors
股價(jià):
5.36 美元 %For investors
股價(jià):
5.36 美元 %認(rèn)真做教育 專心促就業(yè)
針對(duì)Android底層View的直接構(gòu)造很多網(wǎng)友沒有實(shí)戰(zhàn)經(jīng)驗(yàn),本次Android開發(fā)網(wǎng)結(jié)合目前平臺(tái)開源代碼一起通過AnalogClock類 來理解View的直接繼承。AnalogClock就是Home Screen上的那個(gè)帶有兩根指針的表盤類。它的實(shí)現(xiàn)我們直接從開源代碼可以了解到:
public class AnalogClock extends View {
private Time mCalendar;
private Drawable mHourHand; //時(shí)針
private Drawable mMinuteHand; //分針
private Drawable mDial; //表盤背景
private int mDialWidth; //表盤寬度
private int mDialHeight; //表盤高度
private boolean mAttached; //附著狀態(tài)
private final Handler mHandler = new Handler(); //定一個(gè)Handler類實(shí)現(xiàn)更新時(shí)間
private float mMinutes;
private float mHour;
private boolean mChanged; //時(shí)間是否改變
public AnalogClock(Context context) {
this(context, null);
}
public AnalogClock(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AnalogClock(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
Resources r = mContext.getResources();
TypedArray a =
context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.AnalogClock, defStyle, 0);
mDial = a.getDrawable(com.android.internal.R.styleable.AnalogClock_dial); //加載表盤資源
if (mDial == null) {
mDial = r.getDrawable(com.android.internal.R.drawable.clock_dial);
}
mHourHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_hour); //加載時(shí)針圖片資源
if (mHourHand == null) {
mHourHand = r.getDrawable(com.android.internal.R.drawable.clock_hand_hour);
}
mMinuteHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_minute); //加載分針圖片
if (mMinuteHand == null) {
mMinuteHand = r.getDrawable(com.android.internal.R.drawable.clock_hand_minute);
}
mCalendar = new Time(); //獲取當(dāng)前系統(tǒng)時(shí)間
mDialWidth = mDial.getIntrinsicWidth(); //獲取表盤圖片的寬度
mDialHeight = mDial.getIntrinsicHeight(); //高度,同上
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!mAttached) {
mAttached = true;
IntentFilter filter = new IntentFilter(); //注冊(cè)一個(gè)消息過濾器,獲取時(shí)間改變、時(shí)區(qū)改變的action
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
getContext().registerReceiver(mIntentReceiver, filter, null, mHandler);
}
mCalendar = new Time();
onTimeChanged();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mAttached) {
getContext().unregisterReceiver(mIntentReceiver); //反注冊(cè)消息過濾器
mAttached = false;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
float hScale = 1.0f;
float vScale = 1.0f;
if (widthMode != MeasureSpec.UNSPECIFIED && widthSize < mDialWidth) {
hScale = (float) widthSize / (float) mDialWidth;
}
if (heightMode != MeasureSpec.UNSPECIFIED && heightSize < mDialHeight) {
vScale = (float )heightSize / (float) mDialHeight;
}
float scale = Math.min(hScale, vScale);
setMeasuredDimension(resolveSize((int) (mDialWidth * scale), widthMeasureSpec),
resolveSize((int) (mDialHeight * scale), heightMeasureSpec));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mChanged = true;
}
主要的繪圖重寫View的onDraw方法,我們可以看到通過canvas實(shí)例直接屏幕
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
boolean changed = mChanged;
if (changed) {
mChanged = false;
}
int availableWidth = mRight - mLeft;
int availableHeight = mBottom - mTop;
int x = availableWidth / 2;
int y = availableHeight / 2;
final Drawable dial = mDial;
int w = dial.getIntrinsicWidth();
int h = dial.getIntrinsicHeight();
boolean scaled = false;
if (availableWidth < w || availableHeight < h) {
scaled = true;
float scale = Math.min((float) availableWidth / (float) w,
(float) availableHeight / (float) h);
canvas.save();
canvas.scale(scale, scale, x, y);
}
if (changed) {
dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
dial.draw(canvas);
canvas.save();
canvas.rotate(mHour / 12.0f * 360.0f, x, y); //計(jì)算時(shí)針旋轉(zhuǎn)的角度,android123提示就是那個(gè)時(shí)針圖片的旋轉(zhuǎn)角度,直接反應(yīng)的就是表盤上那個(gè)針的時(shí)間
final Drawable hourHand = mHourHand;
if (changed) {
w = hourHand.getIntrinsicWidth();
h = hourHand.getIntrinsicHeight();
hourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
hourHand.draw(canvas);
canvas.restore();
canvas.save();
canvas.rotate(mMinutes / 60.0f * 360.0f, x, y); //同理,分針旋轉(zhuǎn)的角度
final Drawable minuteHand = mMinuteHand;
if (changed) {
w = minuteHand.getIntrinsicWidth();
h = minuteHand.getIntrinsicHeight();
minuteHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
minuteHand.draw(canvas);
canvas.restore();
if (scaled) {
canvas.restore();
}
}
private void onTimeChanged() { //獲取時(shí)間改變,計(jì)算當(dāng)前的時(shí)分秒
mCalendar.setToNow();
int hour = mCalendar.hour;
int minute = mCalendar.minute;
int second = mCalendar.second;
mMinutes = minute + second / 60.0f;
mHour = hour + mMinutes / 60.0f;
mChanged = true;
}
private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { //監(jiān)聽獲取時(shí)間改變action
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
String tz = intent.getStringExtra("time-zone");
mCalendar = new Time(TimeZone.getTimeZone(tz).getID());
}
onTimeChanged(); //獲取新的時(shí)間
invalidate(); //刷新屏幕,強(qiáng)制類調(diào)用onDraw方法實(shí)現(xiàn)分針時(shí)針的走動(dòng)
}
};
看了本例根據(jù),Android開發(fā)很簡單吧,感興趣的網(wǎng)友可以為本程序加入一個(gè)秒針,不過Android123提醒網(wǎng)友的是可能對(duì)于電池,以及系統(tǒng)運(yùn)行效 率產(chǎn)生一定的影響,不過作為練習(xí)大家可以試一試。
【免責(zé)聲明】本文部分系轉(zhuǎn)載,轉(zhuǎn)載目的在于傳遞更多信息,并不代表本網(wǎng)贊同其觀點(diǎn)和對(duì)其真實(shí)性負(fù)責(zé)。如涉及作品內(nèi)容、版權(quán)和其它問題,請(qǐng)?jiān)?0日內(nèi)與聯(lián)系我們,我們會(huì)予以更改或刪除相關(guān)文章,以保證您的權(quán)益!