From 9b5f6ed919955d5f5d2678bfdf24b4a62508e7a8 Mon Sep 17 00:00:00 2001 From: liuzhentao Date: Thu, 12 Oct 2023 15:11:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DImmersiveDialog=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E9=97=AE=E9=A2=98=20span=E5=BA=93=E6=94=AF=E6=8C=81te?= =?UTF-8?q?xtTypeface=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/arc/fast/immersive/ImmersiveDialog.kt | 52 +++++++++++-------- .../com/arc/fast/sample/span/SpanFragment.kt | 32 ++++++++++-- .../com/arc/fast/span/FastTextWrapSpan.kt | 20 +++++-- 3 files changed, 75 insertions(+), 29 deletions(-) diff --git a/immersive/src/main/java/com/arc/fast/immersive/ImmersiveDialog.kt b/immersive/src/main/java/com/arc/fast/immersive/ImmersiveDialog.kt index a8dbe57..35957c3 100644 --- a/immersive/src/main/java/com/arc/fast/immersive/ImmersiveDialog.kt +++ b/immersive/src/main/java/com/arc/fast/immersive/ImmersiveDialog.kt @@ -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 @@ -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() } @@ -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() diff --git a/sample/src/main/java/com/arc/fast/sample/span/SpanFragment.kt b/sample/src/main/java/com/arc/fast/sample/span/SpanFragment.kt index 22d6384..275e469 100644 --- a/sample/src/main/java/com/arc/fast/sample/span/SpanFragment.kt +++ b/sample/src/main/java/com/arc/fast/sample/span/SpanFragment.kt @@ -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() { @@ -47,6 +53,22 @@ class SpanFragment : BaseFragment() { 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, @@ -63,7 +85,11 @@ class SpanFragment : BaseFragment() { 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() } } diff --git a/span/src/main/java/com/arc/fast/span/FastTextWrapSpan.kt b/span/src/main/java/com/arc/fast/span/FastTextWrapSpan.kt index 66e7f27..d41cd9a 100644 --- a/span/src/main/java/com/arc/fast/span/FastTextWrapSpan.kt +++ b/span/src/main/java/com/arc/fast/span/FastTextWrapSpan.kt @@ -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 /** @@ -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, @@ -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 } @@ -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() } @@ -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, @@ -105,6 +116,7 @@ open class FastTextWrapSpan( ) //恢复画笔 paint.typeface = defaultTextTypeface + paint.textSkewX = defaultTextSkewX paint.color = defaultColor paint.textSize = defaultTextSize }