~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to trident/src/main/java/org/pushingpixels/trident/android/AndroidPropertyInterpolators.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2010 Trident Kirill Grouchnikov. All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without 
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 * 
 
7
 *  o Redistributions of source code must retain the above copyright notice, 
 
8
 *    this list of conditions and the following disclaimer. 
 
9
 *     
 
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. 
 
13
 *     
 
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. 
 
17
 *     
 
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. 
 
29
 */
 
30
package org.pushingpixels.trident.android;
 
31
 
 
32
import java.util.*;
 
33
 
 
34
import org.pushingpixels.trident.interpolator.PropertyInterpolator;
 
35
import org.pushingpixels.trident.interpolator.PropertyInterpolatorSource;
 
36
 
 
37
import android.graphics.*;
 
38
 
 
39
/**
 
40
 * Built-in interpolators for Android classes.
 
41
 * 
 
42
 * @author Kirill Grouchnikov
 
43
 */
 
44
public class AndroidPropertyInterpolators implements PropertyInterpolatorSource {
 
45
        private Set<PropertyInterpolator> interpolators;
 
46
 
 
47
        public static final PropertyInterpolator<Integer> COLOR_INTERPOLATOR = new ColorInterpolator();
 
48
 
 
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());
 
55
        }
 
56
 
 
57
        @Override
 
58
        public Set<PropertyInterpolator> getPropertyInterpolators() {
 
59
                return Collections.unmodifiableSet(this.interpolators);
 
60
        }
 
61
 
 
62
        static class ColorInterpolator implements PropertyInterpolator<Integer> {
 
63
                @Override
 
64
                public Class getBasePropertyClass() {
 
65
                        return Color.class;
 
66
                }
 
67
 
 
68
                @Override
 
69
                public Integer interpolate(Integer from, Integer to,
 
70
                                float timelinePosition) {
 
71
                        return getInterpolatedRGB(from, to, 1.0f - timelinePosition);
 
72
                }
 
73
 
 
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 + "]");
 
80
 
 
81
                        if (color1.equals(color2))
 
82
                                return color1;
 
83
                        if (color1Likeness == 1.0)
 
84
                                return color1;
 
85
                        if (color1Likeness == 0.0)
 
86
                                return color2;
 
87
 
 
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);
 
96
 
 
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);
 
107
 
 
108
                        return Color.argb(a, r, g, b);
 
109
                }
 
110
        }
 
111
 
 
112
        static class PointInterpolator implements PropertyInterpolator<Point> {
 
113
                @Override
 
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);
 
118
                }
 
119
 
 
120
                @Override
 
121
                public Class getBasePropertyClass() {
 
122
                        return Point.class;
 
123
                }
 
124
        }
 
125
 
 
126
        static class RectInterpolator implements PropertyInterpolator<Rect> {
 
127
                @Override
 
128
        public Rect interpolate(Rect from, Rect to, float timelinePosition) {
 
129
                        int left = from.left
 
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);
 
137
                }
 
138
 
 
139
                @Override
 
140
                public Class getBasePropertyClass() {
 
141
                        return Rect.class;
 
142
                }
 
143
        }
 
144
 
 
145
        static class RectFInterpolator implements PropertyInterpolator<RectF> {
 
146
                @Override
 
147
        public RectF interpolate(RectF from, RectF to, float timelinePosition) {
 
148
                        float left = from.left
 
149
                                        + (int) (timelinePosition * (to.left - from.left));
 
150
                        float top = from.top
 
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);
 
157
                }
 
158
 
 
159
                @Override
 
160
                public Class getBasePropertyClass() {
 
161
                        return RectF.class;
 
162
                }
 
163
        }
 
164
}