两种方式实现圆形矩阵--自定义view和shape

第一种 自定义view

 //自定义view的宽高尺寸
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       // super.onMeasure(widthMeasureSpec, heightMeasureSpec);
       width=MeasureSpec.getSize(widthMeasureSpec);
        height=MeasureSpec.getSize(heightMeasureSpec);
        int minWidth=150;
        int minHeight=100;

        if (width<minWidth){
            width=minWidth;
        }
        if(height<minHeight){
            height=minHeight;
        }
        setMeasuredDimension(width,height);

    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setAntiAlias(true);                       //设置画笔为无锯齿
        paint.setColor(Color.WHITE);                    //设置画笔颜色
        //canvas.drawColor(Color.WHITE);                  //白色背景
        paint.setStrokeWidth((float) 3.0);              //线宽
        paint.setStyle(Paint.Style.FILL);                   //实心效果
        RectF rectF= new RectF(0,0,width,height);
        canvas.drawRoundRect(rectF,30,30,paint);
    }

第二种 shape 在drawable下新建一个shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- solid指定形状的填充色,只有android:color一个属性 -->
    <solid android:color="#FFFFFF" />
    <!-- padding设置内容区域离边界的间距 -->
    <!-- corners设置圆角,只适用于rectangle -->
    <corners android:radius="200dp" />

</shape>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容