TextView 在xml 中設置圖片大小

TextView xml中只能設置圖片的位置,但是不能再xml中設置圖片的大小

 android:drawableStart="@drawable/pic"

在代碼中可以啊對textview 的圖片進行設置,主要代碼如下:

//設置圖片的上下左右的位置,也就是寬高
drawable.setBounds(left, top, right, bottom);
//設置textView上下左右的圖片
textView.setCompoundDrawables(startDrawable,topDrawable,endDrawable,bottomDrawable);

這裏介紹一個我根據網上的代碼,修改的自定義的TextView ,可以在xml中設置上下左右圖片的大小,並且支持從RTL 佈局

效果展示

先來看看效果:

在這裏插入圖片描述

使用方法:

            <com.xuexuan.view.TextImageView
                    android:id="@+id/tv_bank_card"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="text的內容"
                    android:drawableStart="@drawable/pic"
                    app:drawableStartHeight="20dp"
                    app:drawableStartWidth="20dp"/>

其中android:drawableStart="@drawable/pic"是設置左邊顯示圖片,app:drawableStartHeight="20dp"app:drawableStartWidth="20dp" 是設置圖片的寬高,注意一定要使用Start、End 而不是Left,Right

源碼

資源屬性

在attrs.xml中添加如下代碼

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <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>
</resources>

自定義TextView控件

/**
 * create by 薛瑄
 * time 2019/5/21 15:20
 */
class TextImageView : 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
                }
            }
        }
    }
}

參考:

https://www.jianshu.com/p/09a5399dc26b

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章