自定义控件:雷达图

学习了大神的源代码(奈何不知大神的博客地址o(╥﹏╥)o),觉得必须记录一下,方便以后再次学习。

效果如图所示:

1.自定义雷达图控件:

public class MyPolygonView extends View {

    //-------------我们必须给的模拟数据-------------
    //n边形
    private int n = 6;
    //每个角对应的文字
    private String[] text = new String[]{"语文", "数学", "英语", "生物", "化学","物理"};
    //区域等级,值不能超过n边形的个数(每个角对应的值到达的层数)
    private int[] area = new int[]{3,3,2,2,3,2};

    //-------------View相关-------------
    //View自身的宽和高
    private int mHeight;
    private int mWidth;

    //-------------画笔相关-------------
    //边框的画笔
    private Paint borderPaint;
    //文字的画笔
    private Paint textPaint;
    //区域的画笔
    private Paint areaPaint;

    //-------------多边形相关-------------
    //n边形个数
    private int num = 4;
    //两个多边形之间的半径
    private int r = 60;
    //n边形顶点坐标
    private float x, y;
    //n边形角度
    private float angle = (float) ((2 * Math.PI) / n);
    //文字与边框的边距等级,值越大边距越小(文字与边框的距离)
    private int textAlign = 5;

    //-------------颜色相关-------------
    //边框颜色(整个n边型的区域颜色)
    private int mColor = getResources().getColor(R.color.app_polygon);
    //文字颜色
    private int textColor = getResources().getColor(R.color.app_black);
    //区域颜色(整个连线的颜色)
    private int strengthColor = Color.parseColor("#f9c172");



    public MyPolygonView(Context context) {
        super(context);
    }

    public MyPolygonView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyPolygonView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //初始化画笔
        initPaint();
        //画布移到中心点
        canvas.translate(mWidth / 2, mHeight / 2);
        //画n边形
        drawPolygon(canvas);
        //画n边形的中点到顶点的线
        drawLine(canvas);
        //画文字
        drawText(canvas);
        //画蓝色区域
        drawArea(canvas);
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        //边框画笔
        borderPaint = new Paint();
        borderPaint.setAntiAlias(true);
        borderPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        borderPaint.setColor(mColor);
        borderPaint.setStrokeWidth(3);

        //文字画笔
        textPaint = new Paint();
        textPaint.setTextSize(30);
        textPaint.setColor(textColor);
        textPaint.setAntiAlias(true);

        //区域画笔
        areaPaint = new Paint();
        areaPaint.setStrokeWidth(5);
        areaPaint.setColor(strengthColor);
        areaPaint.setAntiAlias(true);
        areaPaint.setStyle(Paint.Style.STROKE);
    }

    /**
     * 绘制多边形
     *
     * @param canvas
     */
    private void drawPolygon(Canvas canvas) {
        Path path = new Path();
        //n边形数目
        for (int j = 1; j <= num; j++) {
            float r = j * this.r;
            path.reset();
            //画n边形
            for (int i = 1; i <= n; i++) {
                x = (float) (Math.cos(i * angle) * r);
                y = (float) (Math.sin(i * angle) * r);
                if (i == 1) {
                    path.moveTo(x, y);
                } else {
                    path.lineTo(x, y);
                }
            }
            //关闭当前轮廓。如果当前点不等于第一个点的轮廓,一条线段是自动添加的
            path.close();
            canvas.drawPath(path, borderPaint);
        }
    }

    /**
     * 画多边形线段
     *
     * @param canvas
     */
    private void drawLine(Canvas canvas) {
        Path path = new Path();
        float r = num * this.r;
        for (int i = 1; i <= n; i++) {
            path.reset();
            x = (float) (Math.cos(i * angle) * r);
            y = (float) (Math.sin(i * angle) * r);
            path.lineTo(x, y);
            canvas.drawPath(path, borderPaint);
        }
    }

    /**
     * 画文字
     *
     * @param canvas
     */
    private void drawText(Canvas canvas) {
        float r = num * this.r;
        for (int i = 1; i <= n; i++) {
            //测量文字的宽高
            Rect rect = new Rect();
            textPaint.getTextBounds(text[i - 1], 0, text[i - 1].length(), rect);
            float textWidth = rect.width();
            float textHeight = rect.height();

            x = (float) (Math.cos(i * angle) * r);
            y = (float) (Math.sin(i * angle) * r);
            //位置微调
            if (x < 0) {
                x = x - textWidth;
            }
            if (y > 25) {
                y = y + textHeight;
            }
            //调文字与边框的边距
            float LastX = x + x / num / textAlign;
            float LastY = y + y / num / textAlign;
            canvas.drawText(text[i - 1],LastX, LastY, textPaint);
        }
    }

    /**
     * 画区域
     *
     * @param canvas
     */
    private void drawArea(Canvas canvas) {
        Path path = new Path();
        for (int  i= 1;  i<= n; i++) {
            float r = area[i - 1] * this.r;
            x = (float) (Math.cos(i * angle) * r);
            y = (float) (Math.sin(i * angle) * r);
            if (i == 1) {
                path.moveTo(x, y);
            } else {
                path.lineTo(x, y);
            }
        }
        //关闭当前轮廓。如果当前点不等于第一个点的轮廓,一条线段是自动添加的
        path.close();
        canvas.drawPath(path, areaPaint);
    }
    public void setArea (int[] area){
        this.area =area;
        invalidate();
    }

}

2.界面布局文件xml中直接使用:

  <com.lotus.chartspagedemo.MyPolygonView
                android:id="@+id/polygon"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:visibility="visible" />


3.界面activity中可以设置控件颜色:

polygon.setBackgroundColor(getResources().getColor(R.color.app_blue));//雷达图的背景颜色

如果不设置背景颜色,效果就是:

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LVGL(Light and Versatile Graphics Library)是一个开源的GUI库,可用于嵌入式系统。它提供了丰富的功能和易于使用的API,可以帮助开发者快速构建用户界面。在LVGL中,可以通过自定义控件来扩展其功能。下面是关于如何自定义一个雷达控件的说明。 首先,为了实现雷达控件,我们需要绘制一个圆形的背景。可以使用LVGL的绘图函数来绘制一个圆和外部的圆环。可以设置一些属性,如颜色、线宽等。 其次,为了实现雷达效果,需要在圆形的背景上绘制多个射线。可以使用LVGL的直线绘图函数来绘制这些射线。可以设置不同的角度和长度,来实现雷达扫描的效果。 然后,在绘制射线的同时,可以在每个射线的末端绘制一个小圆点,用于表示目标。可以根据具体需求设置小圆点的颜色和大小。 另外,为了使雷达控件更加实用,可以通过添加一些交互功能来增加其灵活性。可以通过触摸屏等输入设备来实现交互,例如点击某个小圆点可以显示详细信息。 最后,在绘制完成后,可以在LVGL的主循环中更新雷达控件,使其实时更新显示。可以通过修改射线的角度和长度,以及小圆点的位置来实现动画效果。 总结起来,自定义雷达控件需要使用LVGL的绘图函数来绘制背景、射线和小圆点,并添加交互功能来增加灵活性。通过不断更新控件的显示内容,可以实现雷达扫描效果和动画效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值