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
17package android.view.animation;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.content.res.Resources.Theme;
22import android.content.res.TypedArray;
23import android.util.AttributeSet;
24
25import com.android.internal.R;
26import com.android.internal.view.animation.HasNativeInterpolator;
27import com.android.internal.view.animation.NativeInterpolatorFactory;
28import com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
29
30/**
31 * Repeats the animation for a specified number of cycles. The
32 * rate of change follows a sinusoidal pattern.
33 *
34 */
35@HasNativeInterpolator
36public class CycleInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
37    public CycleInterpolator(float cycles) {
38        mCycles = cycles;
39    }
40
41    public CycleInterpolator(Context context, AttributeSet attrs) {
42        this(context.getResources(), context.getTheme(), attrs);
43    }
44
45    /** @hide */
46    public CycleInterpolator(Resources resources, Theme theme, AttributeSet attrs) {
47        TypedArray a;
48        if (theme != null) {
49            a = theme.obtainStyledAttributes(attrs, R.styleable.CycleInterpolator, 0, 0);
50        } else {
51            a = resources.obtainAttributes(attrs, R.styleable.CycleInterpolator);
52        }
53
54        mCycles = a.getFloat(R.styleable.CycleInterpolator_cycles, 1.0f);
55        setChangingConfiguration(a.getChangingConfigurations());
56        a.recycle();
57    }
58
59    public float getInterpolation(float input) {
60        return (float)(Math.sin(2 * mCycles * Math.PI * input));
61    }
62
63    private float mCycles;
64
65    /** @hide */
66    @Override
67    public long createNativeInterpolator() {
68        return NativeInterpolatorFactoryHelper.createCycleInterpolator(mCycles);
69    }
70}
71