ShapeDrawable(from a PathShape) not drawing on cor

2019-06-03 11:19发布

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条回答
成全新的幸福
2楼-- · 2019-06-03 12:01

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);
查看更多
登录 后发表回答