1/* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package androidx.core.graphics; 18 19import android.graphics.Path; 20import android.graphics.PointF; 21 22import androidx.annotation.FloatRange; 23import androidx.annotation.NonNull; 24import androidx.annotation.RequiresApi; 25 26import java.util.ArrayList; 27import java.util.Collection; 28import java.util.List; 29 30/** A set of path-related utility methods. */ 31public final class PathUtils { 32 /** 33 * Flattens (or approximate) a {@link Path} with a series of line segments using a 0.5 pixel 34 * error. 35 * 36 * <em>Note:</em> This method requires API 26 or newer. 37 * 38 * @see #flatten(Path, float) 39 */ 40 @RequiresApi(26) 41 @NonNull 42 public static Collection<PathSegment> flatten(@NonNull Path path) { 43 return flatten(path, 0.5f); 44 } 45 46 /** 47 * Flattens (or approximate) a {@link Path} with a series of line segments. 48 * 49 * <em>Note:</em> This method requires API 26 or newer. 50 * 51 * @param error The acceptable error for a line on the Path. Typically this would be 52 * 0.5 so that the error is less than half a pixel. 53 * 54 * @see Path#approximate 55 */ 56 @RequiresApi(26) 57 @NonNull 58 public static Collection<PathSegment> flatten(@NonNull final Path path, 59 @FloatRange(from = 0) final float error) { 60 float[] pathData = path.approximate(error); 61 int pointCount = pathData.length / 3; 62 List<PathSegment> segments = new ArrayList<>(pointCount); 63 for (int i = 1; i < pointCount; i++) { 64 int index = i * 3; 65 int prevIndex = (i - 1) * 3; 66 67 float d = pathData[index]; 68 float x = pathData[index + 1]; 69 float y = pathData[index + 2]; 70 71 float pd = pathData[prevIndex]; 72 float px = pathData[prevIndex + 1]; 73 float py = pathData[prevIndex + 2]; 74 75 if (d != pd && (x != px || y != py)) { 76 segments.add(new PathSegment(new PointF(px, py), pd, new PointF(x, y), d)); 77 } 78 } 79 return segments; 80 } 81 82 private PathUtils() { 83 } 84} 85