Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2016 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.launcher3.graphics;
     18 
     19 import android.graphics.Outline;
     20 import android.graphics.Path;
     21 import android.graphics.drawable.shapes.PathShape;
     22 import android.support.annotation.NonNull;
     23 
     24 /**
     25  * Wrapper around {@link android.graphics.drawable.shapes.PathShape}
     26  * that creates a shape with a triangular path (pointing up or down).
     27  */
     28 public class TriangleShape extends PathShape {
     29     private Path mTriangularPath;
     30 
     31     public TriangleShape(Path path, float stdWidth, float stdHeight) {
     32         super(path, stdWidth, stdHeight);
     33         mTriangularPath = path;
     34     }
     35 
     36     public static TriangleShape create(float width, float height, boolean isPointingUp) {
     37         Path triangularPath = new Path();
     38         if (isPointingUp) {
     39             triangularPath.moveTo(0, height);
     40             triangularPath.lineTo(width, height);
     41             triangularPath.lineTo(width / 2, 0);
     42             triangularPath.close();
     43         } else {
     44             triangularPath.moveTo(0, 0);
     45             triangularPath.lineTo(width / 2, height);
     46             triangularPath.lineTo(width, 0);
     47             triangularPath.close();
     48         }
     49         return new TriangleShape(triangularPath, width, height);
     50     }
     51 
     52     @Override
     53     public void getOutline(@NonNull Outline outline) {
     54         outline.setConvexPath(mTriangularPath);
     55     }
     56 }
     57