From 1fc0fd580ccb5489ee64653da783143db8cdd4d8 Mon Sep 17 00:00:00 2001 From: tianxiangyu <96164429+FlyJingFish@users.noreply.github.com> Date: Tue, 10 Jan 2023 15:11:11 +0800 Subject: [PATCH] =?UTF-8?q?1=EF=BC=8C=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flyjingfish/formattextview/FormatText.kt | 3 +-- .../formattextview/FormatTextView.kt | 27 +++++++++---------- .../formattextview/HtmlTextView.kt | 8 ++---- .../com/flyjingfish/formattextview/Utils.kt | 12 ++++----- .../formattextviewdemo/MainActivity.kt | 2 +- .../formattextviewdemo/SecondActivity.java | 2 +- 6 files changed, 22 insertions(+), 32 deletions(-) diff --git a/FormatTextView/src/main/java/com/flyjingfish/formattextview/FormatText.kt b/FormatTextView/src/main/java/com/flyjingfish/formattextview/FormatText.kt index b9a13ab..aa6ebba 100644 --- a/FormatTextView/src/main/java/com/flyjingfish/formattextview/FormatText.kt +++ b/FormatTextView/src/main/java/com/flyjingfish/formattextview/FormatText.kt @@ -1,8 +1,7 @@ -package com.flyjingfish.FormatTexttextview +package com.flyjingfish.formattextview import androidx.annotation.ColorRes import androidx.annotation.StringRes -import com.flyjingfish.formattextview.BaseFormat class FormatText : BaseFormat() { diff --git a/FormatTextView/src/main/java/com/flyjingfish/formattextview/FormatTextView.kt b/FormatTextView/src/main/java/com/flyjingfish/formattextview/FormatTextView.kt index d75d8bf..432130d 100644 --- a/FormatTextView/src/main/java/com/flyjingfish/formattextview/FormatTextView.kt +++ b/FormatTextView/src/main/java/com/flyjingfish/formattextview/FormatTextView.kt @@ -13,11 +13,8 @@ import android.text.style.* import android.text.util.Linkify import android.util.AttributeSet import android.util.LayoutDirection -import android.util.TypedValue import android.view.View import androidx.annotation.StringRes -import androidx.appcompat.widget.AppCompatTextView -import com.flyjingfish.FormatTexttextview.FormatText import java.lang.NullPointerException import androidx.core.text.TextUtilsCompat import java.util.* @@ -137,20 +134,20 @@ class FormatTextView : BaseTextView { } else { false } - val imageWidth = Utils.dp2px(context, formatImage.width) - val imageHeight = Utils.dp2px(context, formatImage.height) + val imageWidth = dp2px(context, formatImage.width) + val imageHeight = dp2px(context, formatImage.height) var marginLeft = if (isRtl) formatImage.marginEnd else formatImage.marginStart var marginRight = if (isRtl) formatImage.marginStart else formatImage.marginEnd marginLeft = if (marginLeft == 0f){ - Utils.dp2px(context, formatImage.marginLeft) + dp2px(context, formatImage.marginLeft) }else{ - Utils.dp2px(context, marginLeft) + dp2px(context, marginLeft) } marginRight = if (marginRight == 0f){ - Utils.dp2px(context, formatImage.marginRight) + dp2px(context, formatImage.marginRight) }else{ - Utils.dp2px(context, marginRight) + dp2px(context, marginRight) } val start = htmlBuilder.getSpanStart(urlSpan) @@ -268,15 +265,15 @@ class FormatTextView : BaseTextView { } if (underline && (formatText.underlineColor != 0 || formatText.underlineMarginTop != 0f || formatText.underlineWidth != 0f)) { val textPaint = TextPaint() - textPaint.textSize = if (textSize > 0) Utils.sp2px(context, textSize) else getTextSize() + textPaint.textSize = if (textSize > 0) sp2px(context, textSize) else getTextSize() val fm = textPaint.fontMetrics val underLineText = LineText( start, end, if (formatText.underlineColor != 0) resources.getColor(formatText.underlineColor) else textColor, - Utils.dp2px(context, formatText.underlineMarginTop) + fm.descent / 3, - if (formatText.underlineWidth == 0f) Utils.dp2px(context, 1f) else Utils.dp2px( + dp2px(context, formatText.underlineMarginTop) + fm.descent / 3, + if (formatText.underlineWidth == 0f) dp2px(context, 1f) else dp2px( context, formatText.underlineWidth ) @@ -287,7 +284,7 @@ class FormatTextView : BaseTextView { var userDefaultDelete = true if (deleteLine && (formatText.deleteLineColor != 0 || formatText.deleteLineWidth != 0f)) { val textPaint = TextPaint() - textPaint.textSize = if (textSize > 0) Utils.sp2px(context, textSize) else getTextSize() + textPaint.textSize = if (textSize > 0) sp2px(context, textSize) else getTextSize() val fm = textPaint.fontMetrics val deleteLineText = LineText( @@ -295,7 +292,7 @@ class FormatTextView : BaseTextView { end, if (formatText.deleteLineColor != 0) resources.getColor(formatText.deleteLineColor) else textColor, (fm.descent - fm.ascent) / 2 - fm.descent, - if (formatText.deleteLineWidth == 0f) Utils.dp2px(context, 1f) else Utils.dp2px( + if (formatText.deleteLineWidth == 0f) dp2px(context, 1f) else dp2px( context, formatText.deleteLineWidth ) @@ -321,7 +318,7 @@ class FormatTextView : BaseTextView { if (textSize > 0) { htmlBuilder.setSpan( AbsoluteSizeSpan( - Utils.sp2px(context, textSize).toInt(), + sp2px(context, textSize).toInt(), false ), start, end, flags ) diff --git a/FormatTextView/src/main/java/com/flyjingfish/formattextview/HtmlTextView.kt b/FormatTextView/src/main/java/com/flyjingfish/formattextview/HtmlTextView.kt index f46f624..f078d30 100644 --- a/FormatTextView/src/main/java/com/flyjingfish/formattextview/HtmlTextView.kt +++ b/FormatTextView/src/main/java/com/flyjingfish/formattextview/HtmlTextView.kt @@ -1,9 +1,7 @@ package com.flyjingfish.formattextview import android.content.Context -import android.graphics.Canvas import android.graphics.Color -import android.graphics.Paint import android.graphics.drawable.Drawable import android.graphics.drawable.InsetDrawable import android.graphics.drawable.LevelListDrawable @@ -16,8 +14,6 @@ import android.text.style.URLSpan import android.text.util.Linkify import android.util.AttributeSet import android.view.View -import androidx.appcompat.widget.AppCompatTextView -import java.lang.NullPointerException class HtmlTextView : BaseTextView { private var onHtmlClickListener: OnHtmlClickListener? = null @@ -127,12 +123,12 @@ class HtmlTextView : BaseTextView { }, start, end, flags) - val viewWidth: Float = if (htmlImage != null && htmlImage.maxWidth > 0) Utils.dp2px( + val viewWidth: Float = if (htmlImage != null && htmlImage.maxWidth > 0) dp2px( context, htmlImage.maxWidth ) else imageSpan.drawable.intrinsicWidth.toFloat() - val viewHeight: Float = if (htmlImage != null && htmlImage.maxHeight > 0) Utils.dp2px( + val viewHeight: Float = if (htmlImage != null && htmlImage.maxHeight > 0) dp2px( context, htmlImage.maxHeight ) else imageSpan.drawable.intrinsicHeight.toFloat() diff --git a/FormatTextView/src/main/java/com/flyjingfish/formattextview/Utils.kt b/FormatTextView/src/main/java/com/flyjingfish/formattextview/Utils.kt index 6105e43..7c3a8d6 100644 --- a/FormatTextView/src/main/java/com/flyjingfish/formattextview/Utils.kt +++ b/FormatTextView/src/main/java/com/flyjingfish/formattextview/Utils.kt @@ -3,11 +3,9 @@ package com.flyjingfish.formattextview import android.content.Context import android.util.TypedValue -object Utils { - fun dp2px(context: Context,dp: Float): Float { - return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.resources.displayMetrics) - } - fun sp2px(context: Context,sp: Float): Float { - return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.resources.displayMetrics) - } +fun dp2px(context: Context,dp: Float): Float { + return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.resources.displayMetrics) +} +fun sp2px(context: Context,sp: Float): Float { + return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.resources.displayMetrics) } \ No newline at end of file diff --git a/app/src/main/java/com/flyjingfish/formattextviewdemo/MainActivity.kt b/app/src/main/java/com/flyjingfish/formattextviewdemo/MainActivity.kt index 8a19ea4..a922b14 100644 --- a/app/src/main/java/com/flyjingfish/formattextviewdemo/MainActivity.kt +++ b/app/src/main/java/com/flyjingfish/formattextviewdemo/MainActivity.kt @@ -11,7 +11,7 @@ import com.bumptech.glide.RequestBuilder import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.target.CustomTarget import com.bumptech.glide.request.transition.Transition -import com.flyjingfish.FormatTexttextview.FormatText +import com.flyjingfish.formattextview.FormatText import com.flyjingfish.formattextview.* import kotlinx.android.synthetic.main.activity_main.* diff --git a/app/src/main/java/com/flyjingfish/formattextviewdemo/SecondActivity.java b/app/src/main/java/com/flyjingfish/formattextviewdemo/SecondActivity.java index 16bf302..81b1688 100644 --- a/app/src/main/java/com/flyjingfish/formattextviewdemo/SecondActivity.java +++ b/app/src/main/java/com/flyjingfish/formattextviewdemo/SecondActivity.java @@ -12,7 +12,7 @@ import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.CustomTarget; import com.bumptech.glide.request.transition.Transition; -import com.flyjingfish.FormatTexttextview.FormatText; +import com.flyjingfish.formattextview.FormatText; import com.flyjingfish.formattextview.FormatImage; import com.flyjingfish.formattextview.FormatTextView; import com.flyjingfish.formattextview.HtmlImage;