Guest User

Untitled

a guest
Nov 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. package com.github.jasonhezz.likesplash.util
  2.  
  3. import android.content.Context
  4. import android.graphics.Rect
  5. import android.os.Build
  6. import android.support.v4.view.ViewCompat
  7. import android.support.v4.view.WindowInsetsCompat
  8. import android.util.AttributeSet
  9. import android.view.Gravity
  10. import android.view.View
  11. import android.widget.FrameLayout
  12.  
  13.  
  14. /**
  15. * Created by JavaCoder on 2017/11/14.
  16. */
  17. class WindowInsetsFrameLayout @JvmOverloads constructor(context: Context,
  18. attrs: AttributeSet? = null,
  19. defStyleAttr: Int = 0,
  20. defStyleRes: Int = 0) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) {
  21.  
  22. init {
  23. ViewCompat.setOnApplyWindowInsetsListener(this
  24. ) { v, insets -> setWindowInsets(insets) }
  25. }
  26.  
  27. private fun setWindowInsets(insets: WindowInsetsCompat): WindowInsetsCompat {
  28. if (Build.VERSION.SDK_INT >= 21 && insets.hasSystemWindowInsets()) {
  29. if (applySystemWindowInsets21(insets)) {
  30. return insets.consumeSystemWindowInsets()
  31. }
  32. }
  33. return insets
  34. }
  35.  
  36. override protected fun fitSystemWindows(insets: Rect): Boolean {
  37. return if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
  38. applySystemWindowInsets19(insets)
  39. } else super.fitSystemWindows(insets)
  40. }
  41.  
  42. private fun applySystemWindowInsets19(insets: Rect): Boolean {
  43. var consumed = false
  44. for (i in 0 until childCount) {
  45. val child = getChildAt(i)
  46. if (!child.fitsSystemWindows) {
  47. continue
  48. }
  49. val childInsets = Rect(insets)
  50. computeInsetsWithGravity(child, childInsets)
  51. child.setPadding(childInsets.left, childInsets.top, childInsets.right, childInsets.bottom)
  52. consumed = true
  53. }
  54. return consumed
  55. }
  56.  
  57. private fun applySystemWindowInsets21(insets: WindowInsetsCompat): Boolean {
  58. var consumed = false
  59. for (i in 0 until childCount) {
  60. val child = getChildAt(i)
  61. if (!child.fitsSystemWindows) {
  62. continue
  63. }
  64. val childInsets = Rect(
  65. insets.systemWindowInsetLeft,
  66. insets.systemWindowInsetTop,
  67. insets.systemWindowInsetRight,
  68. insets.systemWindowInsetBottom)
  69. computeInsetsWithGravity(child, childInsets)
  70. ViewCompat.dispatchApplyWindowInsets(child, insets.replaceSystemWindowInsets(childInsets))
  71. consumed = true
  72. }
  73. return consumed
  74. }
  75.  
  76. private fun computeInsetsWithGravity(view: View, insets: Rect) {
  77. val lp = view.getLayoutParams() as FrameLayout.LayoutParams
  78. var gravity = lp.gravity
  79. /**
  80. * 因为该方法执行时机早于 FrameLayout.layoutChildren,
  81. * 而在 {FrameLayout#layoutChildren} 中当 gravity == -1 时会设置默认值为 Gravity.TOP | Gravity.LEFT,
  82. * 所以这里也要同样设置
  83. */
  84. if (gravity == -1) {
  85. gravity = Gravity.TOP or Gravity.START
  86. }
  87. if (lp.width != FrameLayout.LayoutParams.MATCH_PARENT) {
  88. val horizontalGravity = gravity and Gravity.HORIZONTAL_GRAVITY_MASK
  89. when (horizontalGravity) {
  90. Gravity.START -> insets.right = 0
  91. Gravity.END -> insets.left = 0
  92. }
  93. }
  94. if (lp.height != FrameLayout.LayoutParams.MATCH_PARENT) {
  95. val verticalGravity = gravity and Gravity.VERTICAL_GRAVITY_MASK
  96. when (verticalGravity) {
  97. Gravity.TOP -> insets.bottom = 0
  98. Gravity.BOTTOM -> insets.top = 0
  99. }
  100. }
  101. }
  102.  
  103. /*@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
  104. override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets {
  105. val childCount = childCount
  106. for (index in 0 until childCount)
  107. getChildAt(index).dispatchApplyWindowInsets(insets)
  108. return insets
  109. }*/
  110. }
Add Comment
Please, Sign In to add comment