自定义简单TextView

记录自定义TextVIew:
这个主要是为了熟悉自定义View流程,实战意义不大
效果图:这里写图片描述
1.实现构造方法:

public MyTextView(Context context) {
        this(context, null);//重要
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);//重要
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
}

2.在style中实现需要参数:

   <declare-styleable name="MyTextView">
        <attr name="width_size" format="dimension"></attr>
        <attr name="height_size" format="dimension"></attr>
        <attr name="text_size" format="dimension"></attr>
    </declare-styleable>

3.完整代码


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;

@SuppressLint("AppCompatCustomView")
public class MyTextView extends TextView {
    private int widthDefault;
    private int heightDefault;
    private int textSizeDefault;
    private Rect recf;

    public MyTextView(Context context) {
        this(context, null);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
        widthDefault = a.getDimensionPixelSize(R.styleable.MyTextView_width_size,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics()));
        heightDefault = a.getDimensionPixelSize(R.styleable.MyTextView_width_size,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics()));

        textSizeDefault = a.getDimensionPixelSize(R.styleable.MyTextView_text_size,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics()));
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int wideModel = MeasureSpec.getMode(widthMeasureSpec);
        int heightModel = MeasureSpec.getMode(heightMeasureSpec);
        int wideSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (wideModel == MeasureSpec.EXACTLY) {
            widthDefault = wideSize;
        }
        if (heightModel == MeasureSpec.EXACTLY) {
            heightDefault = heightSize;
        }
        setMeasuredDimension(widthDefault, heightDefault);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int r = getMeasuredWidth() / 2;
        int centerX = getLeft() + r;
        int centerY = getTop() + r;
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        recf = new Rect();
        paint.getTextBounds(getText().toString(), 0, getText().toString().length(), recf);
        if (recf.width() > r) {
            paint.setTextSize(
                    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 13, getResources().getDisplayMetrics()));
            paint.getTextBounds(getText().toString(), 0, getText().toString().length(), recf);
        }
        canvas.drawText(getText().toString(), r - recf.width() / 2, centerY, paint);

        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.BLACK);
        canvas.drawCircle(centerX, centerY, r, paint);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值