Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <a href="http://developer.android.com/reference/android/graphics/Path.html#cubicTo%28float,%20float,%20float,%20float,%20float,%20float%29" rel="nofollow noreferrer">Path.cubicTo()</a> method. It draws a line using cubic spline algorithm which results in the smoothing effect you want.</p> <p>Checkout the answer to a <a href="https://stackoverflow.com/questions/8287949/android-how-to-draw-a-smooth-line-following-your-finger">similar question here</a>, where a guy is talking about cubic splines. There is a short algorithm showing how to calculate input parameters for <code>Path.cubicTo()</code> method. You can play with divider values to achieve required smoothness. For example, in the picture below I divided by 5 instead of 3. Hope this helps. </p> <p><img src="https://i.stack.imgur.com/JZ94U.png" alt="Example of a polylyne drawn using Path.cubicTo() method"></p> <p>I have spent some time and implemented a <code>SplineLineAndPointFormatter</code> class, which does the stuff you need in androidplot library. It uses same technics. Here is how androidplot example applications looks like. You just need to use it instead of <code>LineAndPointFormatter</code>.</p> <p><img src="https://i.stack.imgur.com/oNvG3.png" alt="Androidplot example with SplineLineAndPointFormatter"></p> <p>Here is code example and the class I wrote.</p> <pre><code>f1 = new SplineLineAndPointFormatter(color.getColor(), null, Color.argb(60, color.getRed(), color.getGreen(), color.getBlue()), null); plot.addSeries(series1, f1); </code></pre> <p>Here is the class doing the magic. It is based on version 0.6.1 of <a href="http://bitbucket.org/androidplot/androidplot" rel="nofollow noreferrer">androidplot</a> library.</p> <pre><code>package com.androidplot.xy; import android.graphics.Canvas; import android.graphics.Path; import android.graphics.PointF; import android.graphics.RectF; import com.androidplot.ui.SeriesRenderer; import com.androidplot.util.ValPixConverter; public class SplineLineAndPointFormatter extends LineAndPointFormatter { public SplineLineAndPointFormatter() { } public SplineLineAndPointFormatter(Integer lineColor, Integer vertexColor, Integer fillColor) { super(lineColor, vertexColor, fillColor, null); } public SplineLineAndPointFormatter(Integer lineColor, Integer vertexColor, Integer fillColor, FillDirection fillDir) { super(lineColor, vertexColor, fillColor, null, fillDir); } @Override public Class&lt;? extends SeriesRenderer&gt; getRendererClass() { return SplineLineAndPointRenderer.class; } @Override public SeriesRenderer getRendererInstance(XYPlot plot) { return new SplineLineAndPointRenderer(plot); } public static class SplineLineAndPointRenderer extends LineAndPointRenderer&lt;BezierLineAndPointFormatter&gt; { static class Point { public float x, y, dx, dy; public Point(PointF pf) { x = pf.x; y = pf.y; } } private Point prev, point, next; private int pointsCounter; public SplineLineAndPointRenderer(XYPlot plot) { super(plot); } @Override protected void appendToPath(Path path, final PointF thisPoint, PointF lastPoint) { pointsCounter--; if (point == null) { point = new Point(thisPoint); point.dx = ((point.x - prev.x) / 5); point.dy = ((point.y - prev.y) / 5); return; } else if (next == null) { next = new Point(thisPoint); } else { prev = point; point = next; next = new Point(thisPoint); } point.dx = ((next.x - prev.x) / 5); point.dy = ((next.y - prev.y) / 5); path.cubicTo(prev.x + prev.dx, prev.y + prev.dy, point.x - point.dx, point.y - point.dy, point.x, point.y); if (pointsCounter == 1) { // last point next.dx = ((next.x - point.x) / 5); next.dy = ((next.y - point.y) / 5); path.cubicTo(point.x + point.dx, point.y + point.dy, next.x - next.dx, next.y - next.dy, next.x, next.y); } } @Override protected void drawSeries(Canvas canvas, RectF plotArea, XYSeries series, LineAndPointFormatter formatter) { Number y = series.getY(0); Number x = series.getX(0); if (x == null || y == null) throw new IllegalArgumentException("no null values in xyseries permitted"); XYPlot p = getPlot(); PointF thisPoint = ValPixConverter.valToPix(x, y, plotArea, p.getCalculatedMinX(), p.getCalculatedMaxX(), p.getCalculatedMinY(), p.getCalculatedMaxY()); prev = new Point(thisPoint); point = next = null; pointsCounter = series.size(); super.drawSeries(canvas, plotArea, series, formatter); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload