3

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

Mirakels
  • 33
  • 3

1 Answers1

0

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
Nick
  • 8,181
  • 4
  • 38
  • 63
  • Hi Nick.Yes, that did the trick. Thanks! I played with margins and padding and looked in the androidplot sources but could not manage to fix it. this setTranslate() did the trick. Isn't this something you could add to the library itself, as the need for such code in a user app isn't obvious... – Mirakels Jul 15 '14 at 17:46
  • I agree that this particular detail is tragically light on documentation in the Android API but once you know about it, it's easy to work with and provides more flexibility than a utility built into Androidplot would. I'll take it under consideration though :) – Nick Jul 15 '14 at 18:11
  • Alternatively a small howto on the DOCs pages of the androidplot website:) – Mirakels Jul 15 '14 at 18:38