1c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount/*
2c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount * Copyright (C) 2013 The Android Open Source Project
3c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount *
4c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount * Licensed under the Apache License, Version 2.0 (the "License");
5c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount * you may not use this file except in compliance with the License.
6c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount * You may obtain a copy of the License at
7c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount *
8c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount *      http://www.apache.org/licenses/LICENSE-2.0
9c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount *
10c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount * Unless required by applicable law or agreed to in writing, software
11c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount * distributed under the License is distributed on an "AS IS" BASIS,
12c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount * See the License for the specific language governing permissions and
14c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount * limitations under the License.
15c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount */
16c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mountpackage android.animation;
17c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount
18c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mountimport android.graphics.PointF;
19c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount
20c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount/**
21c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount * This evaluator can be used to perform type interpolation between <code>PointF</code> values.
22c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount */
23c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mountpublic class PointFEvaluator implements TypeEvaluator<PointF> {
24c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount
25c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount    /**
26c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * When null, a new PointF is returned on every evaluate call. When non-null,
27c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * mPoint will be modified and returned on every evaluate.
28c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     */
29c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount    private PointF mPoint;
30c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount
31c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount    /**
32c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * Construct a PointFEvaluator that returns a new PointF on every evaluate call.
33c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * To avoid creating an object for each evaluate call,
34c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * {@link PointFEvaluator#PointFEvaluator(android.graphics.PointF)} should be used
35c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * whenever possible.
36c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     */
37c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount    public PointFEvaluator() {
38c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount    }
39c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount
40c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount    /**
41c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * Constructs a PointFEvaluator that modifies and returns <code>reuse</code>
42c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * in {@link #evaluate(float, android.graphics.PointF, android.graphics.PointF)} calls.
43c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * The value returned from
44c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * {@link #evaluate(float, android.graphics.PointF, android.graphics.PointF)} should
45c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * not be cached because it will change over time as the object is reused on each
46c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * call.
47c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     *
48c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * @param reuse A PointF to be modified and returned by evaluate.
49c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     */
50c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount    public PointFEvaluator(PointF reuse) {
51c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount        mPoint = reuse;
52c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount    }
53c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount
54c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount    /**
55c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * This function returns the result of linearly interpolating the start and
56c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * end PointF values, with <code>fraction</code> representing the proportion
57c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * between the start and end values. The calculation is a simple parametric
58c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * calculation on each of the separate components in the PointF objects
59c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * (x, y).
60c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     *
61c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * <p>If {@link #PointFEvaluator(android.graphics.PointF)} was used to construct
62c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * this PointFEvaluator, the object returned will be the <code>reuse</code>
63c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * passed into the constructor.</p>
64c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     *
65c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * @param fraction   The fraction from the starting to the ending values
66c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * @param startValue The start PointF
67c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * @param endValue   The end PointF
68c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     * @return A linear interpolation between the start and end values, given the
69c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     *         <code>fraction</code> parameter.
70c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount     */
71c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount    @Override
72c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount    public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
73c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount        float x = startValue.x + (fraction * (endValue.x - startValue.x));
74c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount        float y = startValue.y + (fraction * (endValue.y - startValue.y));
75c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount
76c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount        if (mPoint != null) {
77c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount            mPoint.set(x, y);
78c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount            return mPoint;
79c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount        } else {
80c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount            return new PointF(x, y);
81c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount        }
82c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount    }
83c96c7b2e54965e30c8fb82295f1ca9f891ebd5e7George Mount}
84