Android自定义view设置自定义属性,计算文本基线,判断测量模式

自定义属性首先需要在values目录下attr.xml下定义,新项目可能没有,需要自己创建即可

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TestView01">
        <attr name="LColor" format="color"></attr>
        <attr name="RColor" format="color"></attr>
        <attr name="TxtColor" format="color"></attr>
        <attr name="Kuan" format="dimension"></attr>
    </declare-styleable>
</resources>

自定义属性设置完毕既可以直接在xml布局里面添加属性
然后自定义view里面获取属性值

public class TestView01 extends View {

    private Paint paint;
    private int LColor=Color.RED;   //左圈颜色
    private int RColor=Color.BLUE;   //右圈颜色
    private int TxtColor=Color.BLACK;   //文本颜色
    private int kuan=10;    //圆弧宽度

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

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

    public TestView01(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }

    public TestView01(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        TypedArray butes = context.obtainStyledAttributes(attrs,R.styleable.TestView01);
        LColor = butes.getColor(R.styleable.TestView01_LColor, LColor);
        RColor = butes.getColor(R.styleable.TestView01_RColor, RColor);
        TxtColor = butes.getColor(R.styleable.TestView01_TxtColor, TxtColor);
        kuan = butes.getColor(R.styleable.TestView01_Kuan,kuan);
    }
}

判断测量模式,通过画笔测量文本内容大小,重新设置大小

 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        /*MeasureSpec.UNSPECIFIED 不确定值,铺满
        MeasureSpec.AT_MOST 最大值,包裹内容
        MeasureSpec.EXACTLY 完全准确值*/

        int modeW = MeasureSpec.getMode(widthMeasureSpec);   //获取测量模式
        int modeH = MeasureSpec.getMode(heightMeasureSpec);
        if(modeW==MeasureSpec.AT_MOST){    //如果模式等于wrap_content 需要手动赋值

            Rect bounds=new Rect();
            paint.getTextBounds("内容",0,"内容".length(),bounds); //通过画笔获取文本的Rect大小
            bounds.width();//获得的宽
            bounds.height();//获得的高

        }

        setMeasuredDimension(widthMeasureSpec,heightMeasureSpec);   // 重新设置大小
    }

计算文本基线

 paint = new Paint();
        paint.setAntiAlias(true);  //抗锯齿打开
        paint.setColor(Color.BLACK);   //设置画笔的颜色
        paint.setTextSize(100);     //设置画笔的大小


@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt(); //获取文字排版信息
        int dy=(fontMetricsInt.bottom-fontMetricsInt.top)-fontMetricsInt.bottom;
        int baseLins = getHeight() / 2 + dy;                //获得文本基线


        canvas.drawText("内容",0,getHeight()/2+baseLins,paint);
        
        //invalidate();   重新绘制
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容