ShapeDrawable(from a PathShape) not drawing on correct coordinates

喜你入骨 提交于 2019-12-08 05:11:34

问题


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:


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


来源:https://stackoverflow.com/questions/9133051/shapedrawablefrom-a-pathshape-not-drawing-on-correct-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!