Android DrawableTextView一个自己经常用到的自定义view

平时开发中, 会遇到文字加图片的样式
Android DrawableTextView一个自己经常用到的自定义view_第1张图片
通常情况实现方法:
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代码:


        
        
        
        
            
            
            
            
        
    

使用:


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

你可能感兴趣的:(Android,自定义view)