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

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

<?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 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,417评论 25 709
  • 本文针对include、merge、ViewStub三个标签如何在布局复用、有效减少布局层级以及如何可以按需加载三...
    justin_pan阅读 10,959评论 2 4
  • 在开发过程中,往往会听到 “性能优化” 这个概念,这个概念很大,比如网络性能优化、耗电量优化等等,对我们开发者而言...
    湫水长天阅读 4,881评论 0 4
  • 太长不看版:在 Android UI 布局过程中,遵守一些惯用、有效的布局原则,可以制作出高效且复用性高的 UI。...
    Mupceet阅读 9,309评论 0 14
  • 关于元旦,王安石有一首名诗《元日》:“爆竹声中一岁除,春风送暖入屠苏。千门万户曈曈日,总把新桃换旧符。”诗中说的是...
    章雪峰阅读 4,790评论 3 9