10

I would like to display X/Y data on a canvas using lines with a certain width (in pixels, or "dp" ideally). I have tried the setStrokeWidth(..) method of Paint, and that indeed does change the line width but it isn't what I need. In my case I've scaled my canvas to "real Engineering units" using matrix.preScale(xScale, yScale) so the X scale represents 0 to 100 and the Y is 0 to 1. The setStrokeWidth() method of the Paint object seems to set the stroke so that it follows my matrix preScale() settings. In other words, horizontal lines are drawn really thin, and vertical lines are drawn really thick.

Is there a way to configure the Paint so that no matter which direction the line is drawn, its width is a consistent number of pixels?

I have tried defining a Drawable that is a line, and making a ShapeDrawable from that, and then applying it to the Paint but ran into some nasty class casting errors at run time. This made me think that this was the wrong way to go about it. But maybe I gave up too soon.

I understand that are a number of Android plotting/charting packages available, some with source, but I'm really looking to understand the platform here, rather than using a third-party solution.

Thanks for any hints! Rich

Rich
  • 117
  • 1
  • 1
  • 10
  • Nicely-worded question. I don't know, but good luck. – Jim Blackler Apr 15 '11 at 23:04
  • 1
    I think you should not use the matrix for your unit adaption. Every drawing operation usually uses the same pipeline that finally ends up with a shape which is then transformed and rasterized. So a modified matrix influences the final result of any drawing operation thus distorting the line stroke if different axis units are used. So for plotting the correct way would be to scale your coordinates like you want before passing it to the android paint methods. Other wise you would be unable to draw correct lines and text. – dronus Apr 15 '11 at 23:09
  • @dronus OK, sound advice. Thank you for that. I suspected I would need to avoid the matrix preScale, but was hoping that there might be a way to take advantage of that very useful scaling. – Rich Apr 15 '11 at 23:37
  • @Jim Thanks, I tried to read the "asking good questions help" and it may have worked here. @dronus OK, sound advice. Thank you for that. I suspected I would need to avoid the matrix preScale, but was hoping that there might be a way to take advantage of that very useful scaling. I'm hoping that Romain might offer some hidden tips ... we'll see. – Rich Apr 15 '11 at 23:38
  • Sorry about the duplicate posts guys, FireFox 3 doesn't seem to play well with the stackoverflow forms. – Rich Apr 15 '11 at 23:43
  • In addition to dronus i would advise you to look at getProjection() from the MapView component. It basically transforms a set of screen coordinates to the projection you want(in this case map coordinates or lat/lon points). A similar method would work meaning you don't have to mess with the drawing matrix. – JQCorreia Apr 16 '11 at 03:14
  • I think the final answer is, there is no way. dronus' idea to do your own scaling (bypassing the matrix scaling) worked for me. Thanks JQCorreia for the Projection suggestion, that idea hides what might otherwise be ugly scaling into a nice interface. – Rich Apr 18 '11 at 16:54

2 Answers2

11

For example on Android : paint.setStrokeWidth(3); This method sets line width. In this case, line width is 3 pixels. I am searching just like this method on BlackBerry.

Try this :

    Graphics.setColor(Color.RED);
    Graphics.setStrokeWidth(20);
    Graphics.drawRect(0, 0, 100, 100);

They are available in net.rim.device.api.ui.Graphics package.

0

I had to do something similar when drawing overlays onto a map.

As the user can use multi-touch to scale the map dynamically, when I enter the draw routine I calculate a scaling factor for the x and y axes, apply it to the canvas, draw the map, then invert this scaling factor and apply it to the line widths for the overlays. Thus the canvas is scaled and the overlays are scaled, then anti-scaled, so they are in effect fixed width.

I found this works extremely well with no real performance hit.

ScouseChris
  • 4,377
  • 32
  • 38