美文网首页
Android 闪烁效果 blink标签

Android 闪烁效果 blink标签

作者: 大师鲁 | 来源:发表于2017-11-22 17:25 被阅读357次

今天在看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);
        }
    }
}

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

<?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>

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

相关文章

网友评论

      本文标题:Android 闪烁效果 blink标签

      本文链接:https://www.haomeiwen.com/subject/yjrovxtx.html