开发者

Is there a way to iterate a Path object on Android?

开发者 https://www.devze.com 2023-03-19 03:43 出处:网络
Given a android.graphics.Path, I would like to be able to retrieve the specific path commands: the quadTo(..), curveTo(...), lineTo(...), etc., that the Path is composed of.

Given a android.graphics.Path, I would like to be able to retrieve the specific path commands: the quadTo(..), curveTo(...), lineTo(...), etc., that the Path is composed of.

Java provides a PathIterator to accomplish this f开发者_如何学Cor GeneralPath objects, but I haven't found any equivalent for Android. Am I missing something?


I checked the actual implementation and it doesn't look like you can get the information. Every call just goes down to native code, and the actual path is stored on the native side.

I also tried looking at Canvas's drawPath method, but it just drops to native code as well.

I'm not sure if it's possible in your code, but your best option looks like sub-classing Path and overriding all the adXXX methods to store the values in a list.


Old question but I wanted it to have the answer You cannot retrieve the commands but you can get points back. Look at android.graphics.PathMeasure API 1

  float[] tmpPos = new float[2];
  float[] tmpTan = new float[2];
  PathMeasure measure = new PathMeasure();
  measure.setPath(path, true);
  do {
   float dist = measure.getLength();
   for (float p = 0; p < dist; p += 1) {
     measure.getPosTan(p, tmpPos, tmpTan);
     float x = tmpPos[0], y = tmpPos[1];
     float nx = tmpTan[0], ny = tmpTan[1];
     // do your own stuff
    }
 } while (measure.nextContour());

PathMeasure also has getSegment It may be use to extract the segment but I have never tried that.

0

精彩评论

暂无评论...
验证码 换一张
取 消