1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15package com.android.egg.octo; 16 17import android.graphics.Canvas; 18import android.graphics.Paint; 19import android.graphics.Path; 20import android.graphics.PathMeasure; 21import android.os.Debug; 22 23import java.util.Arrays; 24 25public class TaperedPathStroke { 26 static float sMinStepPx = 4f; 27 static PathMeasure pm = new PathMeasure(); 28 static float[] pos = {0,0}; 29 static float[] tan = {0,0}; 30 static float lerp(float t, float a, float b) { 31 return a + t*(b-a); 32 } 33 public static void setMinStep(float px) { 34 sMinStepPx = px; 35 } 36 37 // it's the variable-width brush algorithm from the Markers app, basically 38 public static void drawPath(Canvas c, Path p, float r1, float r2, Paint pt) { 39 pm.setPath(p,false); 40 final float len = pm.getLength(); 41 float t=0; 42 boolean last=false; 43 while (true) { 44 if (t>=len) { 45 t=len; 46 last=true; 47 } 48 pm.getPosTan(t, pos, tan); 49 float r = len > 0 ? lerp(t/len, r1, r2) : r1; 50 c.drawCircle(pos[0], pos[1], r, pt); 51 t += Math.max(r*0.25f, sMinStepPx); // walk forward 1/4 radius, not too small though 52 if (last) break; 53 } 54 } 55} 56