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