1 /*
2  * Copyright (C) 2007 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 android.graphics.drawable.shapes;
18 
19 import android.annotation.NonNull;
20 import android.graphics.Canvas;
21 import android.graphics.Outline;
22 import android.graphics.Paint;
23 
24 import java.util.Objects;
25 
26 /**
27  * Defines a generic graphical "shape."
28  * <p>
29  * Any Shape can be drawn to a Canvas with its own draw() method, but more
30  * graphical control is available if you instead pass it to a
31  * {@link android.graphics.drawable.ShapeDrawable}.
32  * <p>
33  * Custom Shape classes must implement {@link #clone()} and return an instance
34  * of the custom Shape class.
35  */
36 public abstract class Shape implements Cloneable {
37     private float mWidth;
38     private float mHeight;
39 
40     /**
41      * Returns the width of the Shape.
42      */
getWidth()43     public final float getWidth() {
44         return mWidth;
45     }
46 
47     /**
48      * Returns the height of the Shape.
49      */
getHeight()50     public final float getHeight() {
51         return mHeight;
52     }
53 
54     /**
55      * Draws this shape into the provided Canvas, with the provided Paint.
56      * <p>
57      * Before calling this, you must call {@link #resize(float,float)}.
58      *
59      * @param canvas the Canvas within which this shape should be drawn
60      * @param paint  the Paint object that defines this shape's characteristics
61      */
draw(Canvas canvas, Paint paint)62     public abstract void draw(Canvas canvas, Paint paint);
63 
64     /**
65      * Resizes the dimensions of this shape.
66      * <p>
67      * Must be called before {@link #draw(Canvas,Paint)}.
68      *
69      * @param width the width of the shape (in pixels)
70      * @param height the height of the shape (in pixels)
71      */
resize(float width, float height)72     public final void resize(float width, float height) {
73         if (width < 0) {
74             width = 0;
75         }
76         if (height < 0) {
77             height =0;
78         }
79         if (mWidth != width || mHeight != height) {
80             mWidth = width;
81             mHeight = height;
82             onResize(width, height);
83         }
84     }
85 
86     /**
87      * Checks whether the Shape is opaque.
88      * <p>
89      * Default impl returns {@code true}. Override if your subclass can be
90      * opaque.
91      *
92      * @return true if any part of the drawable is <em>not</em> opaque.
93      */
hasAlpha()94     public boolean hasAlpha() {
95         return true;
96     }
97 
98     /**
99      * Callback method called when {@link #resize(float,float)} is executed.
100      *
101      * @param width the new width of the Shape
102      * @param height the new height of the Shape
103      */
onResize(float width, float height)104     protected void onResize(float width, float height) {}
105 
106     /**
107      * Computes the Outline of the shape and return it in the supplied Outline
108      * parameter. The default implementation does nothing and {@code outline}
109      * is not changed.
110      *
111      * @param outline the Outline to be populated with the result. Must be
112      *                non-{@code null}.
113      */
getOutline(@onNull Outline outline)114     public void getOutline(@NonNull Outline outline) {}
115 
116     @Override
clone()117     public Shape clone() throws CloneNotSupportedException {
118         return (Shape) super.clone();
119     }
120 
121     @Override
equals(Object o)122     public boolean equals(Object o) {
123         if (this == o) {
124             return true;
125         }
126         if (o == null || getClass() != o.getClass()) {
127             return false;
128         }
129         Shape shape = (Shape) o;
130         return Float.compare(shape.mWidth, mWidth) == 0
131             && Float.compare(shape.mHeight, mHeight) == 0;
132     }
133 
134     @Override
hashCode()135     public int hashCode() {
136         return Objects.hash(mWidth, mHeight);
137     }
138 }
139