在android情节中自定义标签和点

时间:2016-10-04 06:48:43

标签: android androidplot

我正在使用android情节开发应用程序,我正在尝试美化它我希望它像图像

one

但我会在下面

two

1 个答案:

答案 0 :(得分:0)

(这个答案解决了上面示例图像中各个点的内圈的绘制)

通常情况下,这将是一个2或3行作业,只需使用正确配置的LineAndPointFormatter实例重新添加系列即可,但最近发布的版本中存在一个错误,导致同一系列被添加更多与使用相同的渲染器一次,无论是否提供不同的Formatter。 (将在1.2.3版本中修复)

如果你不介意完整复制你的系列数据,你仍然可以很容易地做到这一点:

        XYSeries series2b = new SimpleXYSeries(
                Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");

        LineAndPointFormatter series2bFormat = new LineAndPointFormatter(null, Color.WHITE, null, null);

        // this adjusts the size of the inner circle:
        series2bFormat.getVertexPaint().setStrokeWidth(PixelUtils.dpToPix(7));

        ...

        // make sure this line comes AFTER the addSeries call for the copied series:
        plot.addSeries(series2b, series2bFormat);

上述实现的局限性在于它将绘制一个重复的图例图标(在即将发布的1.2.3版本中也会提到)。避免重复图标的另一种方法是使用自定义LineAndPointFormatter自定义扩展LineAndPointRenderer:

    // a custom renderer to draw an second smaller circle for each rendered vertex
    static class MyLineAndPointRenderer extends LineAndPointRenderer<MyLineAndPointFormatter> {


        public MyLineAndPointRenderer(XYPlot plot) {
            super(plot);
        }

        @Override
        protected void renderPoints(Canvas canvas, RectF plotArea, XYSeries series, List<PointF> points,
                LineAndPointFormatter formatter) {
            // draw points as normal:
            super.renderPoints(canvas, plotArea, series, points, formatter);

            // now draw our inner vertices on top of the previously drawn vertices:
            for (PointF p : points) {
                Paint paint = ((MyLineAndPointFormatter) formatter).getInnerPointPaint();
                canvas.drawPoint(p.x, p.y, paint);
            }
        }
    }

    // defines the format for our custom renderer:
    static class MyLineAndPointFormatter extends LineAndPointFormatter {

        Paint innerPointPaint = new Paint();

        {
            // setup innPointPaint to draw a white circle a little smaller than the 
            // typical circle drawn for each vertex:
            innerPointPaint.setAntiAlias(true);
            innerPointPaint.setStrokeCap(Paint.Cap.ROUND);
            innerPointPaint.setColor(Color.WHITE);
            innerPointPaint.setStrokeWidth(PixelUtils.dpToPix(7));
        }

        public MyLineAndPointFormatter(Context context, int xmlCfgId) {
            super(context, xmlCfgId);
        }

        @Override
        public Class<? extends SeriesRenderer> getRendererClass() {
            return MyLineAndPointRenderer.class;
        }

        @Override
        public SeriesRenderer getRendererInstance(XYPlot plot) {
            return new MyLineAndPointRenderer(plot);
        }

        public Paint getInnerPointPaint() {
            return innerPointPaint;
        }
    }

然后,在将系列添加到绘图时使用上面的而不是LineAndPointFormatter:

MyLineAndPointFormatter series1Format =
                new MyLineAndPointFormatter(this, R.xml.my_format);

虽然最后一种方法是更多代码,但它也是最有效和可扩展的。