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.annotation.NonNull; 20import android.graphics.Canvas; 21import android.graphics.Outline; 22import android.graphics.Paint; 23 24/** 25 * Defines a generic graphical "shape." 26 * Any Shape can be drawn to a Canvas with its own draw() method, 27 * but more graphical control is available if you instead pass 28 * it to a {@link android.graphics.drawable.ShapeDrawable}. 29 */ 30public abstract class Shape implements Cloneable { 31 private float mWidth; 32 private float mHeight; 33 34 /** 35 * Returns the width of the Shape. 36 */ 37 public final float getWidth() { 38 return mWidth; 39 } 40 41 /** 42 * Returns the height of the Shape. 43 */ 44 public final float getHeight() { 45 return mHeight; 46 } 47 48 /** 49 * Draw this shape into the provided Canvas, with the provided Paint. 50 * Before calling this, you must call {@link #resize(float,float)}. 51 * 52 * @param canvas the Canvas within which this shape should be drawn 53 * @param paint the Paint object that defines this shape's characteristics 54 */ 55 public abstract void draw(Canvas canvas, Paint paint); 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 */ 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 */ 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 */ 94 protected void onResize(float width, float height) {} 95 96 /** 97 * Compute the Outline of the shape and return it in the supplied Outline 98 * parameter. The default implementation does nothing and {@code outline} is not changed. 99 * 100 * @param outline The Outline to be populated with the result. Should not be null. 101 */ 102 public void getOutline(@NonNull Outline outline) {} 103 104 @Override 105 public Shape clone() throws CloneNotSupportedException { 106 return (Shape) super.clone(); 107 } 108 109} 110