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