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); } }
自定义View画五角星
最新推荐文章于 2021-06-02 16:51:22 发布