Android DrawableTextView一個自己經常用到的自定義view

平時開發中, 會遇到文字加圖片的樣式
在這裏插入圖片描述
通常情況實現方法:
1:用一個ImageView+TextView實現
2:使用TextView+其內部的Drawable實現, 如drawableLeft

這兩種方法都不是很滿意, 第一種可以比較好的適配, 但是需要寫兩個view, 設置外面可能還需要再切套一層viewgroup, 第二種可以實現, 但是drawable的大小不好自己控制, 不能比較好的適配, 所以思路是在textview上做修改, 主要爲了比較好的適配

下面貼出textview代碼:

/**
 * Created by Corey_Jia on 2019-06-11.
 */
public class DrawableTextView extends AppCompatTextView {
    public static final int LEFT = 1, TOP = 2, RIGHT = 3, BOTTOM = 4;

    private int mHeight, mWidth;

    private Drawable mDrawable;

    private int mLocation;

    public DrawableTextView(Context context) {
        this(context, null);
    }

    public DrawableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DrawableTextView);
        mWidth = a.getDimensionPixelSize(R.styleable.DrawableTextView_drawable_width, 0);
        mHeight = a.getDimensionPixelSize(R.styleable.DrawableTextView_drawable_height, 0);
        mDrawable = a.getDrawable(R.styleable.DrawableTextView_drawable_src);
        mLocation = a.getInt(R.styleable.DrawableTextView_drawable_location, LEFT);
        a.recycle();
        //繪製Drawable寬高,位置
        drawDrawable();

    }

    /**
     * 繪製Drawable寬高,位置
     */
    public void drawDrawable() {
        if (mDrawable != null) {
            Drawable drawable;
            if (!(mDrawable instanceof BitmapDrawable)) {
                drawable = mDrawable;
            } else {
                Bitmap bitmap = ((BitmapDrawable) mDrawable).getBitmap();
                if (mWidth != 0 && mHeight != 0) {
                    drawable = new BitmapDrawable(getResources(), getBitmap(bitmap, mWidth, mHeight));
                } else {
                    drawable = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true));
                }
            }

            switch (mLocation) {
                case LEFT:
                    this.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
                    break;
                case TOP:
                    this.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
                    break;
                case RIGHT:
                    this.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
                    break;
                case BOTTOM:
                    this.setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable);
                    break;
            }
        }
    }

    /**
     * 縮放圖片
     *
     * @param bm
     * @param newWidth
     * @param newHeight
     * @return
     */
    public Bitmap getBitmap(Bitmap bm, int newWidth, int newHeight) {
        // 獲得圖片的寬高
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 計算縮放比例
        float scaleWidth = (float) newWidth / width;
        float scaleHeight = (float) newHeight / height;
        // 取得想要縮放的matrix參數
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的圖片
        return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
    }

    /**
     * 設置圖片
     * @param res
     */
    public void setImageResource(int res){
        mDrawable = getResources().getDrawable(res, null);
        drawDrawable();
    }

    /**
     * 設置圖片
     * @param res
     */
//    public void setLocationDrawable(int res) {
//        mDrawable = getResources().getDrawable(res, null);
//        drawDrawable();
//    }

    /**
     * 設置圖片
     *
     * @param res
     * @param location
     */
    public void setLocationDrawable(int res, int location) {
        mDrawable = getResources().getDrawable(res, null);
        mLocation = location;
        drawDrawable();
    }

    /**
     * 設置圖片
     * @param res
     * @param location
     * @param width
     * @param height
     */
    public void setLocationDrawable(int res, int location, int width, int height) {
        mDrawable = getResources().getDrawable(res, null);
        mLocation = location;
        mWidth = width;
        mHeight = height;
        drawDrawable();
    }

}

attrs代碼:

<declare-styleable name="DrawableTextView">
        <attr name="drawable_src" format="reference" />
        <attr name="drawable_height" format="dimension" />
        <attr name="drawable_width" format="dimension" />
        <attr name="drawable_location">
            <enum name="left" value="1" />
            <enum name="top" value="2" />
            <enum name="right" value="3" />
            <enum name="bottom" value="4" />
        </attr>
    </declare-styleable>

使用:

<xxx.xxx.xxx.view.DrawableTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="當前沒有數據哦~"
            android:textColor="#ffffffff"
            android:textSize="12sp"
            android:drawablePadding="20dp"
            app:drawable_src="@drawable/icon_nodata"
            app:drawable_location="top"
            app:drawable_width="40dp"
            app:drawable_height="40dp"/>

也可以在代碼中調用, 比較簡單就不貼代碼了

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