Android-Compose下常用方法&虚拟键盘监听

Android Compose可谓之新起之秀,用起来真棒,但是目前还在处于稳定初期,下面归纳了一些常用方法:


/** 获取字体大小的单位 **/
@Composable
@ReadOnlyComposable
fun fontDimensionResource(@DimenRes id: Int): TextUnit =
    with(LocalDensity.current) { dimensionResource(id).toSp() }

/** 绘制Drawable对象 **/
@Composable
fun drawablePainter(@DrawableRes id: Int): Painter =
    rememberDrawablePainter(AppCompatResources.getDrawable(LocalContext.current, id))

/** Px转Dip **/
@Composable
@ReadOnlyComposable
fun pixelToDip(pixel: Float): Dp = with(LocalDensity.current) { pixel.toDp() }

/** Px转Dip **/
@Composable
@ReadOnlyComposable
fun pixelToDip(pixel: Int): Dp = with(LocalDensity.current) { pixel.toDp() }

/** Dip转Px **/
@Composable
@ReadOnlyComposable
fun dipToPixel(dp: Dp): Float = with(LocalDensity.current) { dp.toPx() }

/** 获取ImageBitmap对象 **/
@Composable
@ReadOnlyComposable
fun imageBitmapResource(@DrawableRes id: Int) =
    ImageBitmap.imageResource(LocalContext.current.resources, id)

/** 无涟漪效果的点击事件 **/
@SuppressLint("UnnecessaryComposedModifier")
inline fun Modifier.noRippleClickable(crossinline onClick: () -> Unit): Modifier = composed {
    clickable(indication = null,
        interactionSource = remember { MutableInteractionSource() }) {
        onClick()
    }
}

另附一个虚拟键盘状态监听方法

/**
 * 给页面添加虚拟键盘可见性的监听事件, 沉浸式页面下使用
 * @param onKeyboardVisible 键盘是否显示的事件
 */
fun Activity.addInputKeyboardVisibleListener(
    rootContainer: View,
    onKeyboardVisible: (Boolean) -> Unit
) {
    val isKeyboardVisible = AtomicBoolean(false)
    val androidContentView = window.decorView.findViewById(Window.ID_ANDROID_CONTENT)
    val hasBottomNavigation = AtomicBoolean(false)
    var keyboardHeight = 0 //键盘高度
    window.decorView.setOnApplyWindowInsetsListener { _, windowInsets ->
        hasBottomNavigation.set(
            WindowInsetsCompat.toWindowInsetsCompat(windowInsets)
                .isVisible(WindowInsetsCompat.Type.navigationBars())
        )
        val insets = WindowInsetsCompat.toWindowInsetsCompat(windowInsets)
            .getInsets(WindowInsetsCompat.Type.ime())
        keyboardHeight = insets.bottom  //获取IME键盘的高度
        return@setOnApplyWindowInsetsListener windowInsets
    }
    androidContentView.viewTreeObserver.addOnGlobalLayoutListener {
        val decorViewRect = Rect()
        window.decorView.getWindowVisibleDisplayFrame(decorViewRect)
        val diffPadding = when {
            hasBottomNavigation.get() && keyboardHeight > 0 -> keyboardHeight
            hasBottomNavigation.get() && keyboardHeight == 0 -> bottomNavigationBarHeight
            else -> keyboardHeight
        }
        if (diffPadding > 0) {
            if (!isKeyboardVisible.getAndSet(true))
                onKeyboardVisible(true)
            if (diffPadding != rootContainer.paddingBottom)
                rootContainer.updatePadding(bottom = diffPadding)
        } else {
            if (rootContainer.paddingBottom != 0)
                rootContainer.updatePadding(bottom = 0)
            if (isKeyboardVisible.getAndSet(false))
                onKeyboardVisible(false)
        }
    }
}

你可能感兴趣的:(Android-Compose下常用方法&虚拟键盘监听)