美文网首页Android OtherAndroid开发经验谈Android开发
两种方式实现圆形矩阵--自定义view和shape

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

作者: 芒果味的你呀 | 来源:发表于2017-10-30 20:34 被阅读71次

第一种 自定义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>

相关文章

网友评论

    本文标题:两种方式实现圆形矩阵--自定义view和shape

    本文链接:https://www.haomeiwen.com/subject/odzkpxtx.html