ArgbEvaluator.java revision 53ee3316bcb3590ff156b3fd7108903c0817c35d
1/*
2 * Copyright (C) 2010 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.animation;
18
19/**
20 * This evaluator can be used to perform type interpolation between integer
21 * values that represent ARGB colors.
22 */
23public class ArgbEvaluator implements TypeEvaluator {
24
25    /**
26     * This function returns the calculated in-between value for a color
27     * given integers that represent the start and end values in the four
28     * bytes of the 32-bit int. Each channel is separately linearly interpolated
29     * and the resulting calculated values are recombined into the return value.
30     *
31     * @param fraction The fraction from the starting to the ending values
32     * @param startValue A 32-bit int value representing colors in the
33     * separate bytes of the parameter
34     * @param endValue A 32-bit int value representing colors in the
35     * separate bytes of the parameter
36     * @return A value that is calculated to be the linearly interpolated
37     * result, derived by separating the start and end values into separate
38     * color channels and interpolating each one separately, recombining the
39     * resulting values in the same way.
40     */
41    public Object evaluate(float fraction, Object startValue, Object endValue) {
42        int startInt = (Integer) startValue;
43        int startA = (startInt >> 24);
44        int startR = (startInt >> 16) & 0xff;
45        int startG = (startInt >> 8) & 0xff;
46        int startB = startInt & 0xff;
47
48        int endInt = (Integer) endValue;
49        int endA = (endInt >> 24);
50        int endR = (endInt >> 16) & 0xff;
51        int endG = (endInt >> 8) & 0xff;
52        int endB = endInt & 0xff;
53
54        return (int)((startA + (int)(fraction * (endA - startA))) << 24) |
55                (int)((startR + (int)(fraction * (endR - startR))) << 16) |
56                (int)((startG + (int)(fraction * (endG - startG))) << 8) |
57                (int)((startB + (int)(fraction * (endB - startB))));
58    }
59}