Skip to content

Commit 1fc0fd5

Browse files
committed
1,优化代码
1 parent afdf914 commit 1fc0fd5

File tree

6 files changed

+22
-32
lines changed

6 files changed

+22
-32
lines changed

FormatTextView/src/main/java/com/flyjingfish/formattextview/FormatText.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
package com.flyjingfish.FormatTexttextview
1+
package com.flyjingfish.formattextview
22

33
import androidx.annotation.ColorRes
44
import androidx.annotation.StringRes
5-
import com.flyjingfish.formattextview.BaseFormat
65

76
class FormatText : BaseFormat() {
87

FormatTextView/src/main/java/com/flyjingfish/formattextview/FormatTextView.kt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ import android.text.style.*
1313
import android.text.util.Linkify
1414
import android.util.AttributeSet
1515
import android.util.LayoutDirection
16-
import android.util.TypedValue
1716
import android.view.View
1817
import androidx.annotation.StringRes
19-
import androidx.appcompat.widget.AppCompatTextView
20-
import com.flyjingfish.FormatTexttextview.FormatText
2118
import java.lang.NullPointerException
2219
import androidx.core.text.TextUtilsCompat
2320
import java.util.*
@@ -137,20 +134,20 @@ class FormatTextView : BaseTextView {
137134
} else {
138135
false
139136
}
140-
val imageWidth = Utils.dp2px(context, formatImage.width)
141-
val imageHeight = Utils.dp2px(context, formatImage.height)
137+
val imageWidth = dp2px(context, formatImage.width)
138+
val imageHeight = dp2px(context, formatImage.height)
142139
var marginLeft = if (isRtl) formatImage.marginEnd else formatImage.marginStart
143140
var marginRight = if (isRtl) formatImage.marginStart else formatImage.marginEnd
144141
marginLeft = if (marginLeft == 0f){
145-
Utils.dp2px(context, formatImage.marginLeft)
142+
dp2px(context, formatImage.marginLeft)
146143
}else{
147-
Utils.dp2px(context, marginLeft)
144+
dp2px(context, marginLeft)
148145
}
149146

150147
marginRight = if (marginRight == 0f){
151-
Utils.dp2px(context, formatImage.marginRight)
148+
dp2px(context, formatImage.marginRight)
152149
}else{
153-
Utils.dp2px(context, marginRight)
150+
dp2px(context, marginRight)
154151
}
155152

156153
val start = htmlBuilder.getSpanStart(urlSpan)
@@ -268,15 +265,15 @@ class FormatTextView : BaseTextView {
268265
}
269266
if (underline && (formatText.underlineColor != 0 || formatText.underlineMarginTop != 0f || formatText.underlineWidth != 0f)) {
270267
val textPaint = TextPaint()
271-
textPaint.textSize = if (textSize > 0) Utils.sp2px(context, textSize) else getTextSize()
268+
textPaint.textSize = if (textSize > 0) sp2px(context, textSize) else getTextSize()
272269
val fm = textPaint.fontMetrics
273270

274271
val underLineText = LineText(
275272
start,
276273
end,
277274
if (formatText.underlineColor != 0) resources.getColor(formatText.underlineColor) else textColor,
278-
Utils.dp2px(context, formatText.underlineMarginTop) + fm.descent / 3,
279-
if (formatText.underlineWidth == 0f) Utils.dp2px(context, 1f) else Utils.dp2px(
275+
dp2px(context, formatText.underlineMarginTop) + fm.descent / 3,
276+
if (formatText.underlineWidth == 0f) dp2px(context, 1f) else dp2px(
280277
context,
281278
formatText.underlineWidth
282279
)
@@ -287,15 +284,15 @@ class FormatTextView : BaseTextView {
287284
var userDefaultDelete = true
288285
if (deleteLine && (formatText.deleteLineColor != 0 || formatText.deleteLineWidth != 0f)) {
289286
val textPaint = TextPaint()
290-
textPaint.textSize = if (textSize > 0) Utils.sp2px(context, textSize) else getTextSize()
287+
textPaint.textSize = if (textSize > 0) sp2px(context, textSize) else getTextSize()
291288
val fm = textPaint.fontMetrics
292289

293290
val deleteLineText = LineText(
294291
start,
295292
end,
296293
if (formatText.deleteLineColor != 0) resources.getColor(formatText.deleteLineColor) else textColor,
297294
(fm.descent - fm.ascent) / 2 - fm.descent,
298-
if (formatText.deleteLineWidth == 0f) Utils.dp2px(context, 1f) else Utils.dp2px(
295+
if (formatText.deleteLineWidth == 0f) dp2px(context, 1f) else dp2px(
299296
context,
300297
formatText.deleteLineWidth
301298
)
@@ -321,7 +318,7 @@ class FormatTextView : BaseTextView {
321318
if (textSize > 0) {
322319
htmlBuilder.setSpan(
323320
AbsoluteSizeSpan(
324-
Utils.sp2px(context, textSize).toInt(),
321+
sp2px(context, textSize).toInt(),
325322
false
326323
), start, end, flags
327324
)

FormatTextView/src/main/java/com/flyjingfish/formattextview/HtmlTextView.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.flyjingfish.formattextview
22

33
import android.content.Context
4-
import android.graphics.Canvas
54
import android.graphics.Color
6-
import android.graphics.Paint
75
import android.graphics.drawable.Drawable
86
import android.graphics.drawable.InsetDrawable
97
import android.graphics.drawable.LevelListDrawable
@@ -16,8 +14,6 @@ import android.text.style.URLSpan
1614
import android.text.util.Linkify
1715
import android.util.AttributeSet
1816
import android.view.View
19-
import androidx.appcompat.widget.AppCompatTextView
20-
import java.lang.NullPointerException
2117

2218
class HtmlTextView : BaseTextView {
2319
private var onHtmlClickListener: OnHtmlClickListener? = null
@@ -127,12 +123,12 @@ class HtmlTextView : BaseTextView {
127123

128124
}, start, end, flags)
129125

130-
val viewWidth: Float = if (htmlImage != null && htmlImage.maxWidth > 0) Utils.dp2px(
126+
val viewWidth: Float = if (htmlImage != null && htmlImage.maxWidth > 0) dp2px(
131127
context,
132128
htmlImage.maxWidth
133129
) else imageSpan.drawable.intrinsicWidth.toFloat()
134130

135-
val viewHeight: Float = if (htmlImage != null && htmlImage.maxHeight > 0) Utils.dp2px(
131+
val viewHeight: Float = if (htmlImage != null && htmlImage.maxHeight > 0) dp2px(
136132
context,
137133
htmlImage.maxHeight
138134
) else imageSpan.drawable.intrinsicHeight.toFloat()

FormatTextView/src/main/java/com/flyjingfish/formattextview/Utils.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package com.flyjingfish.formattextview
33
import android.content.Context
44
import android.util.TypedValue
55

6-
object Utils {
7-
fun dp2px(context: Context,dp: Float): Float {
8-
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.resources.displayMetrics)
9-
}
10-
fun sp2px(context: Context,sp: Float): Float {
11-
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.resources.displayMetrics)
12-
}
6+
fun dp2px(context: Context,dp: Float): Float {
7+
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.resources.displayMetrics)
8+
}
9+
fun sp2px(context: Context,sp: Float): Float {
10+
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.resources.displayMetrics)
1311
}

app/src/main/java/com/flyjingfish/formattextviewdemo/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import com.bumptech.glide.RequestBuilder
1111
import com.bumptech.glide.request.RequestOptions
1212
import com.bumptech.glide.request.target.CustomTarget
1313
import com.bumptech.glide.request.transition.Transition
14-
import com.flyjingfish.FormatTexttextview.FormatText
14+
import com.flyjingfish.formattextview.FormatText
1515
import com.flyjingfish.formattextview.*
1616
import kotlinx.android.synthetic.main.activity_main.*
1717

app/src/main/java/com/flyjingfish/formattextviewdemo/SecondActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import com.bumptech.glide.request.RequestOptions;
1313
import com.bumptech.glide.request.target.CustomTarget;
1414
import com.bumptech.glide.request.transition.Transition;
15-
import com.flyjingfish.FormatTexttextview.FormatText;
15+
import com.flyjingfish.formattextview.FormatText;
1616
import com.flyjingfish.formattextview.FormatImage;
1717
import com.flyjingfish.formattextview.FormatTextView;
1818
import com.flyjingfish.formattextview.HtmlImage;

0 commit comments

Comments
 (0)