Android Simple Cartesian Plotter
Introduction
This library is a simple interface to facilitate the way to graph points in a Cartesian plane, additionally you can create simple polygons, it was done with the native app of Canvas. Develop this small library because after searching several libraries in different sites I did not find any that would help me with my problem, I hope it will save you programming time :).
Install
You can download library files from JCenter or GitHub.
Add the following in your app's build.gradle file:
repositories {
jcenter()
}
dependencies {
implementation 'com.ederdoski.plotter:simplecartesianplotter:1.0'
}
Usage
- In XML
- Create a surfaceView object, this will be your canvas.
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
- In Java (Required)
- Implement in your main class SurfaceHolder.Callback
implements SurfaceHolder.Callback
- Generate the methods surfaceCreated, surfaceChanged, surfaceDestroyed.
@Override
public void surfaceCreated(SurfaceHolder holder) {
CartesianPlotter.setStyle(Paint.Style.STROKE);
CartesianPlotter.getPaint().setStrokeWidth(10);
CartesianPlotter.setSurfaceHolder(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
CartesianPlotter.setSurfaceHolder(holder);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}
- Instantiate the class CartesianPlotter
new CartesianPlotter(this, surfaceView.getHolder());
- Instantiate the following callback
CartesianPlotter.getSurfaceHolder().addCallback(this);
- Draw in Plane
- Start editing the pixels in the surface.
CartesianPlotter.getSurfaceHolder().addCallback(this);
- Once you verify that your canvas is not null, invoke the refresh method to initialize the canvas.
CartesianPlotter.refresh(lyCanvas, aPoints, drawPoligon);
- Then indicate to the drawPoint () method, the coordinates of the point to be drawn, remember to multiply your coordinate by the desired scale (zoom).
float valueX = cx * CartesianPlotter.getScale();
float valueY = cy * CartesianPlotter.getScale();
CartesianPlotter.drawPoint(lyCanvas, valueX, valueY, radius);
- Finally, release your canvas and publish your update
CartesianPlotter.getSurfaceHolder().unlockCanvasAndPost(CartesianPlotter.getCanvas());
You should have a piece of code like this:
if (CartesianPlotter.getCanvas() != null) {
CartesianPlotter.refresh(lyCanvas, aPoints, drawPoligon);
float valueX = cx * CartesianPlotter.getScale();
float valueY = cy * CartesianPlotter.getScale();
CartesianPlotter.drawPoint(lyCanvas, valueX, valueY, radius);
CartesianPlotter.getSurfaceHolder().unlockCanvasAndPost(CartesianPlotter.getCanvas());
}
Methods
This method is responsible for drawing the plane, and previously saved points in an ArrayList , you can also activate or deactivate if you want to draw a polygon with the saved points.
- This method receives three parameters:
- A LinearLayout container of the SurfaceView.
- An ArrayList in which coordinates can be saved.
- A boolean variable that indicates whether or not you want to draw the polygon.
CartesianPlotter.refresh();
This method is responsible for placing a point in the plane.
- This method receives four parameters:
- A LinearLayout container of the SurfaceView.
- A value to be plotted indicating the X coordinate.
- A value to be plotted indicating the Y coordinate.
- A value that indicates the radius of the point to be plotted.
CartesianPlotter.drawPoint();
- Set the size of the scale on the canvas (zoom)
CartesianPlotter.setScale()
- get the size of the scale on the canvas (zoom)
CartesianPlotter.getScale()
- Set background color of canvas
CartesianPlotter.setBackgroundColor()
- Set color of lines in plane
CartesianPlotter.setColorPlane()
- Set the color of the current position
CartesianPlotter.setPointColor()
- Set color of position saved
CartesianPlotter.setPointSaveColor()
- Set the color of the polygon lines
CartesianPlotter.setFenceColor()
Currently the methods that involve setColor, receive an int parameter of type color in encoded format, if you want to modify the color of some element I advise you to use the native interface of Colors of Android, example:
CartesianPlotter.setPointColor (Color.BLACK);
for more details read: Color | Android Developers
License
This code is open-sourced software licensed under the MIT license.