Search code examples
javaandroidgeometryandroidplot

Androidplot draw circle with given point and radius


I am using androidplot with PanZoom attached:

PanZoom.attach(plot);

Therefore I can zoom in and out as it should.

What I want next is to draw a circle at a given point. Right now I use the strokewidth to set the dimension of the circle. But when I zoom in and out, the circle size remains the same, although it should scale according to the zoom level. So imagine I zoom in infinitely, the circle should at a certain amount of zoom level cover the whole screen.

But it does not. How can I achieve this? I was thinking aboud increasing the strokewidth of the circle according to the zoomlevel but I was neither able to get the zoomlevel nor to get the domain levels on the left and right side of the plot.

EDIT: In xml folder I create a file e.g. circle.xml:

<?xml version="1.0" encoding="utf-8"?>
<config
fillPaint.color="#00000000"
linePaint.color="#00000000"
linePaint.strokeWidth="0dp"
pointLabelFormatter.textPaint.color="#FFFFFF"
vertexPaint.color="#371cd1d4"
vertexPaint.strokeWidth="20dp"/>

and in java

    sigmaLabelFormatter = new LineAndPointFormatter();
    sigmaLabelFormatter.setPointLabelFormatter(new PointLabelFormatter());
    sigmaLabelFormatter.configure(activity.getApplicationContext(), R.xml.circle);
    sigmaLabelFormatter.setPointLabelFormatter(null);

Solution

  • I found a solution to my problem, since I finally managed:

    to get the domain levels on the left and right side of the plot.

    or to be more precise to get the width of the plot.

    Since the dimensions of the plot is in meters, I do the calculation as follows:

    private float calculateDP(float px){        
      return px / (densityDpi / densityDefault);
    }
    
    private float pixelsPerMeter(float value){
        float w = plot.getGraph().getWidgetDimensions().canvasRect.width();
        float w2 = plot.getBounds().getWidth().floatValue();
        return value * (w / w2);
    }
    
    private void init(Activity activity){
        densityDpi = activity.getResources().getDisplayMetrics().densityDpi;
        densityDefault = android.util.DisplayMetrics.DENSITY_DEFAULT;
    }
    
    private void onCreate(){
    // ... lots of stuff
    labelFormatter.getVertexPaint().setStrokeWidth(calculateDP(pixelsPerMeter(2)));
    }
    

    Might it help somebody out there...