自定义View包含三角 和五角形

//代码部分

public class MyView extends RelativeLayout {
    private float radius=50;
    private Point cPoint;//圆心
    //五角星的五个角
    private Point p1;
    private Point p2;
    private Point p3;
    private Point p4;
    private Point p5;
    private Paint mPaint;
    public MyView(Context context) {
        this(context,null);
    }
    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typearray=context.obtainStyledAttributes(attrs,R.styleable.MyView);
        radius = typearray.getDimension(R.styleable.MyView_text_ban, 50);
        //画五角星的画笔
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);

    }
    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制大圆
        Paint mpaint = new Paint();
        mpaint.setColor(Color.YELLOW);
        mpaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(getWidth()/2,getHeight()/2,155, mpaint);
        // 绘制这个三角形,你可以绘制任意多边形
        Paint zpaint=new Paint();
        Path path = new Path();
        path.moveTo(getWidth()/2, (getHeight()/2)-155);// 此点为多边形的起点
        path.lineTo((getWidth()/2)-137, ((getHeight()/2)+(155/2)));
        path.lineTo((getWidth()/2)+137, ((getHeight()/2)+(155/2)));
        path.close(); // 使这些点构成封闭的多边形
        canvas.drawPath(path, zpaint);
        //绘制小圆
        Paint ypaint=new Paint();
        ypaint.setColor(Color.RED);
        canvas.drawCircle(getWidth()/2,getHeight()/2,77,ypaint);
        //绘制正方形
        Paint paint=new Paint();
        paint.setColor(Color.BLUE);
        //  绘制一个矩形
        Path path1 = new Path();
        //开根
        float v1 = (float)Math.sqrt(2) / 2* 77;
        path1.moveTo(getWidth()/2+v1,  getHeight()/2+v1);// 此点为多边形的起点
        path1.lineTo(getWidth()/2-v1,  getHeight()/2+v1);
        path1.lineTo(getWidth()/2-v1,  getHeight()/2-v1);
        path1.lineTo(getWidth()/2+v1,  getHeight()/2-v1);
        path1.close(); // 使这些点构成封闭的多边形
        canvas.drawPath(path1, paint);
        //画五角星的三个方法
        circle(canvas);
        makep5();
        drawStar(canvas);
    }
    private void circle(Canvas canvas) {
        cPoint = new Point();
        //确定中心
        cPoint.x = getWidth()/2;
        cPoint.y = getHeight()/2;
        //画笔的颜色
        //画圆
        canvas.drawCircle(cPoint.x,cPoint.y,radius,mPaint);
    }
    private void makep5() {
        //上面的坐标
        p1 = new Point();
        p1.x = cPoint.x;
        p1.y =(cPoint.y-radius);
        //url  http://jingyan.baidu.com/article/e4d08ffda964730fd2f60dbd.html
        //计算CF的长度
        double gen5 = Math.sqrt(5);
        double du36 = Math.toRadians(36);
        float cf = (float) Math.sqrt((gen5-1)*radius*0.5*(gen5-1)*radius*0.5+radius*radius);
        float cfy = (float) (cf*Math.sin(du36));
        float cfx = (float) (cf*Math.cos(du36));
        //左上面的坐标
        p2 = new Point();
        p2.x = (p1.x-cfx);
        p2.y = (p1.y+cfy);
        //右上面的坐标
        p3 = new Point();
        p3.x = (p1.x+cfx);
        p3.y = (p1.y+cfy);
        //计算CM的长度
        double du18 = Math.toRadians(18);
        double cm = 2*radius*Math.cos(du18);
        float cmy = (float)(cm*Math.cos(du18));
        float cmx = (float)(cm*Math.sin(du18));
        //左下面的坐标
        p4 = new Point();
        p4.x = (p1.x-cmx);
        p4.y = (p1.y+cmy);
        //右下面的坐标
        p5 = new Point();
        p5.x = (p1.x+cmx);
        p5.y =  (p1.y+cmy);
    }
    private void drawStar(Canvas canvas) {
        canvas.drawLine(p1.x,p1.y,p4.x,p4.y,mPaint);
        canvas.drawLine(p1.x,p1.y,p5.x,p5.y,mPaint);
        canvas.drawLine(p2.x,p2.y,p3.x,p3.y,mPaint);
        canvas.drawLine(p2.x,p2.y,p5.x,p5.y,mPaint);
        canvas.drawLine(p4.x,p4.y,p3.x,p3.y,mPaint);
    }
//Point

public class Point {
    public float x;
    public float y;
//xml

<com.activity.liangweihang_yizhou.MyView
    android:background="#ff55"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />
//attrs

<declare-styleable name="MyView" >
    <attr name="text_ban" format="dimension"/>
</declare-styleable>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值