自定义View-自定义Text View

1.创建一个TextView.java类继承View
public class TextView extends View
2.创建3个构造方法,将前俩个构造方法的super改成this
//构造函数在代码里面new的时候调用
    public TextView(Context context) {
        this(context,null);
    }

    //在布局layout中使用
    public TextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    //在布局中使用layout中使用,但是会有style
    public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }
 3.在res/values创建attas.xml文件,编写自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--name自定义View名字TextView-->
    <declare-styleable name="TextView">
        <!--name属性名称
        format格式:string文字 color 颜色
                   dimension 宽高 字体大小
        -->
        <attr name="ItJstext" format="string"/>
        <attr name="ItJstextColor" format="color"/>
        <attr name="ItJstestSize" format="dimension"/>
        <attr name="ItJsmaxLength" format="integer"/>
        <!--background自定义View都是继承View,背景是由View管理的-->
<!--        <attr name="background" format="reference|color"/>-->
        <attr name="ItJsinputType">
            <enum name="number" value="1" />
            <enum name="text" value="2" />
            <enum name="password" value="3"/>
        </attr>
    </declare-styleable>
</resources>
4.在布局文件中获取自定义View控件
    <com.itjs.myapplication.TextView
        app:ItJstext="你好"
        app:ItJstextColor="#03A9F4"
        app:ItJsinputType="text"
        app:ItJstestSize="30sp"
        android:padding="60dp"
        android:background="#FF9800"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
5.获取自定义属性
    private String mText;
    private int mTextSize = 15;
    private int mTextColor = Color.BLACK;    
    public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取自定义属性
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextView);
        mTextColor = array.getColor(R.styleable.TextView_ItJstextColor,mTextColor);
        mTextSize =                 
        array.getDimensionPixelSize(R.styleable.TextView_ItJstestSize,sp2px(mTextSize));

        //回收
        array.recycle();
    }

    private int sp2px(int sp) {
        return (int)TypedValue.applyDimension(TypedValue.
                COMPLEX_UNIT_SP,sp,getResources().getDisplayMetrics());
    }
6.指定宽高 onMeasue()(三种测量模式) 

AT_MOST (wrap_content)、EXTRALY(固定值|match_parent)、UNSPECIFIED尽可能的大

    private Paint mPaint;
    public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        //抗锯齿 可以不会那么模糊
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mTextColor);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //布局宽高都是由这个方法指定
        //指定控件的宽高,需要测量
        //获取宽高的模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);  //宽
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);//高

//        1.确定的值,这个时候不需要计算,给的多少就是多少
        int width = MeasureSpec.getSize(widthMeasureSpec);

//        2.给的是warp_content需要计算
        if (widthMode == MeasureSpec.AT_MOST) {
            //计算的宽度 与字体长度有关 与字体大小有关 用画笔测量
            Rect bounds = new Rect();
            //获取文本的Rect
            mPaint.getTextBounds(mText,0,mText.length(),bounds);
            width = bounds.width() + getPaddingLeft() + getPaddingRight();
        }

//        1.确定的值,这个时候不需要计算,给的多少就是多少
        int height = MeasureSpec.getSize(heightMeasureSpec);
//        int height;
//        2.给的是warp_content需要计算
        if (heightMode == MeasureSpec.AT_MOST) {
            //计算的宽度 与字体长度有关 与字体大小有关 用画笔测量
            Rect bounds = new Rect();
            //获取文本的Rect
            mPaint.getTextBounds(mText,0,mText.length(),bounds);
            height = bounds.height() + getPaddingBottom() + getPaddingTop();
        }
        //设置控件的宽高
        setMeasuredDimension(width,height);

    }

7.绘制onDraw()

x就是开始的位置
y 基线

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawText(); //花文本
//        canvas.drawArc(); //画弧
//        canvas.drawCircle(); //画圆

        //画文字 text,x,y,paint
        //x就是开始的位置
        //y 基线

        //by是高度的一半到baseLine的距离
        Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();

        //top是一个负值 bottom是一个正值 bottom代表的是baseLine到文字底部的距离(正值)
        int dy = (fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
        int baseLine = getHeight()/2 + dy;
        int x = getPaddingLeft();
        canvas.drawText(mText,x,baseLine,mPaint);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值