Android中的xml布局的解析是在LayoutInflater中進(jìn)行的快毛,
LayoutInflater.java
// 這里定義的blink標(biāo)簽
private static final String TAG_1995 = "blink";
LayoutInflater在解析到blink
標(biāo)簽時(shí)直接返回一個(gè)BlinkLayout
對(duì)象
private static class BlinkLayout extends FrameLayout {
private static final int MESSAGE_BLINK = 0x42;
private static final int BLINK_DELAY = 500;
private boolean mBlink;
private boolean mBlinkState;
private final Handler mHandler;
public BlinkLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == MESSAGE_BLINK) {
if (mBlink) {
// 不斷的取反,重復(fù)操作
mBlinkState = !mBlinkState;
makeBlink();
}
invalidate();
return true;
}
return false;
}
});
}
private void makeBlink() {
Message message = mHandler.obtainMessage(MESSAGE_BLINK);
mHandler.sendMessageDelayed(message, BLINK_DELAY);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mBlink = true;
mBlinkState = true;
makeBlink();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mBlink = false;
mBlinkState = true;
mHandler.removeMessages(MESSAGE_BLINK);
}
@Override
protected void dispatchDraw(Canvas canvas) {
if (mBlinkState) {
super.dispatchDraw(canvas);
}
}
}
BlinkLayout
的作用是每隔500ms繪制刷新一次盛险,也就形成了500ms閃爍的效果僧家,有點(diǎn)類(lèi)似在EditText中光標(biāo)不斷的閃爍效果雀摘。