PathInterpolatorCompat.java revision 3ac77bf186f87ecad4bf0063b2f6c4384efbd56a
1/*
2 * Copyright (C) 2015 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.support.v4.view.animation;
18
19import android.graphics.Path;
20import android.os.Build;
21import android.view.animation.Interpolator;
22
23/**
24 * Helper for creating path-based {@link Interpolator} instances. On API 21 or newer, the
25 * platform implementation will be used and on older platforms a compatible alternative
26 * implementation will be used.
27 */
28public final class PathInterpolatorCompat {
29
30    private PathInterpolatorCompat() {
31        // prevent instantiation
32    }
33
34    /**
35     * Create an {@link Interpolator} for an arbitrary {@link Path}. The {@link Path}
36     * must begin at {@code (0, 0)} and end at {@code (1, 1)}. The x-coordinate along the
37     * {@link Path} is the input value and the output is the y coordinate of the line at that
38     * point. This means that the Path must conform to a function {@code y = f(x)}.
39     * <p/>
40     * The {@link Path} must not have gaps in the x direction and must not
41     * loop back on itself such that there can be two points sharing the same x coordinate.
42     *
43     * @param path the {@link Path} to use to make the line representing the {@link Interpolator}
44     * @return the {@link Interpolator} representing the {@link Path}
45     */
46    public static Interpolator create(Path path) {
47        if (Build.VERSION.SDK_INT >= 21) {
48            return PathInterpolatorCompatApi21.create(path);
49        }
50        return PathInterpolatorCompatBase.create(path);
51    }
52
53    /**
54     * Create an {@link Interpolator} for a quadratic Bezier curve. The end points
55     * {@code (0, 0)} and {@code (1, 1)} are assumed.
56     *
57     * @param controlX the x coordinate of the quadratic Bezier control point
58     * @param controlY the y coordinate of the quadratic Bezier control point
59     * @return the {@link Interpolator} representing the quadratic Bezier curve
60     */
61    public static Interpolator create(float controlX, float controlY) {
62        if (Build.VERSION.SDK_INT >= 21) {
63            return PathInterpolatorCompatApi21.create(controlX, controlY);
64        }
65        return PathInterpolatorCompatBase.create(controlX, controlY);
66    }
67
68    /**
69     * Create an {@link Interpolator} for a cubic Bezier curve.  The end points
70     * {@code (0, 0)} and {@code (1, 1)} are assumed.
71     *
72     * @param controlX1 the x coordinate of the first control point of the cubic Bezier
73     * @param controlY1 the y coordinate of the first control point of the cubic Bezier
74     * @param controlX2 the x coordinate of the second control point of the cubic Bezier
75     * @param controlY2 the y coordinate of the second control point of the cubic Bezier
76     * @return the {@link Interpolator} representing the cubic Bezier curve
77     */
78    public static Interpolator create(float controlX1, float controlY1,
79            float controlX2, float controlY2) {
80        if (Build.VERSION.SDK_INT >= 21) {
81            return PathInterpolatorCompatApi21.create(controlX1, controlY1, controlX2, controlY2);
82        }
83        return PathInterpolatorCompatBase.create(controlX1, controlY1, controlX2, controlY2);
84    }
85}
86