ShapeDrawable (from PathShape) not using correct coordinates

I am 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 placed shapeDrawable as the top layer of the layer as shown below:

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 in a way I don't understand when you change somenumber

where the shapeDrawable construct is built.

Any help or documentation you can offer is appreciated.

+1


source to share


1 answer


The "someNumber" attributes are actually very important in defining yours PathShape

and are not trivial. They represent the "standard" path width and height, essentially defining the boundaries of the path and referring directly to the coordinates that you define for your path, as specified in the constructor PathShape

.

Another important point is that the coordinates you use to define Path

are not absolute coordinates in relation PathShape

, but are instead combined with the standard width and height to calculate how your shape looks when it is scaled. For example, the following two PathShape

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);

      

+4


source







All Articles