Skip to content

Commit 3608642

Browse files
committed
1,优化代码
1 parent 7da97b0 commit 3608642

File tree

7 files changed

+120
-92
lines changed

7 files changed

+120
-92
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ open class BaseTextView : PerfectTextView {
1717
defStyleAttr
1818
)
1919

20+
init {
21+
Utils.applicationContext = context.applicationContext
22+
}
23+
2024
fun getImageSpanWidthHeight(
2125
viewWidth: Float,
2226
viewHeight: Float,

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

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import android.util.AttributeSet
2222
import android.util.LayoutDirection
2323
import android.view.View
2424
import androidx.annotation.StringRes
25+
import androidx.core.content.ContextCompat
2526
import androidx.core.text.TextUtilsCompat
2627
import java.util.Locale
2728

@@ -49,11 +50,35 @@ class FormatTextView : BaseTextView {
4950
defStyleAttr
5051
)
5152

52-
fun setFormatTextBean(@StringRes formatTextRes: Int, vararg args: BaseFormat?) {
53-
setFormatTextBean(resources.getString(formatTextRes), *args)
53+
fun setFormatText(@StringRes formatTextRes: Int, vararg args: Int) {
54+
setFormatText(resources.getString(formatTextRes), *args)
55+
}
56+
57+
fun setFormatText(formatTextValue: String?, vararg args: Int) {
58+
val formatTexts: Array<FormatText?> = arrayOfNulls(args.size)
59+
for (i in args.indices) {
60+
formatTexts[i] = FormatText().setResValue(args[i])
61+
}
62+
setFormatText(formatTextValue, *formatTexts)
63+
}
64+
65+
fun setFormatText(@StringRes formatTextRes: Int, vararg args: String) {
66+
setFormatText(resources.getString(formatTextRes), *args)
5467
}
5568

56-
fun setFormatTextBean(formatTextValue: String?, vararg args: BaseFormat?) {
69+
fun setFormatText(formatTextValue: String?, vararg args: String) {
70+
val formatTexts: Array<FormatText?> = arrayOfNulls(args.size)
71+
for (i in args.indices) {
72+
formatTexts[i] = FormatText().setStrValue(args[i])
73+
}
74+
setFormatText(formatTextValue, *formatTexts)
75+
}
76+
77+
fun setFormatText(@StringRes formatTextRes: Int, vararg args: BaseFormat?) {
78+
setFormatText(resources.getString(formatTextRes), *args)
79+
}
80+
81+
fun setFormatText(formatTextValue: String?, vararg args: BaseFormat?) {
5782
var textValue = formatTextValue?.replace("\\r\\n".toRegex(), "<br>")
5883
textValue = textValue?.replace("\\n".toRegex(), "<br>")
5984
textValue = textValue?.replace("\\r".toRegex(), "<br>")
@@ -122,31 +147,6 @@ class FormatTextView : BaseTextView {
122147
text = richText?.let { formatArgs?.let { it1 -> getCustomStyleHtml(it, *it1) } }
123148
}
124149

125-
fun setFormatText(@StringRes formatTextRes: Int, vararg args: Int) {
126-
setFormatText(resources.getString(formatTextRes), *args)
127-
}
128-
129-
fun setFormatText(formatTextValue: String?, vararg args: Int) {
130-
val formatTexts: Array<FormatText?> = arrayOfNulls(args.size)
131-
for (i in args.indices) {
132-
formatTexts[i] = FormatText().setResValue(args[i])
133-
}
134-
setFormatTextBean(formatTextValue, *formatTexts)
135-
}
136-
137-
fun setFormatText(@StringRes formatTextRes: Int, vararg args: String) {
138-
setFormatText(resources.getString(formatTextRes), *args)
139-
}
140-
141-
fun setFormatText(formatTextValue: String?, vararg args: String) {
142-
val formatTexts: Array<FormatText?> = arrayOfNulls(args.size)
143-
for (i in args.indices) {
144-
formatTexts[i] = FormatText().setStrValue(args[i])
145-
}
146-
setFormatTextBean(formatTextValue, *formatTexts)
147-
}
148-
149-
150150
private fun getCustomStyleHtml(html: String, vararg args: BaseFormat?): CharSequence? {
151151
val spannedHtml = Html.fromHtml(html)
152152
val htmlBuilder = SpannableStringBuilder(spannedHtml)
@@ -178,37 +178,39 @@ class FormatTextView : BaseTextView {
178178
} else {
179179
false
180180
}
181-
val imageWidth = dp2px(context, formatImage.width)
182-
val imageHeight = dp2px(context, formatImage.height)
181+
val imageWidth = formatImage.width.dp
182+
val imageHeight = formatImage.height.dp
183183
var marginLeft = if (isRtl) formatImage.marginEnd else formatImage.marginStart
184184
var marginRight = if (isRtl) formatImage.marginStart else formatImage.marginEnd
185185
marginLeft = if (marginLeft == 0f){
186-
dp2px(context, formatImage.marginLeft)
186+
formatImage.marginLeft.dp
187187
}else{
188-
dp2px(context, marginLeft)
188+
marginLeft.dp
189189
}
190190

191191
marginRight = if (marginRight == 0f){
192-
dp2px(context, formatImage.marginRight)
192+
formatImage.marginRight.dp
193193
}else{
194-
dp2px(context, marginRight)
194+
marginRight.dp
195195
}
196196

197197
val start = htmlBuilder.getSpanStart(urlSpan)
198198
val end = htmlBuilder.getSpanEnd(urlSpan)
199199
val flags = htmlBuilder.getSpanFlags(urlSpan)
200200
val clickableSpan: ClickableSpan = FormatClickableSpan(urlSpan)
201-
201+
htmlBuilder.removeSpan(formatImage)
202202
htmlBuilder.setSpan(clickableSpan, start, end, flags)
203203

204204
val drawable = LevelListDrawable()
205205
if (formatImage.imageResValue != 0) {
206-
val d = resources.getDrawable(formatImage.imageResValue)
207-
val wh = getImageSpanWidthHeight(
208-
if (imageWidth != 0f) imageWidth else d.intrinsicWidth.toFloat(),
209-
if (imageHeight != 0f) imageHeight else d.intrinsicHeight.toFloat(),
210-
d
211-
)
206+
val d = ContextCompat.getDrawable(context,formatImage.imageResValue)
207+
val wh = d?.let {
208+
getImageSpanWidthHeight(
209+
if (imageWidth != 0f) imageWidth else d.intrinsicWidth.toFloat(),
210+
if (imageHeight != 0f) imageHeight else d.intrinsicHeight.toFloat(),
211+
d
212+
)
213+
}
212214
val insetDrawable =
213215
InsetDrawable(
214216
d,
@@ -220,20 +222,22 @@ class FormatTextView : BaseTextView {
220222
drawable.addLevel(1, 1, insetDrawable)
221223
drawable.setBounds(
222224
0, 0,
223-
wh[0].toInt() + marginLeft.toInt() + marginRight.toInt(),
224-
wh[1].toInt()
225+
(wh?.get(0)?.toInt() ?: 0) + marginLeft.toInt() + marginRight.toInt(),
226+
(wh?.get(1)?.toInt() ?: 0).toInt()
225227
)
226228
drawable.level = 1
227229
val imageSpan = FormatImageSpan(drawable, formatImage.verticalAlignment)
228230
htmlBuilder.setSpan(imageSpan, start, end, flags)
229231
} else {
230232
if (formatImage.imagePlaceHolder != 0) {
231-
val d = resources.getDrawable(formatImage.imagePlaceHolder)
232-
val wh = getImageSpanWidthHeight(
233-
if (imageWidth != 0f) imageWidth else d.intrinsicWidth.toFloat(),
234-
if (imageHeight != 0f) imageHeight else d.intrinsicHeight.toFloat(),
235-
d
236-
)
233+
val d = ContextCompat.getDrawable(context,formatImage.imagePlaceHolder)
234+
val wh = d?.let {
235+
getImageSpanWidthHeight(
236+
if (imageWidth != 0f) imageWidth else d.intrinsicWidth.toFloat(),
237+
if (imageHeight != 0f) imageHeight else d.intrinsicHeight.toFloat(),
238+
d
239+
)
240+
}
237241
val insetDrawable =
238242
InsetDrawable(
239243
d,
@@ -245,15 +249,15 @@ class FormatTextView : BaseTextView {
245249
drawable.addLevel(1, 1, insetDrawable)
246250
drawable.setBounds(
247251
0, 0,
248-
wh[0].toInt() + marginLeft.toInt() + marginRight.toInt(),
249-
wh[1].toInt()
252+
(wh?.get(0)?.toInt() ?: 0) + marginLeft.toInt() + marginRight.toInt(),
253+
(wh?.get(1)?.toInt() ?: 0).toInt()
250254
)
251255
drawable.level = 1
252256
}
253257
val imageSpan = FormatImageSpan(drawable, formatImage.verticalAlignment)
254258
htmlBuilder.setSpan(imageSpan, start, end, flags)
255259
if (onInflateImageListener != null) {
256-
onInflateImageListener?.onInflate(
260+
onInflateImageListener!!.onInflate(
257261
formatImage,
258262
object : OnReturnDrawableListener {
259263
override fun onReturnDrawable(d: Drawable) {
@@ -309,37 +313,31 @@ class FormatTextView : BaseTextView {
309313
}
310314
if (underline && (formatText.underlineColor != 0 || formatText.underlineMarginTop != 0f || formatText.underlineWidth != 0f)) {
311315
val textPaint = TextPaint()
312-
textPaint.textSize = if (textSize > 0) sp2px(context, textSize) else getTextSize()
316+
textPaint.textSize = if (textSize > 0) textSize.sp else getTextSize()
313317
val fm = textPaint.fontMetrics
314318

315319
val underLineText = LineText(
316320
start,
317321
end,
318322
if (formatText.underlineColor != 0) resources.getColor(formatText.underlineColor) else textColor,
319-
dp2px(context, formatText.underlineMarginTop) + fm.descent / 3,
320-
if (formatText.underlineWidth == 0f) dp2px(context, 1f) else dp2px(
321-
context,
322-
formatText.underlineWidth
323-
)
323+
formatText.underlineMarginTop.dp + fm.descent / 3,
324+
if (formatText.underlineWidth == 0f) 1f.dp else formatText.underlineWidth.dp
324325
)
325326
underLineTexts.add(underLineText)
326327
userDefaultUnder = false
327328
}
328329
var userDefaultDelete = true
329330
if (deleteLine && (formatText.deleteLineColor != 0 || formatText.deleteLineWidth != 0f)) {
330331
val textPaint = TextPaint()
331-
textPaint.textSize = if (textSize > 0) sp2px(context, textSize) else getTextSize()
332+
textPaint.textSize = if (textSize > 0) textSize.sp else getTextSize()
332333
val fm = textPaint.fontMetrics
333334

334335
val deleteLineText = LineText(
335336
start,
336337
end,
337338
if (formatText.deleteLineColor != 0) resources.getColor(formatText.deleteLineColor) else textColor,
338339
(fm.descent - fm.ascent) / 2 - fm.descent,
339-
if (formatText.deleteLineWidth == 0f) dp2px(context, 1f) else dp2px(
340-
context,
341-
formatText.deleteLineWidth
342-
)
340+
if (formatText.deleteLineWidth == 0f) 1f.dp else formatText.deleteLineWidth.dp
343341
)
344342
deleteLineTexts.add(deleteLineText)
345343
userDefaultDelete = false
@@ -348,7 +346,7 @@ class FormatTextView : BaseTextView {
348346
//gradient
349347
if (!isDrawGradient && formatText.gradient != null) {
350348
val textPaint = TextPaint()
351-
textPaint.textSize = if (textSize > 0) sp2px(context, textSize) else getTextSize()
349+
textPaint.textSize = if (textSize > 0) textSize.sp else getTextSize()
352350
val fm = textPaint.fontMetrics
353351

354352
val gradientText = LineText(
@@ -438,11 +436,12 @@ class FormatTextView : BaseTextView {
438436
}
439437
}
440438
}
439+
htmlBuilder.removeSpan(urlSpan)
441440
htmlBuilder.setSpan(clickableSpan, start, end, flags)
442441
if (textSize > 0) {
443442
htmlBuilder.setSpan(
444443
AbsoluteSizeSpan(
445-
sp2px(context, textSize).toInt(),
444+
textSize.sp.toInt(),
446445
false
447446
), start, end, flags
448447
)
@@ -476,10 +475,11 @@ class FormatTextView : BaseTextView {
476475
}
477476
}
478477

479-
open inner class FormatClickableSpan(private val urlSpan: URLSpan) : ClickableSpan() {
478+
open inner class FormatClickableSpan(urlSpan: URLSpan) : ClickableSpan() {
479+
private var position = urlSpan.url.toInt()
480+
480481
override fun onClick(widget: View) {
481-
val url = urlSpan.url
482-
onFormatClickListener?.onLabelClick(url.toInt())
482+
onFormatClickListener?.onLabelClick(position)
483483
isClickSpanItem = true
484484
}
485485
}

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,9 @@ class HtmlTextView : BaseTextView {
123123

124124
}, start, end, flags)
125125

126-
val viewWidth: Float = if (htmlImage != null && htmlImage.maxWidth > 0) dp2px(
127-
context,
128-
htmlImage.maxWidth
129-
) else imageSpan.drawable.intrinsicWidth.toFloat()
130-
131-
val viewHeight: Float = if (htmlImage != null && htmlImage.maxHeight > 0) dp2px(
132-
context,
133-
htmlImage.maxHeight
134-
) else imageSpan.drawable.intrinsicHeight.toFloat()
126+
val viewWidth: Float = if (htmlImage != null && htmlImage.maxWidth > 0) htmlImage.maxWidth.dp else imageSpan.drawable.intrinsicWidth.toFloat()
127+
128+
val viewHeight: Float = if (htmlImage != null && htmlImage.maxHeight > 0) htmlImage.maxHeight.dp else imageSpan.drawable.intrinsicHeight.toFloat()
135129

136130
val drawable = LevelListDrawable()
137131
if (htmlImage != null && htmlImage.imagePlaceHolder != 0) {

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

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

6-
fun dp2px(context: Context,dp: Float): Float {
7-
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.resources.displayMetrics)
6+
fun dp2px(context: Context, dp: Float): Float {
7+
return TypedValue.applyDimension(
8+
TypedValue.COMPLEX_UNIT_DIP,
9+
dp,
10+
context.resources.displayMetrics
11+
)
812
}
9-
fun sp2px(context: Context,sp: Float): Float {
10-
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.resources.displayMetrics)
13+
14+
fun sp2px(context: Context, sp: Float): Float {
15+
return TypedValue.applyDimension(
16+
TypedValue.COMPLEX_UNIT_SP,
17+
sp,
18+
context.resources.displayMetrics
19+
)
20+
}
21+
22+
internal val Float.dp
23+
get() = TypedValue.applyDimension(
24+
TypedValue.COMPLEX_UNIT_DIP,
25+
this,
26+
Utils.applicationContext.resources.displayMetrics
27+
)
28+
29+
internal val Float.sp
30+
get() = TypedValue.applyDimension(
31+
TypedValue.COMPLEX_UNIT_SP,
32+
this,
33+
Utils.applicationContext.resources.displayMetrics
34+
)
35+
36+
internal object Utils {
37+
lateinit var applicationContext: Context
1138
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.flyjingfish.formattextviewdemo
22

33
import android.graphics.Color
4-
import android.graphics.LinearGradient
5-
import android.graphics.Shader
64
import android.graphics.drawable.Drawable
75
import android.os.Bundle
8-
import android.view.ViewTreeObserver.OnGlobalLayoutListener
6+
import android.util.Log
97
import android.widget.Toast
108
import androidx.appcompat.app.AppCompatActivity
119
import com.bumptech.glide.Glide
@@ -17,10 +15,16 @@ import com.flyjingfish.formattextview.*
1715
import kotlinx.android.synthetic.main.activity_main.*
1816

1917
class DemoActivity : AppCompatActivity() {
18+
var wh :FloatArray?=null
2019
override fun onCreate(savedInstanceState: Bundle?) {
2120
super.onCreate(savedInstanceState)
2221
setContentView(R.layout.activity_demo)
23-
text1.setFormatTextBean(R.string.xieyi,
22+
23+
val w = (wh?.get(0)?.toInt() ?: 0)
24+
25+
Log.e("DemoActivity","width="+w)
26+
27+
text1.setFormatText(R.string.xieyi,
2428
FormatText().apply {
2529
textSize = 22f
2630
textColor = R.color.colorAccent
@@ -80,7 +84,7 @@ class DemoActivity : AppCompatActivity() {
8084
})
8185
}
8286
})
83-
text2.setFormatTextBean(R.string.xiao_ming_book,
87+
text2.setFormatText(R.string.xiao_ming_book,
8488
FormatText().apply {
8589
textSize = 30f
8690
textColor = R.color.colorAccent

0 commit comments

Comments
 (0)