The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package android.view.animation; |
| 18 | |
| 19 | import android.content.Context; |
ztenghui | e5e9260 | 2014-06-03 14:02:10 -0700 | [diff] [blame] | 20 | import android.content.res.Resources; |
ztenghui | e5e9260 | 2014-06-03 14:02:10 -0700 | [diff] [blame] | 21 | import android.content.res.Resources.Theme; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 22 | import android.content.res.TypedArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.util.AttributeSet; |
| 24 | |
ztenghui | e5e9260 | 2014-06-03 14:02:10 -0700 | [diff] [blame] | 25 | import com.android.internal.R; |
John Reck | c8ac775 | 2014-05-12 16:39:41 -0700 | [diff] [blame] | 26 | import com.android.internal.view.animation.HasNativeInterpolator; |
| 27 | import com.android.internal.view.animation.NativeInterpolatorFactory; |
| 28 | import com.android.internal.view.animation.NativeInterpolatorFactoryHelper; |
| 29 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | /** |
ztenghui | e5e9260 | 2014-06-03 14:02:10 -0700 | [diff] [blame] | 31 | * An interpolator where the rate of change starts out quickly and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | * and then decelerates. |
| 33 | * |
| 34 | */ |
John Reck | c8ac775 | 2014-05-12 16:39:41 -0700 | [diff] [blame] | 35 | @HasNativeInterpolator |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 36 | public class DecelerateInterpolator extends BaseInterpolator implements NativeInterpolatorFactory { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | public DecelerateInterpolator() { |
| 38 | } |
Gilles Debunne | 5296424 | 2010-02-24 11:05:19 -0800 | [diff] [blame] | 39 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Constructor |
ztenghui | e5e9260 | 2014-06-03 14:02:10 -0700 | [diff] [blame] | 42 | * |
Gilles Debunne | 5296424 | 2010-02-24 11:05:19 -0800 | [diff] [blame] | 43 | * @param factor Degree to which the animation should be eased. Setting factor to 1.0f produces |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | * an upside-down y=x^2 parabola. Increasing factor above 1.0f makes exaggerates the |
| 45 | * ease-out effect (i.e., it starts even faster and ends evens slower) |
| 46 | */ |
| 47 | public DecelerateInterpolator(float factor) { |
| 48 | mFactor = factor; |
| 49 | } |
ztenghui | e5e9260 | 2014-06-03 14:02:10 -0700 | [diff] [blame] | 50 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | public DecelerateInterpolator(Context context, AttributeSet attrs) { |
ztenghui | e5e9260 | 2014-06-03 14:02:10 -0700 | [diff] [blame] | 52 | this(context.getResources(), context.getTheme(), attrs); |
| 53 | } |
| 54 | |
| 55 | /** @hide */ |
| 56 | public DecelerateInterpolator(Resources res, Theme theme, AttributeSet attrs) { |
| 57 | TypedArray a; |
| 58 | if (theme != null) { |
| 59 | a = theme.obtainStyledAttributes(attrs, R.styleable.DecelerateInterpolator, 0, 0); |
| 60 | } else { |
| 61 | a = res.obtainAttributes(attrs, R.styleable.DecelerateInterpolator); |
| 62 | } |
| 63 | |
| 64 | mFactor = a.getFloat(R.styleable.DecelerateInterpolator_factor, 1.0f); |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 65 | setChangingConfiguration(a.getChangingConfigurations()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | a.recycle(); |
| 67 | } |
ztenghui | e5e9260 | 2014-06-03 14:02:10 -0700 | [diff] [blame] | 68 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | public float getInterpolation(float input) { |
Dianne Hackborn | 6908cd1 | 2010-11-08 15:11:16 -0800 | [diff] [blame] | 70 | float result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | if (mFactor == 1.0f) { |
Dianne Hackborn | 6908cd1 | 2010-11-08 15:11:16 -0800 | [diff] [blame] | 72 | result = (float)(1.0f - (1.0f - input) * (1.0f - input)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | } else { |
Dianne Hackborn | 6908cd1 | 2010-11-08 15:11:16 -0800 | [diff] [blame] | 74 | result = (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | } |
Dianne Hackborn | 6908cd1 | 2010-11-08 15:11:16 -0800 | [diff] [blame] | 76 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | } |
ztenghui | e5e9260 | 2014-06-03 14:02:10 -0700 | [diff] [blame] | 78 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | private float mFactor = 1.0f; |
John Reck | c8ac775 | 2014-05-12 16:39:41 -0700 | [diff] [blame] | 80 | |
| 81 | /** @hide */ |
| 82 | @Override |
| 83 | public long createNativeInterpolator() { |
| 84 | return NativeInterpolatorFactoryHelper.createDecelerateInterpolator(mFactor); |
| 85 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | } |