TextVeiw 的自定义View

本文介绍了一种自定义View实现考勤页面效果的方法。通过Canvas API绘制圆形,并结合Paint对象设置样式,同时实现了在圆内展示考勤类型数字及下方文字说明的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据项目需求需要制作出下图的页面效果:

反映当前页面的考勤情况,用了自定义View实现,红色原片中的数字和下方的考勤类型说明文字都提供方法设置,在使用canvas.drawCircle()画圆时,刚开始把圆心坐标设置为(0,0),出来的效果是只出现四分之一的圆,把圆心坐标设置为半径的1/2,方全部显示,以下为源码:

/**
 * Created by public on 2016/11/25.
 * 自定义画圆view
 * 设置宽度与半径有关
 */
public class CustomCircleView extends View{
    private Paint paint;
    private String text_type_code;//考勤类型 数字码
    private String text_type_str;//考勤类型 文字表示
    private int width;
    private int height;
    private int radius;//半径大小
    private Rect rect;

    public CustomCircleView(Context context, AttributeSet attrs) {
        super(context,attrs);

        paint=new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setAntiAlias(true);//抗锯齿
        /**
         * 画笔样式分三种: 1.Paint.Style.STROKE:描边 2.Paint.Style.FILL_AND_STROKE:描边并填充
         * 3.Paint.Style.FILL:填充
         */
        paint.setStyle(Paint.Style.FILL);

        rect=new Rect();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        width=getWidth();
        height=getHeight();
        radius=width-60;

        //画圆(圆心x坐标,圆心y坐标,半径,画笔)
        paint.setColor(Color.RED);//设置颜色为红色
        canvas.drawCircle(radius/2,radius/2,radius/2,paint);

        //画数字
        paint.setColor(Color.WHITE);
        paint.setTextSize(50);
        paint.getTextBounds(text_type_code,0,text_type_code.length(),rect);
        float textCodeWidth=rect.width();
        float textCodeHeight=rect.height();
        canvas.drawText(text_type_code,radius/2-textCodeWidth/2-3,radius/2+textCodeHeight/2,paint);

        //画文字
        paint.setColor(Color.BLACK);
        paint.getTextBounds(text_type_str,0,text_type_str.length(),rect);
        float textStrWidth=rect.width();
        float textStrHeight=rect.height();
        canvas.drawText(text_type_str,radius/2-textStrWidth/2,radius+textStrHeight+10,paint);
    }

    /**
     * 考勤类型 数字码
     * @param code
     */
    public void setTypeTextCode(String code){
        text_type_code=code;
        invalidate();
    }

    /**
     * 考勤类型
     * @param text
     */
    public void setTypeTextStr(String text){
        text_type_str=text;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值