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"/>

也可以在代码中调用, 比较简单就不贴代码了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值