自定义View画五角星

package com.example.a03_five_pointed_star.fivepointedstar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by yujie on 2017/11/30.
 */

public class DrawView extends View {
    public DrawView(Context context) {
        super(context);
    }

    public DrawView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        double t = (1+(Math.tan(Math.PI/10)*Math.tan(Math.PI/10)))/(3-(Math.tan(Math.PI/10)*Math.tan(Math.PI/10)));
        float a_x = 0;
        float a_y = -1;

        float b_x = (float) Math.cos(Math.PI/10);
        float b_y = (float) -Math.sin(Math.PI/10);

        float c_x = (float) Math.cos(Math.PI*3/10);
        float c_y = (float) Math.sin(Math.PI*3/10);

        float d_x = (float) -Math.cos(Math.PI*3/10);
        float d_y = (float) Math.sin(Math.PI*3/10);

        float e_x = (float) -Math.cos(Math.PI/10);
        float e_y = (float) -Math.sin(Math.PI/10);

        float aa_x = 0;
        float aa_y = (float) t;

        float bb_x = (float) (-t * (Math.cos(Math.PI/10)));
        float bb_y = (float) (t * (Math.sin(Math.PI/10)));

        float cc_x = (float) (-t * (Math.cos(Math.PI*3/10)));
        float cc_y = (float) (-t * (Math.sin(Math.PI*3/10)));

        float dd_x = (float) (t * (Math.cos(Math.PI*3/10)));
        float dd_y = (float) (-t * (Math.sin(Math.PI*3/10)));

        float ee_x = (float) (t * (Math.cos(Math.PI/10)));
        float ee_y = (float) (t * (Math.sin(Math.PI/10)));

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(5);

        float[] lines = {(a_x+1)*200, (a_y+1)*200, (cc_x+1)*200, (cc_y+1)*200,
                (cc_x+1)*200, (cc_y+1)*200, (e_x+1)*200, (e_y+1)*200,
                (e_x+1)*200, (e_y+1)*200, (bb_x+1)*200, (bb_y+1)*200,
                (bb_x+1)*200, (bb_y+1)*200, (d_x+1)*200, (d_y+1)*200,
                (d_x+1)*200, (d_y+1)*200, (aa_x+1)*200, (aa_y+1)*200,
                (aa_x+1)*200, (aa_y+1)*200, (c_x+1)*200, (c_y+1)*200,
                (c_x+1)*200, (c_y+1)*200, (ee_x+1)*200, (ee_y+1)*200,
                (ee_x+1)*200, (ee_y+1)*200, (b_x+1)*200, (b_y+1)*200,
                (b_x+1)*200, (b_y+1)*200, (dd_x+1)*200, (dd_y+1)*200,
                (dd_x+1)*200, (dd_y+1)*200, (a_x+1)*200, (a_y+1)*200};

        canvas.drawLines(lines, paint);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值