Search code examples
androidandroidplot

Androidplot - when does the gridRect is measured?


I'm using androidplot to display a fixed-sized graph, which I've setted up a BitmapShader to match each Range interval and I need to set this shader to be always displayed on the graph, since it's beginning.

My problem is:

I can't intialize the graph with this shader (I've tested it using as base the DemoApp and the shader is working properly). Every time I try to get the GridRect using the method getGridRect() it returns null, no matter where I call this function during the activity creation. I can only set the shadder after the activity is created, but only with a button or something like it.

I searched through the entire source code but could not find where this measure occurs during the activity's lifecycle.


Solution

  • Unfortunately, I was unable the proper way of getting the gridrect mesured during it's creatiion, but I've found a way around.

    I was able to define the graph height by getting the XYPlot view-height and subtract the graph top and bottom padding.

    A snippet from my code:

      @Override
      protected void onCreate(Bundle savedInstanceState) {
         ...
        mPlot.getGraphWidget().setPaddingBottom(20);
        mPlot.getGraphWidget().setPaddingTop(20);
        //Added a ViewTreeObserver to be sure when the Plot was measured.
        mPlot.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
            @Override
            public void onGlobalLayout() {
                mPlot.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                //Avoid the factory trying to scale bitmap according to Display density
                Options options = new BitmapFactory.Options();
                options.inScaled = false;
                Bitmap bm = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.chartbackground, options), 1, (int) mPlot.getHeight() - 40, false);
                BitmapShader backgroundShader = new BitmapShader(bm, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
                Matrix shaderMatrix = new Matrix();
                //This matrix references always stayed the same even if the plot changed its size.
                shaderMatrix.setTranslate(49.6875f, 30.75f);
                backgroundShader.setLocalMatrix(shaderMatrix);
                mPlot.getGraphWidget().getGridBackgroundPaint().setShader(backgroundShader);
                //Setting the background to be a little bit translucid.
                mPlot.getGraphWidget().getGridBackgroundPaint().setAlpha(90);
            }
    
        });
        ...
    
      }