ShapeDrawable(from a PathShape) not drawing on correct coordinates

2.5k views Asked by At

I'm trying to create a ShapeDrawable that draws the following path:

Path path = new Path();
path.moveTo(50, 20);
path.lineTo(0, 50);
path.lineTo(50, 100);

ShapeDrawable shapeDrawable = new ShapeDrawable(new PathShape(path, someNumber ,someNumber ));

Then I put shapeDrawable as the top layer of a Layer drawable like so:

Drawable layers[] = new Drawable[2];
layers[0] = res.getDrawable(R.drawable.crawford01);
layers[1] =  shapeDrawable;

LayerDrawable layerDrawable = new LayerDrawable(layers);
view.setImageDrawable(layerDrawable);

Now the problem is that the path doesn't start at (50,20) and it jumps around in ways I don't understand when you change somenumber where shapeDrawable is constructed.

Any help or documentation that you can suggest is appreciated.

1

There are 1 answers

0
happydude On BEST ANSWER

The "someNumber" attributes are actually very important when defining your PathShape, and are not trivial. They are the "standard" width and height of the path, essentially defining the path's bounds and relating directly to the coordinates you define your path at as specified in the PathShape constructor here.

Another important point is that the coordinates you use to define your Path are not absolute coordinates as far as a PathShape is concerned, but instead is combined with the standard width and height to calculate how your shape appears when it is scaled. For example, the following two PathShapes are essentially identical.

public Path getPath1 {
    Path path = new Path();
    path.lineTo(0, 1);
    path.lineTo(1, 0);
    path.close();
    return path;
}

public Path getPath2 {
    Path path = new Path();
    path.lineTo(0, 10);
    path.lineTo(5, 0);
    path.close();
    return path;
}

PathShape shape1 = new PathShape(getPath1(), 1, 1);
PathShape shape2 = new PathShape(getPath2(), 5, 10);