1 /* 2 * Copyright (C) 2020 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 17 package com.android.wm.shell.common; 18 19 import android.graphics.Outline; 20 import android.graphics.Path; 21 import android.graphics.drawable.shapes.PathShape; 22 23 import androidx.annotation.NonNull; 24 25 /** 26 * Wrapper around {@link PathShape} 27 * that creates a shape with a triangular path (pointing up or down). 28 * 29 * This is the copy from SystemUI/recents. 30 */ 31 public class TriangleShape extends PathShape { 32 private Path mTriangularPath; 33 TriangleShape(Path path, float stdWidth, float stdHeight)34 public TriangleShape(Path path, float stdWidth, float stdHeight) { 35 super(path, stdWidth, stdHeight); 36 mTriangularPath = path; 37 } 38 create(float width, float height, boolean isPointingUp)39 public static TriangleShape create(float width, float height, boolean isPointingUp) { 40 Path triangularPath = new Path(); 41 if (isPointingUp) { 42 triangularPath.moveTo(0, height); 43 triangularPath.lineTo(width, height); 44 triangularPath.lineTo(width / 2, 0); 45 triangularPath.close(); 46 } else { 47 triangularPath.moveTo(0, 0); 48 triangularPath.lineTo(width / 2, height); 49 triangularPath.lineTo(width, 0); 50 triangularPath.close(); 51 } 52 return new TriangleShape(triangularPath, width, height); 53 } 54 55 /** Create an arrow TriangleShape that points to the left or the right */ createHorizontal( float width, float height, boolean isPointingLeft)56 public static TriangleShape createHorizontal( 57 float width, float height, boolean isPointingLeft) { 58 Path triangularPath = new Path(); 59 if (isPointingLeft) { 60 triangularPath.moveTo(0, height / 2); 61 triangularPath.lineTo(width, height); 62 triangularPath.lineTo(width, 0); 63 triangularPath.close(); 64 } else { 65 triangularPath.moveTo(0, height); 66 triangularPath.lineTo(width, height / 2); 67 triangularPath.lineTo(0, 0); 68 triangularPath.close(); 69 } 70 return new TriangleShape(triangularPath, width, height); 71 } 72 73 @Override getOutline(@onNull Outline outline)74 public void getOutline(@NonNull Outline outline) { 75 outline.setPath(mTriangularPath); 76 } 77 } 78