2
* Copyright (c) 2005-2010 Trident Kirill Grouchnikov. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
10
* o Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
14
* o Neither the name of Trident Kirill Grouchnikov nor the names of
15
* its contributors may be used to endorse or promote products derived
16
* from this software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
package org.pushingpixels.trident.android;
34
import org.pushingpixels.trident.interpolator.PropertyInterpolator;
35
import org.pushingpixels.trident.interpolator.PropertyInterpolatorSource;
37
import android.graphics.*;
40
* Built-in interpolators for Android classes.
42
* @author Kirill Grouchnikov
44
public class AndroidPropertyInterpolators implements PropertyInterpolatorSource {
45
private Set<PropertyInterpolator> interpolators;
47
public static final PropertyInterpolator<Integer> COLOR_INTERPOLATOR = new ColorInterpolator();
49
public AndroidPropertyInterpolators() {
50
this.interpolators = new HashSet<PropertyInterpolator>();
51
this.interpolators.add(COLOR_INTERPOLATOR);
52
this.interpolators.add(new PointInterpolator());
53
this.interpolators.add(new RectInterpolator());
54
this.interpolators.add(new RectFInterpolator());
58
public Set<PropertyInterpolator> getPropertyInterpolators() {
59
return Collections.unmodifiableSet(this.interpolators);
62
static class ColorInterpolator implements PropertyInterpolator<Integer> {
64
public Class getBasePropertyClass() {
69
public Integer interpolate(Integer from, Integer to,
70
float timelinePosition) {
71
return getInterpolatedRGB(from, to, 1.0f - timelinePosition);
74
int getInterpolatedRGB(Integer color1, Integer color2,
75
float color1Likeness) {
76
if ((color1Likeness < 0.0) || (color1Likeness > 1.0))
77
throw new IllegalArgumentException(
78
"Color likeness should be in 0.0-1.0 range [is "
79
+ color1Likeness + "]");
81
if (color1.equals(color2))
83
if (color1Likeness == 1.0)
85
if (color1Likeness == 0.0)
88
int lr = Color.red(color1);
89
int lg = Color.green(color1);
90
int lb = Color.blue(color1);
91
int la = Color.alpha(color1);
92
int dr = Color.red(color2);
93
int dg = Color.green(color2);
94
int db = Color.blue(color2);
95
int da = Color.alpha(color2);
97
// using some interpolation values (such as 0.29 from issue 401)
98
// results in an incorrect final value without Math.round.
99
int r = (lr == dr) ? lr : (int) Math.round(color1Likeness * lr
100
+ (1.0 - color1Likeness) * dr);
101
int g = (lg == dg) ? lg : (int) Math.round(color1Likeness * lg
102
+ (1.0 - color1Likeness) * dg);
103
int b = (lb == db) ? lb : (int) Math.round(color1Likeness * lb
104
+ (1.0 - color1Likeness) * db);
105
int a = (la == da) ? la : (int) Math.round(color1Likeness * la
106
+ (1.0 - color1Likeness) * da);
108
return Color.argb(a, r, g, b);
112
static class PointInterpolator implements PropertyInterpolator<Point> {
114
public Point interpolate(Point from, Point to, float timelinePosition) {
115
int x = from.x + (int) (timelinePosition * (to.x - from.x));
116
int y = from.y + (int) (timelinePosition * (to.y - from.y));
117
return new Point(x, y);
121
public Class getBasePropertyClass() {
126
static class RectInterpolator implements PropertyInterpolator<Rect> {
128
public Rect interpolate(Rect from, Rect to, float timelinePosition) {
130
+ (int) (timelinePosition * (to.left - from.left));
131
int top = from.top + (int) (timelinePosition * (to.top - from.top));
132
int right = from.right
133
+ (int) (timelinePosition * (to.right - from.right));
134
int bottom = from.bottom
135
+ (int) (timelinePosition * (to.bottom - from.bottom));
136
return new Rect(left, top, right, bottom);
140
public Class getBasePropertyClass() {
145
static class RectFInterpolator implements PropertyInterpolator<RectF> {
147
public RectF interpolate(RectF from, RectF to, float timelinePosition) {
148
float left = from.left
149
+ (int) (timelinePosition * (to.left - from.left));
151
+ (int) (timelinePosition * (to.top - from.top));
152
float right = from.right
153
+ (int) (timelinePosition * (to.right - from.right));
154
float bottom = from.bottom
155
+ (int) (timelinePosition * (to.bottom - from.bottom));
156
return new RectF(left, top, right, bottom);
160
public Class getBasePropertyClass() {