Android 闪烁效果 blink标签

今天在看LayoutInflate源码的时候发现这样一个TAG,TAG_1995 = "blink".很是诧异.一般来说在写Xml布局文件的时候我们会使用SubView,merge,include,各种View标签等等.但是从来没见过blink标签.

 if (TAG_1995.equals(name)) {
    temp = new BlinkLayout(mContext, attrs);
     } else {
     temp = createViewFromTag(root, name, attrs);
    }

BlinkLayout是Layoutinflate中的一个private类型的内部类.继承自FrameLayout.可见它是一个容器.源码如下:

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);
        }
    }
}

接下来看一下如何使用,以及有什么效果:






    


直接加载这个布局,发现TextView会一闪一闪.而blink单词也是这个意思.闪烁频率500毫秒,不支持修改.

你可能感兴趣的:(Android 闪烁效果 blink标签)