新手引导的实现

    /**
     * 首页的新手引导
     */
    private fun checkShowUserGuide() {
        if (!PreferencesUtil.homeNewUserGuideHasShow) {
            home_avatar_parent.postDelayed ({
                home_avatar_parent?.let {
                    var location = IntArray(2)
                    it.getLocationOnScreen(location)
                    var safeInsetTop = 0 // 适配水滴屏
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                        it.rootWindowInsets.displayCutout?.let { cutout ->
                            safeInsetTop = cutout.safeInsetTop
                        }
                    }
                    Log.d("zivon", "checkShowUserGuide safeTop = $safeInsetTop, statusBarHeight = ${DeviceUtil.statusBarHeight}")
                    userGuideDialog = HomeNewUserGuideDialog(baseActivity, location[0], location[1] - safeInsetTop, it.width, it.height)
                    userGuideDialog?.show()
                    PreferencesUtil.homeNewUserGuideHasShow = true
                }
            }, 200)
        }
    }

 

 

class HomeNewUserGuideDialog(context: Context, left: Int, top: Int, width: Int, height: Int) : BaseDialog(context, R.style.AlertDialog), View.OnClickListener {

    private val rootView = LayoutInflater.from(context).inflate(R.layout.layout_home_new_user_guide_dialog, null, false)

    init {
        setContentView(rootView)
        rootView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        )
        window?.let {
            it.setLayout(DeviceUtil.screenSize.width, DeviceUtil.screenSize.height)
            it.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN) // 全屏布局
        }

        var offset = rootView.resources.getDimensionPixelSize(R.dimen.home_page_avatar_radio_width)
        (rootView.dialog_root.layoutParams as FrameLayout.LayoutParams).topMargin = top -  offset / 2
        (rootView.new_guide_image.layoutParams as ConstraintLayout.LayoutParams).width = width + offset
        (rootView.new_guide_image.layoutParams as ConstraintLayout.LayoutParams).height = height + offset
        Log.d("zivon", "HomeNewUserGuideDialog left = $left, top = $top, width = $width, height = $height")

        rootView.dialog_root.setOnClickListener(this)
        rootView.new_guide_image.setOnClickListener(this)
        rootView.new_guide_arrow.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.dialog_root, R.id.new_guide_image, R.id.new_guide_arrow -> dismiss()
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/dialog_root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/new_guide_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/home_new_user_guide_main"
        android:layout_marginStart="@dimen/main_fragment_item_margin_left"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/new_guide_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/home_new_user_guide_arrow"
        android:layout_marginTop="9.57dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/new_guide_image" />

    <TextView
        android:id="@+id/new_guide_tips"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/home_new_user_guide_tips"
        android:textSize="17.39dp"
        android:textStyle="bold"
        android:textColor="@color/white"
        android:layout_marginTop="9.57dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/new_guide_arrow" />

</androidx.constraintlayout.widget.ConstraintLayout>

主要思路是在界面上加个dialog,遮住需要引导的view。

getLocationOnScreen获取view的位置

点赞