易用版Popupwindow by Kotlin瞭解一下

概述

XPopupWindow,對系統的PopupWindow進行進一步封裝和增強以便於使用。採用Kotlin語言,提供了許多額外的功能方法例如設置彈窗位置,調整彈窗動畫等等。android

項目地址

XPopupWindowgit

預覽

XPopupWindow-demo

特性

  • 簡單快速地建立一個自定義彈窗
  • 以一種相對便捷的方式設置彈窗位置
  • 更加自由地調整你的彈窗動畫

開始

使用Gradle:github

allprojects {
    repositories {
        ...
	maven { url "https://jitpack.io" }
    }
}

dependencies {
    implementation 'com.github.XuDeveloper:XPopupWindow:1.0.1'
}
複製代碼

使用

以建立一個登陸彈窗爲例:express

界面編寫

(略,含有一個帳號輸入框,一個密碼輸入框以及登陸按鈕,github有demo)apache

建立XPopupWindow

/** * Created by Xu on 2018/6/17. * @author Xu */

class InputPopupWindow : XPopupWindow {
    private var btnLogin: Button? = null
    private var etPhone: TextInputEditText? = null

    constructor(ctx: Context) : super(ctx)

    constructor(ctx: Context, w: Int, h: Int) : super(ctx, w, h)

    /** * 設置popupwindow的layoutId */
    override fun getLayoutId(): Int {
        return R.layout.popup_input
    }

    /** * 設置layout的parentNodeId */
    override fun getLayoutParentNodeId(): Int {
        return R.id.input_parent
    }

    /** * 初始化界面 */
    override fun initViews() {
        btnLogin = findViewById(R.id.btn_login)
        btnLogin?.setOnClickListener { dismiss() }
        etPhone = findViewById(R.id.et_mobile)
    }

    /** * 初始化數據 */
    override fun initData() {
        // 設置彈窗背景透明度
        setShowingBackgroundAlpha(0.4f)
        // 彈窗彈出時自動獲取輸入框的焦點
        setAutoShowInput(etPhone, true)
    }

    /** * 爲彈窗設置彈出動畫,若是不想設置或是想經過xml方式設置,則設置返回值爲-1 */
    override fun startAnim(view: View): Animator? {
        var animatorX: ObjectAnimator = ObjectAnimator.ofFloat(view, "scaleX", 0f, 1f)
        var animatorY: ObjectAnimator = ObjectAnimator.ofFloat(view, "scaleY", 0f, 1f)
        var set = AnimatorSet()
        set.play(animatorX).with(animatorY)
        set.duration = 500
        return set
    }

    /** * 爲彈窗設置退出動畫,若是不想設置或是想經過xml方式設置,則設置返回值爲-1 */
    override fun exitAnim(view: View): Animator? {
        var animatorX: ObjectAnimator = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0f)
        var animatorY: ObjectAnimator = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0f)
        var set = AnimatorSet()
        set.play(animatorX).with(animatorY)
        set.duration = 700
        return set
    }

    /** * 經過xml方式設置動畫,xml編寫方法與原生popupwindow設置動畫方法相同 */
    override fun animStyle(): Int {
        return -1
    }

}

複製代碼

具體使用

private fun showInputPopup() {
    inputPopupWindow = InputPopupWindow(this, 1000, 600)
    // 可設置彈窗退出的監聽器,在回調中執行相應操做
    inputPopupWindow?.setXPopupDismissListener(object : XPopupWindowDismissListener {
        override fun xPopupBeforeDismiss() {
        }

        override fun xPopupAfterDismiss() {
            Snackbar.make(findViewById(android.R.id.content), "登陸成功!", Snackbar.LENGTH_LONG).show()
        }
    })
    inputPopupWindow?.showPopupFromScreenCenter(R.layout.activity_main)
}
複製代碼
  • 你能夠查看xpopupwindowdemo以獲取更多使用方法!

給我買杯檸檬茶唄 :smile:

微信 支付寶

協議

Copyright [2018] XuDeveloper

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
複製代碼
相關文章
相關標籤/搜索