自定义TextView之 给你的TextView添加边框

来源:互联网 发布:网上买旧书知乎 编辑:程序博客网 时间:2024/06/04 19:32

自定义TextView 之给你的TextView添加边框


 

1.首先添加自定义属性,在values 文件夹下添加attr.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="type" format="string"></attr>
    <attr name="borderColor" format="color"></attr>
    <declare-styleable name="BorderTextView">
        <attr name="type"/>
        <attr name="borderColor"/>
    </declare-styleable>
</resources>

属性含义:

type得到边框类型

boderColor设置边框颜色

 

 

2.在自定义TextView类下获取属性 给属性设置值

public class BorderTextView extends TextView {
    private int boderColor;//边框颜色
    private String boderType;//边框类型
    private Paint mPaint;//画笔

    private Bitmap bitmap;//位图 画边框的位图
    public BorderTextView(Context context) {
        this(context,null);
    }

    public BorderTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BorderTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取属性
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs,R.styleable.BorderTextView,defStyleAttr,0);
        int n = ta.getIndexCount();
        for(int i = 0; i < n ;i++){
            int attr = ta.getIndex(i);
            switch (attr){
                case R.styleable.BorderTextView_borderColor:
                    boderColor = ta.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.BorderTextView_type:
                    boderType = ta.getString(attr);

                    break;
            }
        }
        ta.recycle();
        //默认矩形 当你不设置bodertype属性时候 默认类型设置rect 不给值使用时候会报空指针异常
        if(boderType == null){
            boderType "rect";
        }

      //设置画笔状态
        mPaint new Paint();
        mPaint.setColor(boderColor);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE); // 设置空心
        mPaint.setStrokeWidth(5);

       // initView();
    }

 

 

3.接下来重写onDraw方法

@Override
protected void onDraw(Canvas canvas) {

//获取textview自己的画笔
    Paint p = getPaint();

//得到textview 里设置的字体的尺寸
    float size = p.getTextSize();

//重新设置尺寸为原来的一半 为了保证字体可以 在边框内部有足够空间
    p.setTextSize(size/2);


    int x,y;//文本显示的坐标
    Rect bound = new Rect();//获取文本的宽高
    p.getTextBounds(getText().toString(),0,getText().toString().length(),bound);

   //计算文本的坐标 让文本居中在边框内部
    x = getMeasuredWidth()/- bound.width()/2;
    y = getMeasuredHeight()/+ bound.height()/2;

  //根据bodertype类型 来绘制边框 
    if(boderType.equals("rect")){
        drawRect();
    }else if(boderType.equals("roundrect")){
        drawRoundRect();
    }else if(boderType.equals("oval")){
        drawOval();
    }   

//将保存边框的bitmap 绘制出来
    canvas.drawBitmap(bitmap,0,0,null);

//保存以上绘制内容
    canvas.save();
   //绘制文字
   canvas.drawText(getText().toString(),x,y,p);
   //将文字 与bitmap内容一起绘制出来
    canvas.restore();
}

 

 

//绘制圆角矩形 绘制在bitmap上 最后 在ondraw方法里直接使用bitmap 绘制出边框图像

private void drawRoundRect(){
    bitmap = Bitmap.createBitmap(getMeasuredWidth(),getMeasuredHeight(),  Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    RectF rect = new RectF(  0,
            0,
            getMeasuredWidth(),
            getMeasuredHeight());
    canvas.drawRoundRect(rect,
            3030,
            mPaint);
}

//绘制矩形
private void drawRect( ){
    bitmap = Bitmap.createBitmap(getMeasuredWidth(),getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawRect(
            0,
            0,
            getMeasuredWidth(),
            getMeasuredHeight(),
            mPaint);
}

//绘制椭圆 
private void drawOval(){

//椭圆绘制的区域应该小于矩形的区域 所以不能再0,0出绘制 否则边框显示不全
    RectF rel1 = new RectF(10,10,getMeasuredWidth()-10,getMeasuredHeight()-10);
    //绘制椭圆

    bitmap = Bitmap.createBitmap(getMeasuredWidth(),getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawOval(rel1,mPaint);

}

 

 

源码下载地址:https://github.com/gacmy/BorderTextView/

0 0
原创粉丝点击