今天在看LayoutInflate源碼的時(shí)候發(fā)現(xiàn)這樣一個(gè)TAG,TAG_1995 = "blink".很是詫異.一般來(lái)說(shuō)在寫(xiě)Xml布局文件的時(shí)候我們會(huì)使用SubView,merge,include,各種View標(biāo)簽等等.但是從來(lái)沒(méi)見(jiàn)過(guò)blink標(biāo)簽.
if (TAG_1995.equals(name)) {
temp = new BlinkLayout(mContext, attrs);
} else {
temp = createViewFromTag(root, name, attrs);
}
BlinkLayout是Layoutinflate中的一個(gè)private類(lèi)型的內(nèi)部類(lèi).繼承自FrameLayout.可見(jiàn)它是一個(gè)容器.源碼如下:
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) {
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);
}
}
}
接下來(lái)看一下如何使用,以及有什么效果:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cderg.cocc.myhandler.MainActivity">
<blink
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</blink>
</android.support.constraint.ConstraintLayout>
直接加載這個(gè)布局,發(fā)現(xiàn)TextView會(huì)一閃一閃.而blink單詞也是這個(gè)意思.閃爍頻率500毫秒,不支持修改.