android.graphics(一)shapes

来源:互联网 发布:提示mac中毒 mackeeper 编辑:程序博客网 时间:2024/06/03 19:54

这里写图片描述
先看下shapes的结构

PathShape extends ShapeRectShape extends ShapeArcShape extends RectShapeOvalShape extends RectShapeRoundRectShape extends RectShape

这里写图片描述

其中:

ArcShape:扇形;
OvalShape:椭圆形;
RoundRectShape :圆角矩形;
PathShape:指定曲线图形;

1、ArcShape

ArcShape(float startAngle, float sweepAngle)

startAngel
为直角坐标系中起始位置角度(如45,代表45°角位置)

sweepAngel
为起始位置和椭圆中心连线在直角坐标系中扫过的角度(如-135°,代表连线逆时针扫过135°角)

        ArcShape arcShape = new ArcShape(30, 180);        ShapeDrawable arcShapeDrawable = new ShapeDrawable(arcShape);        //指定填充颜色        arcShapeDrawable.getPaint().setColor(Color.RED);        // 指定填充模式FILL,STROKE,FILL_AND_STROKE        arcShapeDrawable.getPaint().setStyle(Paint.Style.FILL);        findViewById(R.id.textView1).setBackgroundDrawable(arcShapeDrawable);

2、OvalShape

OvalShape是指给定的矩形框的内切椭圆饼

        OvalShape ovalShape = new OvalShape();        ShapeDrawable ovalShapeDrawable = new ShapeDrawable(ovalShape);        ovalShapeDrawable.getPaint().setColor(Color.RED);        ovalShapeDrawable.getPaint().setStyle(Paint.Style.FILL);        findViewById(R.id.textView2).setBackgroundDrawable(ovalShapeDrawable);

3、RoundRectShape

这里写图片描述

RoundRectShape(float[] outerRadii, RectF inset,float[] innerRadii)

outerRadii:指定一个外部(圆角)矩形
inset:内部矩形与外部矩形的距离
innerRadii:一个可选的 内部(圆角)矩形

一个包含8个弧度值,指定外部圆角矩形的 4个角部的弧度

 new float[] {l, l, t, t, r, r, b, b};参数: 12代表左上角, 34代表右上角, 56代表右下角, 78代表左下角,如果没弧度的话,传入null即可。
// 外部矩形弧度float[] outerRadii = new float[]{8, 8, 8, 8, 8, 8, 8, 8};// 内部矩形与外部矩形的距离RectF inset = new RectF(50, 20, 50, 50);// 内部矩形弧度float[] innerRadii = new float[]{20, 20, 20, 20, 20, 20, 20, 20};RoundRectShape roundRectShape = new RoundRectShape(outerRadii, inset, innerRadii);ShapeDrawable roundRectShapeDrawable = new ShapeDrawable(roundRectShape);        roundRectShapeDrawable.getPaint().setColor(Color.RED);        roundRectShapeDrawable.getPaint().setStyle(Paint.Style.FILL);        findViewById(R.id.textView3).setBackgroundDrawable(roundRectShapeDrawable);

4、PathShape

路径绘图

PathShape(Path path, float stdWidth, float stdHeight)

stdWidth:标准宽度
stdHeight:标准高度

        Path path = new Path();        path.moveTo(50, 0);        path.lineTo(0, 50);        path.lineTo(50, 100);        path.lineTo(100, 50);        path.lineTo(50, 0);        PathShape pathShape = new PathShape(path, 200, 100);        ShapeDrawable drawable = new ShapeDrawable(pathShape);        drawable.getPaint().setColor(Color.RED);        drawable.getPaint().setStyle(Paint.Style.FILL);        findViewById(R.id.textView4).setBackgroundDrawable(drawable);

参考:
http://blog.csdn.net/vqqyuan/article/details/42921315
http://blog.csdn.net/zhangphil/article/details/52025152
http://blog.csdn.net/silk2018/article/details/19192329
http://blog.csdn.net/jjwwmlp456/article/details/46928859