1 /* 2 * Copyright (C) 2016 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.animation; 18 19 import com.android.layoutlib.bridge.impl.DelegateManager; 20 import com.android.tools.layoutlib.annotations.LayoutlibDelegate; 21 22 import android.graphics.Path; 23 import android.util.MathUtils; 24 import android.view.animation.AccelerateDecelerateInterpolator; 25 import android.view.animation.AccelerateInterpolator; 26 import android.view.animation.AnticipateInterpolator; 27 import android.view.animation.AnticipateOvershootInterpolator; 28 import android.view.animation.BaseInterpolator; 29 import android.view.animation.BounceInterpolator; 30 import android.view.animation.CycleInterpolator; 31 import android.view.animation.DecelerateInterpolator; 32 import android.view.animation.Interpolator; 33 import android.view.animation.LinearInterpolator; 34 import android.view.animation.OvershootInterpolator; 35 import android.view.animation.PathInterpolator; 36 37 /** 38 * Delegate used to provide new implementation of a select few methods of {@link 39 * NativeInterpolatorFactory} 40 * <p> 41 * Through the layoutlib_create tool, the original methods of NativeInterpolatorFactory have 42 * been replaced by calls to methods of the same name in this delegate class. 43 */ 44 @SuppressWarnings("unused") 45 public class NativeInterpolatorFactory_Delegate { 46 private static final DelegateManager<Interpolator> sManager = new DelegateManager<> 47 (Interpolator.class); 48 getDelegate(long nativePtr)49 public static Interpolator getDelegate(long nativePtr) { 50 return sManager.getDelegate(nativePtr); 51 } 52 53 @LayoutlibDelegate createAccelerateDecelerateInterpolator()54 /*package*/ static long createAccelerateDecelerateInterpolator() { 55 return sManager.addNewDelegate(new AccelerateDecelerateInterpolator()); 56 } 57 58 @LayoutlibDelegate createAccelerateInterpolator(float factor)59 /*package*/ static long createAccelerateInterpolator(float factor) { 60 return sManager.addNewDelegate(new AccelerateInterpolator(factor)); 61 } 62 63 @LayoutlibDelegate createAnticipateInterpolator(float tension)64 /*package*/ static long createAnticipateInterpolator(float tension) { 65 return sManager.addNewDelegate(new AnticipateInterpolator(tension)); 66 } 67 68 @LayoutlibDelegate createAnticipateOvershootInterpolator(float tension)69 /*package*/ static long createAnticipateOvershootInterpolator(float tension) { 70 return sManager.addNewDelegate(new AnticipateOvershootInterpolator(tension)); 71 } 72 73 @LayoutlibDelegate createBounceInterpolator()74 /*package*/ static long createBounceInterpolator() { 75 return sManager.addNewDelegate(new BounceInterpolator()); 76 } 77 78 @LayoutlibDelegate createCycleInterpolator(float cycles)79 /*package*/ static long createCycleInterpolator(float cycles) { 80 return sManager.addNewDelegate(new CycleInterpolator(cycles)); 81 } 82 83 @LayoutlibDelegate createDecelerateInterpolator(float factor)84 /*package*/ static long createDecelerateInterpolator(float factor) { 85 return sManager.addNewDelegate(new DecelerateInterpolator(factor)); 86 } 87 88 @LayoutlibDelegate createLinearInterpolator()89 /*package*/ static long createLinearInterpolator() { 90 return sManager.addNewDelegate(new LinearInterpolator()); 91 } 92 93 @LayoutlibDelegate createOvershootInterpolator(float tension)94 /*package*/ static long createOvershootInterpolator(float tension) { 95 return sManager.addNewDelegate(new OvershootInterpolator(tension)); 96 } 97 98 @LayoutlibDelegate createPathInterpolator(float[] x, float[] y)99 /*package*/ static long createPathInterpolator(float[] x, float[] y) { 100 Path path = new Path(); 101 path.moveTo(x[0], y[0]); 102 for (int i = 1; i < x.length; i++) { 103 path.lineTo(x[i], y[i]); 104 } 105 return sManager.addNewDelegate(new PathInterpolator(path)); 106 } 107 108 private static class LutInterpolator extends BaseInterpolator { 109 private final float[] mValues; 110 private final int mSize; 111 LutInterpolator(float[] values)112 private LutInterpolator(float[] values) { 113 mValues = values; 114 mSize = mValues.length; 115 } 116 117 @Override getInterpolation(float input)118 public float getInterpolation(float input) { 119 float lutpos = input * (mSize - 1); 120 if (lutpos >= (mSize - 1)) { 121 return mValues[mSize - 1]; 122 } 123 124 int ipart = (int) lutpos; 125 float weight = lutpos - ipart; 126 127 int i1 = ipart; 128 int i2 = Math.min(i1 + 1, mSize - 1); 129 130 assert i1 >= 0 && i2 >= 0 : "Negatives in the interpolation"; 131 132 return MathUtils.lerp(mValues[i1], mValues[i2], weight); 133 } 134 } 135 136 @LayoutlibDelegate createLutInterpolator(float[] values)137 /*package*/ static long createLutInterpolator(float[] values) { 138 return sManager.addNewDelegate(new LutInterpolator(values)); 139 } 140 } 141