Skip to content

Commit

Permalink
1,优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyJingFish committed Jan 10, 2023
1 parent afdf914 commit 1fc0fd5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
)
Expand All @@ -287,15 +284,15 @@ 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(
start,
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
)
Expand All @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1fc0fd5

Please sign in to comment.