1376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen/*
2376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen * Copyright (C) 2015 The Android Open Source Project
3376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen *
4376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen * Licensed under the Apache License, Version 2.0 (the "License");
5376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen * you may not use this file except in compliance with the License.
6376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen * You may obtain a copy of the License at
7376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen *
8376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen *   http://www.apache.org/licenses/LICENSE-2.0
9376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen *
10376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen * Unless required by applicable law or agreed to in writing, software
11376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen * distributed under the License is distributed on an "AS IS" BASIS,
12376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen * See the License for the specific language governing permissions and
14376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen * limitations under the License.
15376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen */
16376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen
17376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassenpackage android.support.v4.view.animation;
18376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen
19376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassenimport android.graphics.Path;
20376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassenimport android.os.Build;
21376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassenimport android.view.animation.Interpolator;
22150417cd66a89ffb27fd7edb4b7ff3e66dd377ccAurimas Liutikasimport android.view.animation.PathInterpolator;
23376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen
24376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen/**
25376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen * Helper for creating path-based {@link Interpolator} instances. On API 21 or newer, the
26376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen * platform implementation will be used and on older platforms a compatible alternative
27376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen * implementation will be used.
28376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen */
2917f0ca51e84eb8d9f70a64e97de1151739a04406Chris Banespublic final class PathInterpolatorCompat {
30376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen
31376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen    private PathInterpolatorCompat() {
32376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen        // prevent instantiation
33376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen    }
34376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen
35376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen    /**
36376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * Create an {@link Interpolator} for an arbitrary {@link Path}. The {@link Path}
37376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * must begin at {@code (0, 0)} and end at {@code (1, 1)}. The x-coordinate along the
38376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * {@link Path} is the input value and the output is the y coordinate of the line at that
39376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * point. This means that the Path must conform to a function {@code y = f(x)}.
40376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * <p/>
41376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * The {@link Path} must not have gaps in the x direction and must not
42376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * loop back on itself such that there can be two points sharing the same x coordinate.
43376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     *
44376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * @param path the {@link Path} to use to make the line representing the {@link Interpolator}
45376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * @return the {@link Interpolator} representing the {@link Path}
46376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     */
47376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen    public static Interpolator create(Path path) {
48376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen        if (Build.VERSION.SDK_INT >= 21) {
49150417cd66a89ffb27fd7edb4b7ff3e66dd377ccAurimas Liutikas            return new PathInterpolator(path);
50376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen        }
51150417cd66a89ffb27fd7edb4b7ff3e66dd377ccAurimas Liutikas        return new PathInterpolatorApi14(path);
52376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen    }
53376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen
54376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen    /**
55376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * Create an {@link Interpolator} for a quadratic Bezier curve. The end points
56376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * {@code (0, 0)} and {@code (1, 1)} are assumed.
57376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     *
58376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * @param controlX the x coordinate of the quadratic Bezier control point
59376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * @param controlY the y coordinate of the quadratic Bezier control point
60376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * @return the {@link Interpolator} representing the quadratic Bezier curve
61376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     */
62376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen    public static Interpolator create(float controlX, float controlY) {
63376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen        if (Build.VERSION.SDK_INT >= 21) {
64150417cd66a89ffb27fd7edb4b7ff3e66dd377ccAurimas Liutikas            return new PathInterpolator(controlX, controlY);
65376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen        }
66150417cd66a89ffb27fd7edb4b7ff3e66dd377ccAurimas Liutikas        return new PathInterpolatorApi14(controlX, controlY);
67376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen    }
68376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen
69376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen    /**
70376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * Create an {@link Interpolator} for a cubic Bezier curve.  The end points
71376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * {@code (0, 0)} and {@code (1, 1)} are assumed.
72376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     *
73376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * @param controlX1 the x coordinate of the first control point of the cubic Bezier
74376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * @param controlY1 the y coordinate of the first control point of the cubic Bezier
75376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * @param controlX2 the x coordinate of the second control point of the cubic Bezier
76376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * @param controlY2 the y coordinate of the second control point of the cubic Bezier
77376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     * @return the {@link Interpolator} representing the cubic Bezier curve
78376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen     */
79376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen    public static Interpolator create(float controlX1, float controlY1,
80376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen            float controlX2, float controlY2) {
81376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen        if (Build.VERSION.SDK_INT >= 21) {
82150417cd66a89ffb27fd7edb4b7ff3e66dd377ccAurimas Liutikas            return new PathInterpolator(controlX1, controlY1, controlX2, controlY2);
83376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen        }
84150417cd66a89ffb27fd7edb4b7ff3e66dd377ccAurimas Liutikas        return new PathInterpolatorApi14(controlX1, controlY1, controlX2, controlY2);
85376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen    }
86376f90c159c74a267a5b5e13a5d71273980a72dfJustin Klaassen}
87