Home | History | Annotate | Download | only in shapes
      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 import android.graphics.Path;
     22 
     23 /**
     24  * Creates geometric paths, utilizing the {@link android.graphics.Path} class.
     25  * The path can be drawn to a Canvas with its own draw() method,
     26  * but more graphical control is available if you instead pass
     27  * the PathShape to a {@link android.graphics.drawable.ShapeDrawable}.
     28  */
     29 public class PathShape extends Shape {
     30     private Path    mPath;
     31     private float   mStdWidth;
     32     private float   mStdHeight;
     33 
     34     private float   mScaleX;    // cached from onResize
     35     private float   mScaleY;    // cached from onResize
     36 
     37     /**
     38      * PathShape constructor.
     39      *
     40      * @param path       a Path that defines the geometric paths for this shape
     41      * @param stdWidth   the standard width for the shape. Any changes to the
     42      *                   width with resize() will result in a width scaled based
     43      *                   on the new width divided by this width.
     44      * @param stdHeight  the standard height for the shape. Any changes to the
     45      *                   height with resize() will result in a height scaled based
     46      *                   on the new height divided by this height.
     47      */
     48     public PathShape(Path path, float stdWidth, float stdHeight) {
     49         mPath = path;
     50         mStdWidth = stdWidth;
     51         mStdHeight = stdHeight;
     52     }
     53 
     54     @Override
     55     public void draw(Canvas canvas, Paint paint) {
     56         canvas.save();
     57         canvas.scale(mScaleX, mScaleY);
     58         canvas.drawPath(mPath, paint);
     59         canvas.restore();
     60     }
     61 
     62     @Override
     63     protected void onResize(float width, float height) {
     64         mScaleX = width / mStdWidth;
     65         mScaleY = height / mStdHeight;
     66     }
     67 
     68     @Override
     69     public PathShape clone() throws CloneNotSupportedException {
     70         PathShape shape = (PathShape) super.clone();
     71         shape.mPath = new Path(mPath);
     72         return shape;
     73     }
     74 }
     75 
     76