• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.graphics;
18 
19 import com.android.ide.common.rendering.api.LayoutLog;
20 import com.android.layoutlib.bridge.Bridge;
21 import com.android.layoutlib.bridge.impl.DelegateManager;
22 import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
23 
24 import android.graphics.Shader.TileMode;
25 
26 /**
27  * Delegate implementing the native methods of android.graphics.LinearGradient
28  *
29  * Through the layoutlib_create tool, the original native methods of LinearGradient have been
30  * replaced by calls to methods of the same name in this delegate class.
31  *
32  * This class behaves like the original native implementation, but in Java, keeping previously
33  * native data into its own objects and mapping them to int that are sent back and forth between
34  * it and the original LinearGradient class.
35  *
36  * Because this extends {@link Shader_Delegate}, there's no need to use a {@link DelegateManager},
37  * as all the Shader classes will be added to the manager owned by {@link Shader_Delegate}.
38  *
39  * @see Shader_Delegate
40  *
41  */
42 public final class LinearGradient_Delegate extends Gradient_Delegate {
43 
44     // ---- delegate data ----
45     private java.awt.Paint mJavaPaint;
46 
47     // ---- Public Helper methods ----
48 
49     @Override
getJavaPaint()50     public java.awt.Paint getJavaPaint() {
51         return mJavaPaint;
52     }
53 
54     // ---- native methods ----
55 
56     @LayoutlibDelegate
nativeCreate1( float x0, float y0, float x1, float y1, int colors[], float positions[], int tileMode)57     /*package*/ static int nativeCreate1(
58             float x0, float y0, float x1, float y1,
59             int colors[], float positions[], int tileMode) {
60         LinearGradient_Delegate newDelegate = new LinearGradient_Delegate(x0, y0, x1, y1,
61                 colors, positions, Shader_Delegate.getTileMode(tileMode));
62         return sManager.addNewDelegate(newDelegate);
63     }
64 
65     @LayoutlibDelegate
nativeCreate2( float x0, float y0, float x1, float y1, int color0, int color1, int tileMode)66     /*package*/ static int nativeCreate2(
67             float x0, float y0, float x1, float y1,
68             int color0, int color1, int tileMode) {
69         return nativeCreate1(x0, y0, x1, y1, new int[] { color0, color1}, null /*positions*/,
70                 tileMode);
71     }
72 
73     // ---- Private delegate/helper methods ----
74 
75     /**
76      * Create a shader that draws a linear gradient along a line.
77      *
78      * @param x0 The x-coordinate for the start of the gradient line
79      * @param y0 The y-coordinate for the start of the gradient line
80      * @param x1 The x-coordinate for the end of the gradient line
81      * @param y1 The y-coordinate for the end of the gradient line
82      * @param colors The colors to be distributed along the gradient line
83      * @param positions May be null. The relative positions [0..1] of each
84      *            corresponding color in the colors array. If this is null, the
85      *            the colors are distributed evenly along the gradient line.
86      * @param tile The Shader tiling mode
87      */
LinearGradient_Delegate(float x0, float y0, float x1, float y1, int colors[], float positions[], TileMode tile)88     private LinearGradient_Delegate(float x0, float y0, float x1, float y1,
89             int colors[], float positions[], TileMode tile) {
90         super(colors, positions);
91         mJavaPaint = new LinearGradientPaint(x0, y0, x1, y1, mColors, mPositions, tile);
92     }
93 
94     // ---- Custom Java Paint ----
95     /**
96      * Linear Gradient (Java) Paint able to handle more than 2 points, as
97      * {@link java.awt.GradientPaint} only supports 2 points and does not support Android's tile
98      * modes.
99      */
100     private class LinearGradientPaint extends GradientPaint {
101 
102         private final float mX0;
103         private final float mY0;
104         private final float mDx;
105         private final float mDy;
106         private final float mDSize2;
107 
LinearGradientPaint(float x0, float y0, float x1, float y1, int colors[], float positions[], TileMode tile)108         public LinearGradientPaint(float x0, float y0, float x1, float y1, int colors[],
109                 float positions[], TileMode tile) {
110             super(colors, positions, tile);
111             mX0 = x0;
112             mY0 = y0;
113             mDx = x1 - x0;
114             mDy = y1 - y0;
115             mDSize2 = mDx * mDx + mDy * mDy;
116         }
117 
createContext( java.awt.image.ColorModel colorModel, java.awt.Rectangle deviceBounds, java.awt.geom.Rectangle2D userBounds, java.awt.geom.AffineTransform xform, java.awt.RenderingHints hints)118         public java.awt.PaintContext createContext(
119                 java.awt.image.ColorModel      colorModel,
120                 java.awt.Rectangle             deviceBounds,
121                 java.awt.geom.Rectangle2D      userBounds,
122                 java.awt.geom.AffineTransform  xform,
123                 java.awt.RenderingHints        hints) {
124             precomputeGradientColors();
125 
126             java.awt.geom.AffineTransform canvasMatrix;
127             try {
128                 canvasMatrix = xform.createInverse();
129             } catch (java.awt.geom.NoninvertibleTransformException e) {
130                 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_INVERSE,
131                         "Unable to inverse matrix in LinearGradient", e, null /*data*/);
132                 canvasMatrix = new java.awt.geom.AffineTransform();
133             }
134 
135             java.awt.geom.AffineTransform localMatrix = getLocalMatrix();
136             try {
137                 localMatrix = localMatrix.createInverse();
138             } catch (java.awt.geom.NoninvertibleTransformException e) {
139                 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_INVERSE,
140                         "Unable to inverse matrix in LinearGradient", e, null /*data*/);
141                 localMatrix = new java.awt.geom.AffineTransform();
142             }
143 
144             return new LinearGradientPaintContext(canvasMatrix, localMatrix, colorModel);
145         }
146 
147         private class LinearGradientPaintContext implements java.awt.PaintContext {
148 
149             private final java.awt.geom.AffineTransform mCanvasMatrix;
150             private final java.awt.geom.AffineTransform mLocalMatrix;
151             private final java.awt.image.ColorModel mColorModel;
152 
LinearGradientPaintContext( java.awt.geom.AffineTransform canvasMatrix, java.awt.geom.AffineTransform localMatrix, java.awt.image.ColorModel colorModel)153             private LinearGradientPaintContext(
154                     java.awt.geom.AffineTransform canvasMatrix,
155                     java.awt.geom.AffineTransform localMatrix,
156                     java.awt.image.ColorModel colorModel) {
157                 mCanvasMatrix = canvasMatrix;
158                 mLocalMatrix = localMatrix;
159                 mColorModel = colorModel;
160             }
161 
dispose()162             public void dispose() {
163             }
164 
getColorModel()165             public java.awt.image.ColorModel getColorModel() {
166                 return mColorModel;
167             }
168 
getRaster(int x, int y, int w, int h)169             public java.awt.image.Raster getRaster(int x, int y, int w, int h) {
170                 java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(w, h,
171                         java.awt.image.BufferedImage.TYPE_INT_ARGB);
172 
173                 int[] data = new int[w*h];
174 
175                 int index = 0;
176                 float[] pt1 = new float[2];
177                 float[] pt2 = new float[2];
178                 for (int iy = 0 ; iy < h ; iy++) {
179                     for (int ix = 0 ; ix < w ; ix++) {
180                         // handle the canvas transform
181                         pt1[0] = x + ix;
182                         pt1[1] = y + iy;
183                         mCanvasMatrix.transform(pt1, 0, pt2, 0, 1);
184 
185                         // handle the local matrix.
186                         pt1[0] = pt2[0];
187                         pt1[1] = pt2[1];
188                         mLocalMatrix.transform(pt1, 0, pt2, 0, 1);
189 
190                         data[index++] = getColor(pt2[0], pt2[1]);
191                     }
192                 }
193 
194                 image.setRGB(0 /*startX*/, 0 /*startY*/, w, h, data, 0 /*offset*/, w /*scansize*/);
195 
196                 return image.getRaster();
197             }
198         }
199 
200         /**
201          * Returns a color for an arbitrary point.
202          */
getColor(float x, float y)203         private int getColor(float x, float y) {
204             float pos;
205             if (mDx == 0) {
206                 pos = (y - mY0) / mDy;
207             } else if (mDy == 0) {
208                 pos = (x - mX0) / mDx;
209             } else {
210                 // find the x position on the gradient vector.
211                 float _x = (mDx*mDy*(y-mY0) + mDy*mDy*mX0 + mDx*mDx*x) / mDSize2;
212                 // from it get the position relative to the vector
213                 pos = (_x - mX0) / mDx;
214             }
215 
216             return getGradientColor(pos);
217         }
218     }
219 }
220