Skip to content

Commit

Permalink
修复ImmersiveDialog相关问题
Browse files Browse the repository at this point in the history
span库支持textTypeface配置
  • Loading branch information
liuzhentao committed Oct 12, 2023
1 parent 8defc8c commit 9b5f6ed
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 29 deletions.
52 changes: 30 additions & 22 deletions immersive/src/main/java/com/arc/fast/immersive/ImmersiveDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ class ImmersiveDialogBackground(
private val backgroundView: View
private val navigationBarView: View?
private val animator: ObjectAnimator
private val windowManager: WindowManager by lazy {
dialogFragment.activity?.getSystemService(Context.WINDOW_SERVICE) as WindowManager
private val windowManager: WindowManager? by lazy {
dialogFragment.activity?.getSystemService(Context.WINDOW_SERVICE) as? WindowManager
}
private val isContainerReleased: Boolean get() = dialogFragment.activity.let { it?.isFinishing == true || it?.isDestroyed == true }
private val isContainerReleased: Boolean get() = dialogFragment.activity.let { it == null || it.isFinishing || it.isDestroyed }

private val currentIsAppearanceLightNavigationBars =
!dialogConfig.isLightNavigationBarForegroundColor
Expand Down Expand Up @@ -297,11 +297,14 @@ class ImmersiveDialogBackground(
.setDuration(300).apply {
addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
Log.i("ImmersiveDialog", "onAnimationEnd isContainerReleased:" + isContainerReleased)
Log.i(
"ImmersiveDialog",
"onAnimationEnd isContainerReleased:" + isContainerReleased
)
if (backgroundView.alpha == 0f && rootView.windowToken != null) {
try {
rootView.isVisible = false
windowManager.removeView(rootView)
windowManager?.removeView(rootView)
} catch (e: Exception) {
e.printStackTrace()
}
Expand Down Expand Up @@ -370,24 +373,29 @@ class ImmersiveDialogBackground(
parentWindowController?.isAppearanceLightStatusBars
}
}
windowManager.addView(
rootView,
WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_APPLICATION_PANEL,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,
PixelFormat.TRANSLUCENT
).apply {
token = parentWindow?.decorView?.windowToken
gravity = Gravity.CENTER
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
fitInsetsTypes = fitInsetsTypes and WindowInsetsCompat.Type.systemBars().inv()
try {
windowManager?.addView(
rootView,
WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_APPLICATION_PANEL,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,
PixelFormat.TRANSLUCENT
).apply {
token = parentWindow?.decorView?.windowToken
gravity = Gravity.CENTER
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
fitInsetsTypes =
fitInsetsTypes and WindowInsetsCompat.Type.systemBars().inv()
}
}
}
)
)
} catch (e: WindowManager.BadTokenException) {
e.printStackTrace()
}
// 显示时尝试设置背景
dialogFragment.dialog?.setOnShowListener {
executeShowAnimator()
Expand Down
32 changes: 29 additions & 3 deletions sample/src/main/java/com/arc/fast/sample/span/SpanFragment.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package com.arc.fast.sample.span

import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.arc.fast.core.extensions.resToColor
import com.arc.fast.core.extensions.dpToPx
import com.arc.fast.core.extensions.resToColor
import com.arc.fast.core.extensions.spToPx
import com.arc.fast.sample.BaseFragment
import com.arc.fast.sample.R
import com.arc.fast.sample.databinding.FragmentSpanBinding
import com.arc.fast.span.*
import com.arc.fast.span.FastTextWrapSpan
import com.arc.fast.span.appendFastImageStyle
import com.arc.fast.span.appendFastSpacing
import com.arc.fast.span.appendFastSpan
import com.arc.fast.span.appendFastTextStyle
import com.arc.fast.span.enableClickableSpan

class SpanFragment : BaseFragment<FragmentSpanBinding>() {

Expand Down Expand Up @@ -47,6 +53,22 @@ class SpanFragment : BaseFragment<FragmentSpanBinding>() {
borderColor = R.color.main.resToColor,
textSize = 12f.spToPx,
textColor = R.color.main.resToColor,
textTypeface = Typeface.create(Typeface.DEFAULT,Typeface.ITALIC),
textRightMargin = 6f.dpToPx,
topPadding = 2f.dpToPx,
bottomPadding = 2f.dpToPx,
leftPadding = 6f.dpToPx,
rightPadding = 6f.dpToPx
)
)
spannableStringBuilder.appendFastSpan(
"满99元减10元", FastTextWrapSpan(
radius = 4f.dpToPx,
borderSize = 1f.dpToPx,
borderColor = R.color.main.resToColor,
textSize = 12f.spToPx,
textColor = R.color.main.resToColor,
textTypeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD),
textRightMargin = 6f.dpToPx,
topPadding = 2f.dpToPx,
bottomPadding = 2f.dpToPx,
Expand All @@ -63,7 +85,11 @@ class SpanFragment : BaseFragment<FragmentSpanBinding>() {
underlineColor = Color.TRANSPARENT
setTextMediumBold()
onClick = {
Toast.makeText(requireContext(), "10月31日-11月3日的订单,预计在2日内发货", Toast.LENGTH_SHORT)
Toast.makeText(
requireContext(),
"10月31日-11月3日的订单,预计在2日内发货",
Toast.LENGTH_SHORT
)
.show()
}
}
Expand Down
20 changes: 16 additions & 4 deletions span/src/main/java/com/arc/fast/span/FastTextWrapSpan.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.arc.fast.span

import android.graphics.*
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.Typeface
import android.text.style.ReplacementSpan

/**
Expand All @@ -14,7 +18,7 @@ open class FastTextWrapSpan(
private val backgroundColor: Int = Color.TRANSPARENT,
private val textSize: Float? = null,
private val textColor: Int? = null,
private val textStyle: Int? = null,
private val textTypeface: Typeface? = null,
private val textLeftMargin: Float = 0f,
private val textRightMargin: Float = 0f,
private val leftPadding: Float = 0f,
Expand All @@ -25,6 +29,7 @@ open class FastTextWrapSpan(
private var textWidth = 0
private var defaultTextSize = 0f
private var defaultTextTypeface = Typeface.DEFAULT
private var defaultTextSkewX = 0f
private var defaultColor = Color.BLACK
private var defaultStrokeWidth = 0f
private val strokeWidthOffset: Float by lazy { borderSize / 2 }
Expand All @@ -38,9 +43,10 @@ open class FastTextWrapSpan(
defaultColor = paint.color
defaultStrokeWidth = paint.strokeWidth
defaultTextTypeface = paint.typeface
defaultTextSkewX = paint.textSkewX
}
if (textSize != null) paint.textSize = textSize
if (textStyle != null) paint.typeface = Typeface.create(defaultTextTypeface, textStyle)
if (textTypeface != null) paint.typeface = textTypeface
textWidth = (paint.measureText(text, start, end) + leftPadding + rightPadding).toInt()
return textWidth + textLeftMargin.toInt() + textRightMargin.toInt()
}
Expand All @@ -63,7 +69,12 @@ open class FastTextWrapSpan(
val metrics = paint.fontMetrics;
val top = y + metrics.top
val bottom = y + metrics.bottom
paint.typeface = Typeface.DEFAULT
if (textTypeface != null) {
paint.typeface = textTypeface
if (textTypeface.isItalic) {
paint.textSkewX = -0.25f
}
}
val rectF = RectF(
x + strokeWidthOffset + textLeftMargin,
top + strokeWidthOffset - topPadding - bottomPadding,
Expand Down Expand Up @@ -105,6 +116,7 @@ open class FastTextWrapSpan(
)
//恢复画笔
paint.typeface = defaultTextTypeface
paint.textSkewX = defaultTextSkewX
paint.color = defaultColor
paint.textSize = defaultTextSize
}
Expand Down

0 comments on commit 9b5f6ed

Please sign in to comment.