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.Paint; 21 22 /** 23 * Defines a generic graphical "shape." 24 * Any Shape can be drawn to a Canvas with its own draw() method, 25 * but more graphical control is available if you instead pass 26 * it to a {@link android.graphics.drawable.ShapeDrawable}. 27 */ 28 public abstract class Shape implements Cloneable { 29 private float mWidth; 30 private float mHeight; 31 32 /** 33 * Returns the width of the Shape. 34 */ getWidth()35 public final float getWidth() { 36 return mWidth; 37 } 38 39 /** 40 * Returns the height of the Shape. 41 */ getHeight()42 public final float getHeight() { 43 return mHeight; 44 } 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 */ draw(Canvas canvas, Paint paint)54 public abstract void draw(Canvas canvas, Paint paint); 55 56 57 /** 58 * Resizes the dimensions of this shape. 59 * Must be called before {@link #draw(Canvas,Paint)}. 60 * 61 * @param width the width of the shape (in pixels) 62 * @param height the height of the shape (in pixels) 63 */ resize(float width, float height)64 public final void resize(float width, float height) { 65 if (width < 0) { 66 width = 0; 67 } 68 if (height < 0) { 69 height =0; 70 } 71 if (mWidth != width || mHeight != height) { 72 mWidth = width; 73 mHeight = height; 74 onResize(width, height); 75 } 76 } 77 78 /** 79 * Checks whether the Shape is opaque. 80 * Default impl returns true. Override if your subclass can be opaque. 81 * 82 * @return true if any part of the drawable is <em>not</em> opaque. 83 */ hasAlpha()84 public boolean hasAlpha() { 85 return true; 86 } 87 88 /** 89 * Callback method called when {@link #resize(float,float)} is executed. 90 * 91 * @param width the new width of the Shape 92 * @param height the new height of the Shape 93 */ onResize(float width, float height)94 protected void onResize(float width, float height) {} 95 96 @Override clone()97 public Shape clone() throws CloneNotSupportedException { 98 return (Shape) super.clone(); 99 } 100 } 101