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