117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase/*
217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Copyright (C) 2010 The Android Open Source Project
317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Licensed under the Apache License, Version 2.0 (the "License");
517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * you may not use this file except in compliance with the License.
617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * You may obtain a copy of the License at
717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *      http://www.apache.org/licenses/LICENSE-2.0
917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
1017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Unless required by applicable law or agreed to in writing, software
1117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * distributed under the License is distributed on an "AS IS" BASIS,
1217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * See the License for the specific language governing permissions and
1417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * limitations under the License.
1517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */
1617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
1717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haasepackage android.animation;
1817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
1917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase/**
2017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This evaluator can be used to perform type interpolation between integer
2117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * values that represent ARGB colors.
2217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */
2353ee3316bcb3590ff156b3fd7108903c0817c35dChet Haasepublic class ArgbEvaluator implements TypeEvaluator {
241ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    private static final ArgbEvaluator sInstance = new ArgbEvaluator();
251ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount
261ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    /**
271ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * Returns an instance of <code>ArgbEvaluator</code> that may be used in
281ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * {@link ValueAnimator#setEvaluator(TypeEvaluator)}. The same instance may
291ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * be used in multiple <code>Animator</code>s because it holds no state.
301ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * @return An instance of <code>ArgbEvalutor</code>.
311ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     *
321ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * @hide
331ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     */
341ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    public static ArgbEvaluator getInstance() {
351ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        return sInstance;
361ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    }
3717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
3817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
3917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This function returns the calculated in-between value for a color
4017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * given integers that represent the start and end values in the four
4117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * bytes of the 32-bit int. Each channel is separately linearly interpolated
4217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * and the resulting calculated values are recombined into the return value.
4317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
4417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param fraction The fraction from the starting to the ending values
4517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param startValue A 32-bit int value representing colors in the
4617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * separate bytes of the parameter
4717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param endValue A 32-bit int value representing colors in the
4817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * separate bytes of the parameter
4917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return A value that is calculated to be the linearly interpolated
5017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * result, derived by separating the start and end values into separate
5117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * color channels and interpolating each one separately, recombining the
5217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * resulting values in the same way.
5317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
5417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public Object evaluate(float fraction, Object startValue, Object endValue) {
5517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        int startInt = (Integer) startValue;
569b5599894b80b2707909e8e6872eeec7c58af73aChet Haase        int startA = (startInt >> 24) & 0xff;
5717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        int startR = (startInt >> 16) & 0xff;
5817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        int startG = (startInt >> 8) & 0xff;
5917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        int startB = startInt & 0xff;
6017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
6117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        int endInt = (Integer) endValue;
629b5599894b80b2707909e8e6872eeec7c58af73aChet Haase        int endA = (endInt >> 24) & 0xff;
6317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        int endR = (endInt >> 16) & 0xff;
6417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        int endG = (endInt >> 8) & 0xff;
6517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        int endB = endInt & 0xff;
6617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
6717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return (int)((startA + (int)(fraction * (endA - startA))) << 24) |
6817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                (int)((startR + (int)(fraction * (endR - startR))) << 16) |
6917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                (int)((startG + (int)(fraction * (endG - startG))) << 8) |
7017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                (int)((startB + (int)(fraction * (endB - startB))));
7117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
7217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase}