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.graphics.Canvas;
20 import android.graphics.Outline;
21 import android.graphics.Paint;
22 import android.graphics.RectF;
23 
24 import java.util.Objects;
25 
26 /**
27  * Defines a rectangle shape.
28  * <p>
29  * The rectangle can be drawn to a Canvas with its own draw() method,
30  * but more graphical control is available if you instead pass
31  * the RectShape to a {@link android.graphics.drawable.ShapeDrawable}.
32  */
33 public class RectShape extends Shape {
34     private RectF mRect = new RectF();
35 
RectShape()36     public RectShape() {}
37 
38     @Override
draw(Canvas canvas, Paint paint)39     public void draw(Canvas canvas, Paint paint) {
40         canvas.drawRect(mRect, paint);
41     }
42 
43     @Override
getOutline(Outline outline)44     public void getOutline(Outline outline) {
45         final RectF rect = rect();
46         outline.setRect((int) Math.ceil(rect.left), (int) Math.ceil(rect.top),
47                 (int) Math.floor(rect.right), (int) Math.floor(rect.bottom));
48     }
49 
50     @Override
onResize(float width, float height)51     protected void onResize(float width, float height) {
52         mRect.set(0, 0, width, height);
53     }
54 
55     /**
56      * Returns the RectF that defines this rectangle's bounds.
57      */
rect()58     protected final RectF rect() {
59         return mRect;
60     }
61 
62     @Override
clone()63     public RectShape clone() throws CloneNotSupportedException {
64         final RectShape shape = (RectShape) super.clone();
65         shape.mRect = new RectF(mRect);
66         return shape;
67     }
68 
69     @Override
equals(Object o)70     public boolean equals(Object o) {
71         if (this == o) {
72             return true;
73         }
74         if (o == null || getClass() != o.getClass()) {
75             return false;
76         }
77         if (!super.equals(o)) {
78             return false;
79         }
80         RectShape rectShape = (RectShape) o;
81         return Objects.equals(mRect, rectShape.mRect);
82     }
83 
84     @Override
hashCode()85     public int hashCode() {
86         return Objects.hash(super.hashCode(), mRect);
87     }
88 }
89