/** * Copyright 2013 Alex Curran * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.espian.snippets; import android.animation.ObjectAnimator; import android.animation.TypeEvaluator; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.view.View; /** * Class which allows animation between two colors. Compatible with NineOldAndroids or the standard * Android animation APIs (make sure you change the imports when using NOA). *
* When using with background colors for views, ensure you use the {@link ViewBackgroundWrapper} class. * {@link View} does not have agetBackgroundColor() method required by the {@link ObjectAnimator}
* framework; the wrapper class solves this issue.
*/
public class ColorAnimator {
public static ObjectAnimator ofColor(Object target, String propertyName, int from, int to) {
return ObjectAnimator.ofObject(target, propertyName, new ColorEvaluator(), from, to);
}
public static ObjectAnimator ofColor(Object target, String propertyName, int to) {
return ObjectAnimator.ofObject(target, propertyName, new ColorEvaluator(), to);
}
public static ObjectAnimator ofBackgroundColor(View target, int from, int to) {
return ObjectAnimator.ofObject(new ViewBackgroundWrapper(target), "backgroundColor", new ColorEvaluator(), from, to);
}
public static ObjectAnimator ofBackgroundColor(View target, int to) {
return ObjectAnimator.ofObject(new ViewBackgroundWrapper(target), "backgroundColor", new ColorEvaluator(), to);
}
private static class ColorEvaluator implements TypeEvaluator