Shape.java revision 31ba192dd201df2cad96a8c503f730130ab0d80f
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
17package android.graphics.drawable.shapes;
18
19import android.graphics.Canvas;
20import android.graphics.Outline;
21import android.graphics.Paint;
22
23/**
24 * Defines a generic graphical "shape."
25 * Any Shape can be drawn to a Canvas with its own draw() method,
26 * but more graphical control is available if you instead pass
27 * it to a {@link android.graphics.drawable.ShapeDrawable}.
28 */
29public abstract class Shape implements Cloneable {
30    private float mWidth;
31    private float mHeight;
32
33    /**
34     * Returns the width of the Shape.
35     */
36    public final float getWidth() {
37        return mWidth;
38    }
39
40    /**
41     * Returns the height of the Shape.
42     */
43    public final float getHeight() {
44        return mHeight;
45    }
46
47    /**
48     * Draw this shape into the provided Canvas, with the provided Paint.
49     * Before calling this, you must call {@link #resize(float,float)}.
50     *
51     * @param canvas the Canvas within which this shape should be drawn
52     * @param paint  the Paint object that defines this shape's characteristics
53     */
54    public abstract void draw(Canvas canvas, Paint paint);
55
56    /**
57     * Resizes the dimensions of this shape.
58     * Must be called before {@link #draw(Canvas,Paint)}.
59     *
60     * @param width the width of the shape (in pixels)
61     * @param height the height of the shape (in pixels)
62     */
63    public final void resize(float width, float height) {
64        if (width < 0) {
65            width = 0;
66        }
67        if (height < 0) {
68            height =0;
69        }
70        if (mWidth != width || mHeight != height) {
71            mWidth = width;
72            mHeight = height;
73            onResize(width, height);
74        }
75    }
76
77    /**
78     * Checks whether the Shape is opaque.
79     * Default impl returns true. Override if your subclass can be opaque.
80     *
81     * @return true if any part of the drawable is <em>not</em> opaque.
82     */
83    public boolean hasAlpha() {
84        return true;
85    }
86
87    /**
88     * Callback method called when {@link #resize(float,float)} is executed.
89     *
90     * @param width the new width of the Shape
91     * @param height the new height of the Shape
92     */
93    protected void onResize(float width, float height) {}
94
95    /**
96     * Compute the Outline of the shape.
97     *
98     * The default implementation does not supply an outline.
99     */
100    public void getOutline(Outline outline) {}
101
102    @Override
103    public Shape clone() throws CloneNotSupportedException {
104        return (Shape) super.clone();
105    }
106
107}
108