Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 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 | |
| 17 | package android.animation; |
| 18 | |
| 19 | /** |
| 20 | * This evaluator can be used to perform type interpolation between integer |
| 21 | * values that represent ARGB colors. |
| 22 | */ |
Chet Haase | 53ee331 | 2011-01-10 15:56:56 -0800 | [diff] [blame] | 23 | public class ArgbEvaluator implements TypeEvaluator { |
George Mount | 1ffb280 | 2013-10-09 16:13:54 -0700 | [diff] [blame] | 24 | private static final ArgbEvaluator sInstance = new ArgbEvaluator(); |
| 25 | |
| 26 | /** |
| 27 | * Returns an instance of <code>ArgbEvaluator</code> that may be used in |
| 28 | * {@link ValueAnimator#setEvaluator(TypeEvaluator)}. The same instance may |
| 29 | * be used in multiple <code>Animator</code>s because it holds no state. |
| 30 | * @return An instance of <code>ArgbEvalutor</code>. |
| 31 | * |
| 32 | * @hide |
| 33 | */ |
| 34 | public static ArgbEvaluator getInstance() { |
| 35 | return sInstance; |
| 36 | } |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * This function returns the calculated in-between value for a color |
| 40 | * given integers that represent the start and end values in the four |
| 41 | * bytes of the 32-bit int. Each channel is separately linearly interpolated |
| 42 | * and the resulting calculated values are recombined into the return value. |
| 43 | * |
| 44 | * @param fraction The fraction from the starting to the ending values |
| 45 | * @param startValue A 32-bit int value representing colors in the |
| 46 | * separate bytes of the parameter |
| 47 | * @param endValue A 32-bit int value representing colors in the |
| 48 | * separate bytes of the parameter |
| 49 | * @return A value that is calculated to be the linearly interpolated |
| 50 | * result, derived by separating the start and end values into separate |
| 51 | * color channels and interpolating each one separately, recombining the |
| 52 | * resulting values in the same way. |
| 53 | */ |
| 54 | public Object evaluate(float fraction, Object startValue, Object endValue) { |
| 55 | int startInt = (Integer) startValue; |
Chet Haase | 9b55998 | 2012-08-09 14:16:29 -0700 | [diff] [blame] | 56 | int startA = (startInt >> 24) & 0xff; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 57 | int startR = (startInt >> 16) & 0xff; |
| 58 | int startG = (startInt >> 8) & 0xff; |
| 59 | int startB = startInt & 0xff; |
| 60 | |
| 61 | int endInt = (Integer) endValue; |
Chet Haase | 9b55998 | 2012-08-09 14:16:29 -0700 | [diff] [blame] | 62 | int endA = (endInt >> 24) & 0xff; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 63 | int endR = (endInt >> 16) & 0xff; |
| 64 | int endG = (endInt >> 8) & 0xff; |
| 65 | int endB = endInt & 0xff; |
| 66 | |
| 67 | return (int)((startA + (int)(fraction * (endA - startA))) << 24) | |
| 68 | (int)((startR + (int)(fraction * (endR - startR))) << 16) | |
| 69 | (int)((startG + (int)(fraction * (endG - startG))) << 8) | |
| 70 | (int)((startB + (int)(fraction * (endB - startB)))); |
| 71 | } |
| 72 | } |