Android 自定义View PolygonsView多边形能力分析控件

    xiaoxiao2021-03-25  88

    这里就不讲View的绘制流程,就讲实现方法,关于View的绘制流程,到时专门写一篇。 先看下效果图: 这里主要的就是绘制多边形的算法: 其实很简单 根据角度计算出顶点的坐标(r为半径,angle为平均角度) x = r*Math.cos(i*angle); y = r*Math.sin(i*angle); 这样就可以绘制出所有点的坐标。 先看下面绘制最外层多边形的代码: 类似的方法缩短下半径r就可以画出第一层,第二层的多边形。 接着就是画能力线的算法: 与上面的一样,其实就是r的大小不同; 接着就是绘制文字的算法: 主要要控制文字摆放的位置 1.要在多边形外,所以r要增大点 2.文字的长度会影响摆放的位置,画到多边形里,所以可以减小x坐标调整,按字体大小跟字符串个数调x=x-size*count 下面就是主要代码: /** * Created by Administrator on 2017/3/9. */ public class PolygonsView extends View { //view的宽 private int viewWidth; //view的高 private int viewHeight; //默认宽 private int defaultWidth = 200; //默认高 private int defaultHeight = 200; //中心平均角度 private double angle ; //半径长度 private int r ; //边数 private int polygonsEdgeCount; //中心点 private Point center; //画笔 private Paint mpaint; //字体颜色 private int textColor; //能力线条颜色 private int dataPolygonsColor; //多边形颜色 private int polygonsColor; //默认字体颜色 private static int DEFAULT_TEXT_COLOR = 0xff00bfff; //默认能力线颜色 private static int DEFAULT_DATA_POLYGONS_COLOR = 0xff00bfff; //默认多变形颜色 private static int DEFAULT_POLYGONS_COLOR = 0x00bfff; //默认边数 private static int DEFAULT_POLYGONS_EDGE_COUNT = 3; private ArrayList attrStr; private ArrayList percentageS; public PolygonsView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context,attrs); } public PolygonsView(Context context) { this(context,null); } public PolygonsView(Context context, AttributeSet attrs) { this(context, attrs,0); } private void init(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PolygonsView); textColor = a.getColor(R.styleable.PolygonsView_textColor,DEFAULT_TEXT_COLOR); dataPolygonsColor = a.getColor(R.styleable.PolygonsView_dataPolygonsColor,DEFAULT_DATA_POLYGONS_COLOR); polygonsColor = a.getColor(R.styleable.PolygonsView_polygonsColor,DEFAULT_POLYGONS_COLOR); polygonsEdgeCount = a.getInt(R.styleable.PolygonsView_polygonsEdgeCount,DEFAULT_POLYGONS_EDGE_COUNT); polygonsEdgeCount = polygonsEdgeCount<3 ? 3:polygonsEdgeCount; angle = (double)Math.PI*2/polygonsEdgeCount; center = new Point(); mpaint = new Paint(Paint.ANTI_ALIAS_FLAG); defaultHeight = dip2px(context,defaultHeight); defaultWidth = dip2px(context,defaultWidth); //去锯齿 mpaint.setAntiAlias(true); mpaint.setColor(Color.BLUE); mpaint.setStyle(Paint.Style.STROKE); mpaint.setStrokeWidth(1); attrStr = new ArrayList<>(); initArreStr(); percentageS = new ArrayList<>(); initPoints(); } private void initArreStr() { for(int i=0;i list){ attrStr.clear(); for(int i=0;i list){ percentageS.clear(); for(int i=0;i public class MainActivity extends AppCompatActivity { private PolygonsView mView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mView = (PolygonsView) findViewById(R.id.v); ArrayList ad = new ArrayList<>(); ad.add(0.4); ad.add(0.9); ad.add(0.7); ad.add(0.1); ad.add(0.5); ad.add(0.8); ad.add(0.3); mView.setPercentageS(ad); ArrayList ar = new ArrayList<>(); ar.add("ke"); ar.add("liu"); ar.add("ke"); ar.add("liu"); ar.add("ke"); ar.add("liu"); ar.add("ke"); mView.setArrayStr(ar); } }
    转载请注明原文地址: https://ju.6miu.com/read-25810.html

    最新回复(0)