自定义TextView 插入图片调整大小

就是在原有的基础上进行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" />

做个记录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值