Android 使用 WindowManager 打造懸浮通知

最近項(xiàng)目中遇到使用通知欄的功能,開始使用Notification的時(shí)候,發(fā)現(xiàn)存在兼容問(wèn)題,于是換成了
WindowManager , 發(fā)現(xiàn) WindowManager 能夠很好的解決問(wèn)題。
先看下效果圖

微信圖片_20171212150512.png

以下是詳細(xì)內(nèi)容 ps:代碼用的Kotlin

首先布局文件

<android.support.v7.widget.CardView 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:id="@+id/windowFloatContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/layout_middle_margin"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:fitsSystemWindows="true"
    app:cardBackgroundColor="@color/orange_bg"
    app:cardCornerRadius="10dp"
    app:contentPadding="@dimen/layout_middle_padding">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:lineSpacingExtra="5dp"
            android:padding="@dimen/layout_normal_padding"
            android:text="實(shí)時(shí)\n從茶園魯能領(lǐng)秀城出發(fā)到光電園鳳凰C座"
            android:textColor="@color/white"
            android:textSize="@dimen/middle_text_size"
            tools:ignore="HardcodedText" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/layout_large_margin"
            android:layout_marginRight="@dimen/layout_large_margin">

            <android.support.v7.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:textColor="@color/white"
                android:padding="@dimen/layout_small_padding"
                android:textSize="@dimen/normal_text_size"
                android:text="距您:0.86公里"/>

            <android.support.v7.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|right"
                android:padding="@dimen/layout_small_padding"
                android:textColor="@color/white"
                android:textSize="@dimen/normal_text_size"
                android:text="全程:18.65公里"/>

        </FrameLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>

其次 在Application中創(chuàng)建 WindowManager.LayoutParams

class App : Application() {

    companion object {
        private var wmParams: WindowManager.LayoutParams? = null

        fun getWindowLayoutParams(): WindowManager.LayoutParams? = wmParams
    }

    override fun onCreate() {
        super.onCreate()
        wmParams = WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PRIORITY_PHONE,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT)
    }

   ...

}

然后在Activity中使用

class MainActivity : BaseActivity() {

    private var mWindowManager: WindowManager? = null
    private var mWindowParams: WindowManager.LayoutParams? = null
    private var floatView: View? = null

    override fun getLayoutId(): Int = R.layout.activity_main

    override fun initView() {
    }

    override fun onViewClick() {
        super.onViewClick()
        RxView.clicks(mainNotifyBtn)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribeBy(onNext = {
                    showWindowAlert()
                }, onError = {

                })
        RxView.clicks(mainCancelNotifyBtn)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribeBy(onNext = {
                        hideWindowAlert()
                }, onError = {

                })
    }

    override fun isSupportBack(): Boolean = false

    override fun getToolBarTitle(): String? = "San"

    override fun processLogic() {

    }

    private fun showWindowAlert() {
        mWindowManager = applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        mWindowParams = App.getWindowLayoutParams()
        floatView = layoutInflater.inflate(R.layout.ic_window_float, null)

//        mWindowParams!!.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
//        mWindowParams!!.format = 1
//        mWindowParams!!.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE // 不能搶占聚焦點(diǎn)
//        mWindowParams!!.flags = mWindowParams!!.flags or WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
//        mWindowParams!!.flags = mWindowParams!!.flags or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS // 排版不受限制
//        mWindowParams!!.flags = mWindowParams!!.flags or WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS //
//        mWindowParams!!.alpha = 0F
        mWindowParams!!.gravity = Gravity.TOP

        mWindowParams!!.x = 0
        mWindowParams!!.y = 0

        mWindowParams!!.verticalMargin = 0F
        mWindowParams!!.horizontalMargin = 15F

//        mWindowParams!!.width = floatView!!.layoutParams.width
//        mWindowParams!!.height = floatView!!.layoutParams.height

        mWindowManager!!.addView(floatView, mWindowParams)
    }

    private fun hideWindowAlert() {
        if (mWindowManager != null)
            mWindowManager!!.removeViewImmediate(floatView)
    }

    override fun onDestroy() {
        super.onDestroy()
        hideWindowAlert()
    }
}

可以根據(jù)實(shí)際功能需求,給FloatView添加動(dòng)畫
按照上面的代碼做,功能就能實(shí)現(xiàn)了。謝謝!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容