blob: 2d1249df4fb51692bd4a675b470e5ca4d6f55f3f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.view.animation;
18
19import android.content.Context;
ztenghuie5e92602014-06-03 14:02:10 -070020import android.content.res.Resources;
ztenghuie5e92602014-06-03 14:02:10 -070021import android.content.res.Resources.Theme;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070022import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.util.AttributeSet;
24
ztenghuie5e92602014-06-03 14:02:10 -070025import com.android.internal.R;
John Reckc8ac7752014-05-12 16:39:41 -070026import com.android.internal.view.animation.HasNativeInterpolator;
27import com.android.internal.view.animation.NativeInterpolatorFactory;
28import com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
29
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030/**
ztenghuie5e92602014-06-03 14:02:10 -070031 * An interpolator where the rate of change starts out quickly and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032 * and then decelerates.
33 *
34 */
John Reckc8ac7752014-05-12 16:39:41 -070035@HasNativeInterpolator
Yigit Boyard422dc32014-09-25 12:23:35 -070036public class DecelerateInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 public DecelerateInterpolator() {
38 }
Gilles Debunne52964242010-02-24 11:05:19 -080039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 /**
41 * Constructor
ztenghuie5e92602014-06-03 14:02:10 -070042 *
Gilles Debunne52964242010-02-24 11:05:19 -080043 * @param factor Degree to which the animation should be eased. Setting factor to 1.0f produces
koprivaf07a4602018-09-20 11:20:27 -070044 * an upside-down y=x^2 parabola. Increasing factor above 1.0f exaggerates the
45 * ease-out effect (i.e., it starts even faster and ends evens slower).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 */
47 public DecelerateInterpolator(float factor) {
48 mFactor = factor;
49 }
ztenghuie5e92602014-06-03 14:02:10 -070050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 public DecelerateInterpolator(Context context, AttributeSet attrs) {
ztenghuie5e92602014-06-03 14:02:10 -070052 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 Boyard422dc32014-09-25 12:23:35 -070065 setChangingConfiguration(a.getChangingConfigurations());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 a.recycle();
67 }
ztenghuie5e92602014-06-03 14:02:10 -070068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 public float getInterpolation(float input) {
Dianne Hackborn6908cd12010-11-08 15:11:16 -080070 float result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 if (mFactor == 1.0f) {
Dianne Hackborn6908cd12010-11-08 15:11:16 -080072 result = (float)(1.0f - (1.0f - input) * (1.0f - input));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 } else {
Dianne Hackborn6908cd12010-11-08 15:11:16 -080074 result = (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 }
Dianne Hackborn6908cd12010-11-08 15:11:16 -080076 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 }
ztenghuie5e92602014-06-03 14:02:10 -070078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 private float mFactor = 1.0f;
John Reckc8ac7752014-05-12 16:39:41 -070080
81 /** @hide */
82 @Override
83 public long createNativeInterpolator() {
84 return NativeInterpolatorFactoryHelper.createDecelerateInterpolator(mFactor);
85 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086}