android 自定义TextView知识点总结

自定义TextView 体验后总结关键步骤:

1.

class TextView extends View

自定义类TextView继承自View

2重写父类的构造方法,  还有onMeasure(),onDraw(),onTouchEvent()方法

    private String mText;
    private  int mTextSize = 15;
    private  int mTextColor = Color.BLACK;
    private Paint mPaint;

    //构造函数会在代码里面new的时候调用
    public TextView(Context context) {
        this(context,null);
    }
    //在布局中使用
    public TextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
     //在布局Layout中使用,但是会有style
    public TextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取自定义属性
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextView);
        mText = array.getString(R.styleable.TextView_text);
        mTextColor = array.getColor(R.styleable.TextView_textColor,mTextColor);
        //15 sp or  px
        mTextSize = array.getDimensionPixelSize(R.styleable.TextView_textSize,mTextSize);
        //回收
        array.recycle();

        mPaint = new Paint();
        //抗锯齿
        mPaint.setAntiAlias(true);
        //设置字体的大小和颜色
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mTextColor);
//        setBackground(Color.TRANSPARENT);
        setWillNotDraw(false);
    }

3.最关键的三个方法onMeasure(),onDraw(),onTouchEvent()方法解析

onMeasure()用于测量宽高,就是我们自定义的TextView的宽高,比如说固定的值100dp,或者是warp_content,原理细节在代码中呈现

 /**自定义view的测量方法
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //布局的宽高由这个方法指定---指定控件的宽高需要测量
        //获取宽高德模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //AT_MOST--------在布局中指定了warp_content
        //EXACTLY--------在布局中制定了确切的值 100dp---match_parent  fill_parent
        //UNSPECIFIED----尽可能的大,很少能用到,ListView,Scrollview
        //1.确定的值,这个时候不需要计算,给的多少就是多少
        int width = MeasureSpec.getSize(widthMeasureSpec);
        //在自定义viewgroup的时候会详细讲解--给的是warp_content需要计算
        if (widthMode == MeasureSpec.AT_MOST){
        //计算的宽度与字体的长度有关与字体的大小用画笔来测量
            Rect bounds = new Rect();
            //获取文本的RECT
            mPaint.getTextBounds(mText,0,mText.length(),bounds);
            width = bounds.width()+getPaddingLeft()+getPaddingRight();
        }
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode == MeasureSpec.AT_MOST){
            //计算的宽度与字体的长度有关与字体的大小用画笔来测量
            Rect bounds = new Rect();
            //获取文本的RECT
            mPaint.getTextBounds(mText,0,mText.length(),bounds);
            height = bounds.height()+getPaddingTop()+getPaddingBottom();
        }
        //设置属性的宽高
        setMeasuredDimension(width,height);
    }

onDraw()用于绘制,就是实现TextView的内容呈现,比如文字,大小颜色,显示在界面上等,代码中有一个小细节知识点,就是基线,说白了就是我们上学时候的知识点,拼音在线条中的书写格式,原理代码中看

    //用于绘制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        //画文本
//        canvas.drawText();
//        //画弧形
//        canvas.drawArc();
//        //画圆
//        canvas.drawCircle();

        //画文字 text x y paint
        //x 就是开始的位置 0
        //y 基线 baseline 求? getHeight()/2知道的 centerY
        //dy 代表的是:高度的一半到baseline的距离
        Paint.FontMetricsInt fontMetricsInt = mPaint.getFontMetricsInt();
        //top 是一个负值 bottom是一个正值 top,bottom的值代表是 bottom是baseline到文字底部的距离(正值)
        //必须要清楚的,自己打印可看
        int dy = (fontMetricsInt.bottom-fontMetricsInt.top)/2-fontMetricsInt.bottom;
        int baseline = getHeight()/2+dy;
        int x = getPaddingLeft();
        canvas.drawText(mText,x,baseline,mPaint);
    }

onTouchEvent()触摸事件,比如我们在使用TextView的时候会点击,长按,等动作监听,此处为简单demon没有太多功能,只做了一下他的手指监听操作,看代码

  @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_UP:
                //手指抬起
                Log.e("Tag","手指抬起--");
                break;
            case MotionEvent.ACTION_DOWN:
                //手指按下
                Log.e("Tag","手指按下--");
                break;
            case MotionEvent.ACTION_MOVE:
                //手指移动
                Log.e("Tag","手指移动--");
                break;
        }
        return true;
    }

demon连接地址:https://download.csdn.net/download/u013110200/10758320

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Android自定义TextView中显示全部内容,可以使用以下两种方法: 1. 使用setEllipsize()方法 通过设置setEllipsize()方法,可以在TextView的末尾添加省略号,从而指示文本被截断。你可以使用以下代码来实现: ``` yourTextView.setEllipsize(TextUtils.TruncateAt.END); yourTextView.setSingleLine(true); ``` 上述代码将设置TextView只显示一行并在末尾添加省略号。 2. 自定义TextView 你可以从TextView类继承一个新类,并覆盖onMeasure()方法以测量控件的高度和宽度。 你可以使用以下代码实现: ``` public class CustomTextView extends TextView { public CustomTextView(Context context) { super(context); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //获取TextView的内容 CharSequence text = getText(); if (text != null) { //测量TextView的高度 int width = getMeasuredWidth(); int height = getMeasuredHeight(); int lineCount = getLineCount(); int lineHeight = getLineHeight(); int totalHeight = lineCount * lineHeight; if (totalHeight > height) { setMeasuredDimension(width, totalHeight); } } } } ``` 上述代码将测量TextView的高度,如果文本的高度超出了TextView的高度,则调整TextView的高度以适应文本。然后你可以使用此自定义TextView来显示你的文本。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TobiasLeeBeiJing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值