Skip to content

Commit 9dab010

Browse files
committed
feat:「添加text、textImage的背景图片属性」
1 parent d66ccde commit 9dab010

11 files changed

Lines changed: 217 additions & 102 deletions

File tree

.idea/misc.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Java使用参考[JavaActivity](https://github.com/ChinaLike/TagTextView/blob/mai
7070
| `tvt_bottom_padding` | 标签下内边距 | 0dp | `tvt_type`为text、textImage,且`tvt_padding`未设置有效 |
7171
| `tvt_left_padding` | 标签左内边距 | 5dp | `tvt_type`为text、textImage,且`tvt_padding`未设置有效 |
7272
| `tvt_background_color` | 标签背景颜色 | Color.GRAY | `tvt_type`为text、textImage时有效 |
73+
| `tvt_background_img` | 标签背景图片 | | `tvt_type`为text、textImage时有效 |
7374
| `tvt_start_gradient_background_color` | 标签渐变开始颜色 | | `tvt_type`为text、textImage时有效 |
7475
| `tvt_end_gradient_background_color` | 标签渐变结束颜色 | | `tvt_type`为text、textImage时有效 |
7576
| `tvt_stroke_width` | 标签边框宽度 | | `tvt_type`为text、textImage时有效 |
@@ -96,31 +97,32 @@ Java使用参考[JavaActivity](https://github.com/ChinaLike/TagTextView/blob/mai
9697
| API | 描述 | 默认值 | 说明 |
9798
|------------------------------|-------------------------|-------------------------------------------|--------------------------------------------------------------------|
9899
| type | 标签类型 | | TEXT-文本,IMAGE-图片,TEXT_IMAGE-图文结合,URL-网络链接 |
99-
| textSize | 标签上文本的字体大小 | | 单位PX,不传就和TextView的textSize保持一致 |
100-
| textColor | 标签上文本的字体颜色 | Color.WHITE | |
100+
| textSize | 标签上文本的字体大小 | | 单位PX,不传就和TextView的textSize保持一致 |
101+
| textColor | 标签上文本的字体颜色 | Color.WHITE | |
101102
| width | 标签宽度 | | 不设置自动适应 |
102103
| height | 标签宽度 | | 不设置自动适应 |
103104
| radius | 标签的圆角 | | |
104-
| leftTopRadius | 标签左上的圆角 | 2dp | |
105-
| leftBottomRadius | 标签左下的圆角 | 2dp | |
106-
| rightTopRadius | 标签右上的圆角 | 2dp | |
107-
| rightBottomRadius | 标签右下的圆角 | 2dp | |
108-
| padding | 标签的内边距 | | 默认值:topPadding=0,rightPadding=5,bottomPadding=0,leftPadding=5 |
109-
| topPadding | 标签的上内边距 | 0dp | |
110-
| rightPadding | 标签的右内边距 | 5dp | |
111-
| bottomPadding | 标签的下内边距 | 0dp | |
112-
| leftPadding | 标签的左内边距 | 5dp | |
113-
| backgroundColor | 标签的背景颜色 | Color.GRAY | |
114-
| startGradientBackgroundColor | 标签的渐变开始颜色 | | |
115-
| endGradientBackgroundColor | 标签的渐变结束颜色 | | |
116-
| gradientOrientation | 标签的渐变样式 | GradientDrawable .Orientation .LEFT_RIGHT | |
117-
| strokeWidth | 标签的边框宽度 | 0 | |
118-
| strokeColor | 标签的边框颜色 | Color.GRAY | strokeWidth>0有效 |
119-
| imageAlignText | 图片在文字的那一个方向 | Orientation.LEFT | |
120-
| drawableZoomType | 图片缩放类型 | DrawableZoomType.ORIGINAL | |
105+
| leftTopRadius | 标签左上的圆角 | 2dp | |
106+
| leftBottomRadius | 标签左下的圆角 | 2dp | |
107+
| rightTopRadius | 标签右上的圆角 | 2dp | |
108+
| rightBottomRadius | 标签右下的圆角 | 2dp | |
109+
| padding | 标签的内边距 | | 默认值:topPadding=0,rightPadding=5,bottomPadding=0,leftPadding=5 |
110+
| topPadding | 标签的上内边距 | 0dp | |
111+
| rightPadding | 标签的右内边距 | 5dp | |
112+
| bottomPadding | 标签的下内边距 | 0dp | |
113+
| leftPadding | 标签的左内边距 | 5dp | |
114+
| backgroundColor | 标签的背景颜色 | Color.GRAY | |
115+
| backgroundDrawable | 标签的背景图片 | | |
116+
| startGradientBackgroundColor | 标签的渐变开始颜色 | | |
117+
| endGradientBackgroundColor | 标签的渐变结束颜色 | | |
118+
| gradientOrientation | 标签的渐变样式 | GradientDrawable .Orientation .LEFT_RIGHT | |
119+
| strokeWidth | 标签的边框宽度 | 0 | |
120+
| strokeColor | 标签的边框颜色 | Color.GRAY | strokeWidth>0有效 |
121+
| imageAlignText | 图片在文字的那一个方向 | Orientation.LEFT | |
122+
| drawableZoomType | 图片缩放类型 | DrawableZoomType.ORIGINAL | |
121123
| imageWidth | 图片的宽度 | | |
122124
| imageHeight | 图片的高度 | | |
123-
| align | 标签的对齐方式 | Align.CENTER | |
125+
| align | 标签的对齐方式 | Align.CENTER | |
124126
| text | 标签文本 | | type为TYPE.TEXT、TYPE.TEXT_IMAGE有效 |
125127
| imageResource | 标签图片 | | type为TYPE.IMAGE、TYPE.TEXT_IMAGE有效 |
126128
| imageDrawable | 标签图片 | | type为TYPE.IMAGE、TYPE.TEXT_IMAGE有效 |

TagTextView/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
defaultConfig {
99
minSdkVersion 16
1010
targetSdkVersion 30
11-
versionCode 24
12-
versionName "0.2.4"
11+
versionCode 25
12+
versionName "0.2.5"
1313

1414
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1515
consumerProguardFiles "consumer-rules.pro"

TagTextView/src/main/java/com/view/text/TextViewEx.kt

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ import android.text.method.LinkMovementMethod
1616
import android.text.style.ReplacementSpan
1717
import android.text.style.StrikethroughSpan
1818
import android.text.style.UnderlineSpan
19+
import android.view.Gravity
1920
import android.view.View
21+
import android.view.ViewGroup
22+
import android.widget.FrameLayout
23+
import android.widget.RelativeLayout
2024
import android.widget.TextView
2125
import androidx.annotation.ColorInt
26+
import com.google.android.material.imageview.ShapeableImageView
27+
import com.google.android.material.shape.ShapeAppearanceModel
2228
import com.view.text.annotation.Align
2329
import com.view.text.config.LinkType
2430
import com.view.text.config.TagConfig
@@ -630,26 +636,49 @@ private fun createItemView(context: Context, config: TagConfig): View {
630636
config.padding ?: config.bottomPadding
631637
)
632638
//圆角
633-
val cornerRadii = floatArrayOf(
634-
config?.radius ?: config.leftTopRadius, config?.radius ?: config.leftTopRadius,
635-
config?.radius ?: config.rightTopRadius, config?.radius ?: config.rightTopRadius,
636-
config?.radius ?: config.rightBottomRadius, config?.radius ?: config.rightBottomRadius,
637-
config?.radius ?: config.leftBottomRadius, config?.radius ?: config.leftBottomRadius
638-
)
639-
val gradientDrawable = GradientDrawable().apply {
640-
setCornerRadii(cornerRadii)
641-
this.colors =
642-
intArrayOf(
643-
config.startGradientBackgroundColor ?: config.backgroundColor,
644-
config.endGradientBackgroundColor ?: config.backgroundColor
639+
if (config?.backgroundDrawable != null) {
640+
val backgroundImageView = ShapeableImageView(context).apply {
641+
background = config?.backgroundDrawable
642+
shapeAppearanceModel = ShapeAppearanceModel.builder().apply {
643+
setTopLeftCornerSize(config?.radius ?: config.leftTopRadius)
644+
setTopRightCornerSize(config?.radius ?: config.rightTopRadius)
645+
setBottomLeftCornerSize(config?.radius ?: config.leftBottomRadius)
646+
setBottomRightCornerSize(config?.radius ?: config.rightBottomRadius)
647+
}.build()
648+
}
649+
return FrameLayout(context).apply {
650+
addView(
651+
backgroundImageView,
652+
FrameLayout.LayoutParams(
653+
if (config.width == 0) FrameLayout.LayoutParams.WRAP_CONTENT else config.width,
654+
if (config.height == 0) FrameLayout.LayoutParams.WRAP_CONTENT else config.height
655+
)
645656
)
646-
if (config.strokeWidth > 0) {
647-
setStroke(config.strokeWidth, config.strokeColor)
657+
addView(tagItemView)
658+
(tagItemView.layoutParams as? FrameLayout.LayoutParams)?.gravity = Gravity.CENTER
659+
}
660+
} else {
661+
val cornerRadii = floatArrayOf(
662+
config?.radius ?: config.leftTopRadius, config?.radius ?: config.leftTopRadius,
663+
config?.radius ?: config.rightTopRadius, config?.radius ?: config.rightTopRadius,
664+
config?.radius ?: config.rightBottomRadius, config?.radius ?: config.rightBottomRadius,
665+
config?.radius ?: config.leftBottomRadius, config?.radius ?: config.leftBottomRadius
666+
)
667+
val gradientDrawable = GradientDrawable().apply {
668+
setCornerRadii(cornerRadii)
669+
this.colors =
670+
intArrayOf(
671+
config.startGradientBackgroundColor ?: config.backgroundColor,
672+
config.endGradientBackgroundColor ?: config.backgroundColor
673+
)
674+
if (config.strokeWidth > 0) {
675+
setStroke(config.strokeWidth, config.strokeColor)
676+
}
677+
orientation = config.gradientOrientation
648678
}
649-
orientation = config.gradientOrientation
679+
tagItemView.background = gradientDrawable
680+
return tagItemView
650681
}
651-
tagItemView.background = gradientDrawable
652-
return tagItemView
653682
}
654683

655684
/**

TagTextView/src/main/java/com/view/text/config/TagConfig.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ data class TagConfig(val type: Type) {
106106
@ColorInt
107107
var backgroundColor: Int = Color.GRAY
108108

109+
/**
110+
* 背景图片
111+
*/
112+
var backgroundDrawable:Drawable? = null
113+
109114
/**
110115
* 开始渐变颜色
111116
*/

TagTextView/src/main/java/com/view/text/view/TagTextView.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ open class TagTextView @JvmOverloads constructor(
9595
R.styleable.TagTextView_tvt_background_color -> {
9696
config?.backgroundColor = getColor(attr, Color.TRANSPARENT)
9797
}
98+
R.styleable.TagTextView_tvt_background_img -> {
99+
config?.backgroundDrawable = getDrawable(attr)
100+
}
98101
R.styleable.TagTextView_tvt_start_gradient_background_color -> {
99102
config?.startGradientBackgroundColor = getColor(attr, Color.TRANSPARENT)
100103
}

TagTextView/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<attr name="tvt_bottom_padding" format="dimension" />
1919
<attr name="tvt_left_padding" format="dimension" />
2020
<attr name="tvt_background_color" format="color" />
21+
<attr name="tvt_background_img" format="reference" />
2122
<attr name="tvt_start_gradient_background_color" format="color"/>
2223
<attr name="tvt_end_gradient_background_color" format="color"/>
2324
<attr name="tvt_stroke_width" format="dimension" />

app/src/main/java/com/like/tag/JavaActivity.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import androidx.appcompat.app.AppCompatActivity;
1111
import androidx.appcompat.widget.AppCompatTextView;
12+
import androidx.core.content.ContextCompat;
1213
import androidx.recyclerview.widget.RecyclerView;
1314

1415
import com.core.util.DensityUtilKt;
@@ -244,6 +245,16 @@ private void textStyle() {
244245
Toast.makeText(JavaActivity.this, "这里是解释", Toast.LENGTH_LONG).show();
245246
return null;
246247
});
248+
249+
//1.6 自定义背景图片
250+
TextView text_tv16 = findViewById(R.id.text_tv16);
251+
TagConfig tv16Config = new TagConfig(Type.TEXT);
252+
tv16Config.setText("新品");
253+
tv16Config.setLeftTopRadius(DensityUtilKt.getDp(10));
254+
tv16Config.setWidth(DensityUtilKt.getDp(40));
255+
tv16Config.setHeight(DensityUtilKt.getDp(20));
256+
tv16Config.setBackgroundDrawable(ContextCompat.getDrawable(this, R.mipmap.custom_img));
257+
TextViewExKt.addTag(text_tv16,tv16Config);
247258
}
248259

249260
/**

app/src/main/java/com/like/tag/MainActivity.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.view.Menu
88
import android.view.MenuItem
99
import android.widget.Toast
1010
import androidx.appcompat.app.AppCompatActivity
11+
import androidx.core.content.ContextCompat
1112
import com.core.util.dp
1213
import com.core.util.sp
1314
import com.view.text.*
@@ -232,7 +233,11 @@ class MainActivity : AppCompatActivity() {
232233
endGradientBackgroundColor = Color.parseColor("#DE4313")
233234
marginLeft = 20.dp
234235
align = Align.BASELINE
235-
}).replaceTag("荣耀", LayoutInflater.from(this).inflate(R.layout.custom_tag_view, null), align = Align.TOP)
236+
}).replaceTag(
237+
"荣耀",
238+
LayoutInflater.from(this).inflate(R.layout.custom_tag_view, null),
239+
align = Align.TOP
240+
)
236241

237242
//1.15 对齐方式,底部对齐
238243
val tv15Config = TagConfig(Type.TEXT).apply {
@@ -248,6 +253,17 @@ class MainActivity : AppCompatActivity() {
248253
text_tv15.addTag(tv15Config) {
249254
Toast.makeText(this, "这里是解释", Toast.LENGTH_LONG).show()
250255
}
256+
257+
//1.6 自定义背景图片
258+
val tv16Config = TagConfig(Type.TEXT).apply {
259+
text = "新品"
260+
leftTopRadius = 10.dp.toFloat()
261+
width = 40.dp
262+
height = 20.dp
263+
backgroundDrawable =
264+
ContextCompat.getDrawable(this@MainActivity, R.mipmap.custom_img)
265+
}
266+
text_tv16.addTag(tv16Config)
251267
}
252268

253269
/**

0 commit comments

Comments
 (0)