就是在原有的基础上进行textView的调整以及修改,方便我们在使用drawableStart \ end 的时候进行图片大小的调整,然后直接上代码
/**
* @ClassName: TextImage
* @Description:
* @CreateDate: 2023/7/18 11:42
* @Creator: For the sins I am about to commit, may James Gosling forgive me
*/
class TextImage : AppCompatTextView {
private var mStartWidth: Int = 0
private var mStartHeight: Int = 0
private var mTopWidth: Int = 0
private var mTopHeight: Int = 0
private var mEndWidth: Int = 0
private var mEndHeight: Int = 0
private var mBottomWidth: Int = 0
private var mBottomHeight: Int = 0
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init(context, attrs)
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init(context, attrs)
}
fun init(context: Context, attrs: AttributeSet) {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextImageView)
mStartWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableStartWidth, 0)
mStartHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableStartHeight, 0)
mTopWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableTopWidth, 0)
mTopHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableTopHeight, 0)
mEndWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableEndWidth, 0)
mEndHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableEndHeight, 0)
mBottomWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableBottomWidth, 0)
mBottomHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableBottomHeight, 0)
typedArray.recycle()
setDrawablesSize()
}
private fun setDrawablesSize() {
val compoundDrawables = compoundDrawablesRelative
for (i in compoundDrawables.indices) {
when (i) {
0 -> setDrawableBounds(compoundDrawables[0], mStartWidth, mStartHeight)
1 -> setDrawableBounds(compoundDrawables[1], mTopWidth, mTopHeight)
2 -> setDrawableBounds(compoundDrawables[2], mEndWidth, mEndHeight)
3 -> setDrawableBounds(compoundDrawables[3], mBottomWidth, mBottomHeight)
else -> {
}
}
}
setCompoundDrawablesRelative(
compoundDrawables[0],
compoundDrawables[1],
compoundDrawables[2],
compoundDrawables[3]
)
}
private fun setDrawableBounds(drawable: Drawable?, width: Int, height: Int) {
if (drawable != null) {
drawable.setBounds(0, 0, width, height)
if (width == 0 || height == 0) {
val scale = drawable.intrinsicHeight.toDouble() / drawable.intrinsicWidth.toDouble()
val bounds = drawable.bounds
//高宽只给一个值时,自适应
if (width == 0 && height != 0) {
bounds.right = (bounds.bottom / scale) as Int
drawable.bounds = bounds
}
if (width != 0 && height == 0) {
bounds.bottom = (bounds.right * scale) as Int
drawable.bounds = bounds
}
}
}
}
}
style的属性,使用上下左右的的图片,就用对应上下左右的宽高
<declare-styleable name="TextImageView">
<attr name="drawableStartWidth" format="dimension"/>
<attr name="drawableStartHeight" format="dimension"/>
<attr name="drawableTopWidth" format="dimension"/>
<attr name="drawableTopHeight" format="dimension"/>
<attr name="drawableEndWidth" format="dimension"/>
<attr name="drawableEndHeight" format="dimension"/>
<attr name="drawableBottomWidth" format="dimension"/>
<attr name="drawableBottomHeight" format="dimension"/>
</declare-styleable>
实际使用方法
<com.xxxx.xxxx.xxxx.TextImage
android:id="@+id/fgrth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:drawableStart="@mipmap/logo"
> app:drawableStartHeight="20dp"
> app:drawableStartWidth="20dp"
android:textColor="#009944"
android:drawablePadding="7dp"
android:textStyle="bold"
android:text="@string/app_name"
tools:ignore="ContentDescription" />
做个记录