Shape

Android 中的 java代码 shape讲解

1.类型


2.详解

   (1)Shape的详解

            Shape是其他类的基类,可以绘制任何的集合图形,通过自己德尔ondraw()方法。

     

public class  MyView extends View
{

    public MyView(Context context) {
        super(context);

    }
    Shape shape=new Shape() {
        @Override
        public void draw(Canvas canvas, Paint paint) {
           

            canvas.drawCircle(shape.getWidth(),shape.getHeight(),shape.getWidth()/2,paint);
        }
    };
    @Override
    protected void onDraw(Canvas canvas) {

            super.onDraw(canvas);
            Paint paint=new Paint();
            paint.setColor(Color.BLUE);
            paint.setAntiAlias(true);
            paint.setStrokeWidth(40);
            paint.setStyle(Paint.Style.STROKE);
shape.resize(800,800);//设置大小 shape.draw(canvas, paint); }}

            当你创建一个shape对象,要求重写public void draw(Canvas canvas, Paint paint)方法,shape.draw()执行时需要将画布和画笔传入。


   (2)RectShape(矩形)的详解(shape直接子类)

        

public  class  RectView extends View
{
    RectShape rectShape;
    public RectView(Context context) {
        super(context);

        rectShape=new RectShape();
        rectShape.resize(400, 400);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint=new Paint();

        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
        rectShape.draw(canvas,paint);

    }
}
 
    很简单,只要设定画布,画笔,宽高,即可!
   (3)PathShape(shape的直接子类)
public  class pathView extends View
{
    PathShape pathShape;
    public pathView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.BLUE);
        Path path1=new Path();
        path1.moveTo(100, 100);
        path1.lineTo(200, 200);
        path1.lineTo(400, 800);
        //path1.close();
        Paint paint=new Paint();
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);

        pathShape=new PathShape(path1,100,100);
        pathShape.resize(300,300);
        pathShape.draw(canvas,paint);

    }
}
1.必须要调用pathShape.resize(300,300)方法,否则无法显示。
2.pathShape=new PathShape(path1,stdwidth,stdheight);这个里边的宽高为标准宽高。
  以后通过pathShape.resize(width,height)来修改宽高,
  宽为:canvas.scale(width/stdwidth)
  高为:canvas.scale(height/stdheight)

 

   (3)ArcShape的详解shape直接子类

public  class ArcView extends View
{

    public ArcView(Context context) {
        super(context);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        ArcShape arcShape=new ArcShape(0,-90);
        arcShape.resize(500,800);
        Paint paint=new Paint();
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);

        arcShape.draw(canvas,paint);

    }
}
1.必须制定尺寸 arcShape.resize(500,800);

2.以制定的尺寸所形成的集合图形的中心进行扇形截图


   (4)OvalShape(圆形或者椭圆)shape直接子类

public  class OvalView extends View
{

    public OvalView(Context context) {
        super(context);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        OvalShape ovalShape=new OvalShape();
        ovalShape.resize(500,800);
        Paint paint=new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        ovalShape.draw(canvas,paint);
    }
}

1.必须制定尺寸 arcShape.resize(500,800);

2.以制定的尺寸所形成的集合图形的中心进行绘制圆形或者椭圆

(5)RoundRectShape(圆角矩形)

public class  roundView extends View
{

    public roundView(Context context) {
        super(context);
    }
    RectF r=new RectF(100,100,300,100);
    float jiao[]={20,20,20,20,20,20,20,20};
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        RoundRectShape roundRectShape=new RoundRectShape(jiao,r,jiao);
        roundRectShape.resize(800,800);
        Paint paint=new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        roundRectShape.draw(canvas,paint);
    }
}

1.构造函数中的第一个函数 圆角的度数,前两个为左上角,在两个为右上角,在两个为右下角,在两个为左下角


2.第二个参数为,RectF(),他会把这个矩形套在外边矩形内部。注意:

RectF(100,100,300,100)的四个参数是距离外部矩形的边的长度

3.第三个参数为内部矩形的圆角,如果第二个参数为空,第三个参数直接被忽略、



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值