Search code examples
androidplot

androidplot background image shift


I'm trying to separate the background of the graph grid in 3 areas using this code:

int[] data = {0xff000000, 0x80008000, 0xff000000};
bgBitmap = Bitmap.createBitmap(data, 1, 3, Bitmap.Config.ARGB_8888);
RectF rect = plot.getGraphWidget().getGridRect();
BitmapShader myShader = new BitmapShader(
                    Bitmap.createScaledBitmap(bgBitmap, 1, (int) rect.height(), false),
                    Shader.TileMode.REPEAT,
                    Shader.TileMode.REPEAT);
plot.getGraphWidget().getGridBackgroundPaint().setShader(myShader);

So scaling a 3 pixel bitmap to the graph height and repeating it over the whole domain area. However the resulting graph show that the background seems to be shifted up a bit. It looks like the shift size is about equal to the domain label height.

How can I fix this?

Hm cannot post picture because of 'reputation' sigh. Link to the example graph: http://marcel.mesa.nl/androidplot.png


Solution

  • I think you're running into the issue mentioned near the end of this thread. Essentially, the origin of the shader is the top-left corner of the screen, not the top-left corner of component for which the background is being drawn using the shader. The solution is to translate to the top-left point of the graphWidget like this:

    RectF rect = plot.getGraphWidget().getGridRect();
    Matrix m = new Matrix();
    m.setTranslate(rect.left, rect.top);
    shader.setLocalMatrix(m); // where shader is your shader instance