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 /** 25 * Delegate implementing the native methods of android.graphics.SweepGradient 26 * 27 * Through the layoutlib_create tool, the original native methods of SweepGradient have been 28 * replaced by calls to methods of the same name in this delegate class. 29 * 30 * This class behaves like the original native implementation, but in Java, keeping previously 31 * native data into its own objects and mapping them to int that are sent back and forth between 32 * it and the original SweepGradient class. 33 * 34 * Because this extends {@link Shader_Delegate}, there's no need to use a {@link DelegateManager}, 35 * as all the Shader classes will be added to the manager owned by {@link Shader_Delegate}. 36 * 37 * @see Shader_Delegate 38 * 39 */ 40 public class SweepGradient_Delegate extends Gradient_Delegate { 41 42 // ---- delegate data ---- 43 private java.awt.Paint mJavaPaint; 44 45 // ---- Public Helper methods ---- 46 47 @Override 48 public java.awt.Paint getJavaPaint() { 49 return mJavaPaint; 50 } 51 52 // ---- native methods ---- 53 54 @LayoutlibDelegate 55 /*package*/ static int nativeCreate1(float x, float y, int colors[], float positions[]) { 56 SweepGradient_Delegate newDelegate = new SweepGradient_Delegate(x, y, colors, positions); 57 return sManager.addNewDelegate(newDelegate); 58 } 59 60 @LayoutlibDelegate 61 /*package*/ static int nativeCreate2(float x, float y, int color0, int color1) { 62 return nativeCreate1(x, y, new int[] { color0, color1 }, null /*positions*/); 63 } 64 65 @LayoutlibDelegate 66 /*package*/ static int nativePostCreate1(int native_shader, float cx, float cy, 67 int[] colors, float[] positions) { 68 // nothing to be done here. 69 return 0; 70 } 71 72 @LayoutlibDelegate 73 /*package*/ static int nativePostCreate2(int native_shader, float cx, float cy, 74 int color0, int color1) { 75 // nothing to be done here. 76 return 0; 77 } 78 79 // ---- Private delegate/helper methods ---- 80 81 /** 82 * A subclass of Shader that draws a sweep gradient around a center point. 83 * 84 * @param cx The x-coordinate of the center 85 * @param cy The y-coordinate of the center 86 * @param colors The colors to be distributed between around the center. 87 * There must be at least 2 colors in the array. 88 * @param positions May be NULL. The relative position of 89 * each corresponding color in the colors array, beginning 90 * with 0 and ending with 1.0. If the values are not 91 * monotonic, the drawing may produce unexpected results. 92 * If positions is NULL, then the colors are automatically 93 * spaced evenly. 94 */ 95 private SweepGradient_Delegate(float cx, float cy, 96 int colors[], float positions[]) { 97 super(colors, positions); 98 mJavaPaint = new SweepGradientPaint(cx, cy, mColors, mPositions); 99 } 100 101 private class SweepGradientPaint extends GradientPaint { 102 103 private final float mCx; 104 private final float mCy; 105 106 public SweepGradientPaint(float cx, float cy, int[] colors, 107 float[] positions) { 108 super(colors, positions, null /*tileMode*/); 109 mCx = cx; 110 mCy = cy; 111 } 112 113 public java.awt.PaintContext createContext( 114 java.awt.image.ColorModel colorModel, 115 java.awt.Rectangle deviceBounds, 116 java.awt.geom.Rectangle2D userBounds, 117 java.awt.geom.AffineTransform xform, 118 java.awt.RenderingHints hints) { 119 precomputeGradientColors(); 120 121 java.awt.geom.AffineTransform canvasMatrix; 122 try { 123 canvasMatrix = xform.createInverse(); 124 } catch (java.awt.geom.NoninvertibleTransformException e) { 125 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_INVERSE, 126 "Unable to inverse matrix in SweepGradient", e, null /*data*/); 127 canvasMatrix = new java.awt.geom.AffineTransform(); 128 } 129 130 java.awt.geom.AffineTransform localMatrix = getLocalMatrix(); 131 try { 132 localMatrix = localMatrix.createInverse(); 133 } catch (java.awt.geom.NoninvertibleTransformException e) { 134 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_INVERSE, 135 "Unable to inverse matrix in SweepGradient", e, null /*data*/); 136 localMatrix = new java.awt.geom.AffineTransform(); 137 } 138 139 return new SweepGradientPaintContext(canvasMatrix, localMatrix, colorModel); 140 } 141 142 private class SweepGradientPaintContext implements java.awt.PaintContext { 143 144 private final java.awt.geom.AffineTransform mCanvasMatrix; 145 private final java.awt.geom.AffineTransform mLocalMatrix; 146 private final java.awt.image.ColorModel mColorModel; 147 148 public SweepGradientPaintContext( 149 java.awt.geom.AffineTransform canvasMatrix, 150 java.awt.geom.AffineTransform localMatrix, 151 java.awt.image.ColorModel colorModel) { 152 mCanvasMatrix = canvasMatrix; 153 mLocalMatrix = localMatrix; 154 mColorModel = colorModel; 155 } 156 157 public void dispose() { 158 } 159 160 public java.awt.image.ColorModel getColorModel() { 161 return mColorModel; 162 } 163 164 public java.awt.image.Raster getRaster(int x, int y, int w, int h) { 165 java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(w, h, 166 java.awt.image.BufferedImage.TYPE_INT_ARGB); 167 168 int[] data = new int[w*h]; 169 170 // compute angle from each point to the center, and figure out the distance from 171 // it. 172 int index = 0; 173 float[] pt1 = new float[2]; 174 float[] pt2 = new float[2]; 175 for (int iy = 0 ; iy < h ; iy++) { 176 for (int ix = 0 ; ix < w ; ix++) { 177 // handle the canvas transform 178 pt1[0] = x + ix; 179 pt1[1] = y + iy; 180 mCanvasMatrix.transform(pt1, 0, pt2, 0, 1); 181 182 // handle the local matrix 183 pt1[0] = pt2[0] - mCx; 184 pt1[1] = pt2[1] - mCy; 185 mLocalMatrix.transform(pt1, 0, pt2, 0, 1); 186 187 float dx = pt2[0]; 188 float dy = pt2[1]; 189 190 float angle; 191 if (dx == 0) { 192 angle = (float) (dy < 0 ? 3 * Math.PI / 2 : Math.PI / 2); 193 } else if (dy == 0) { 194 angle = (float) (dx < 0 ? Math.PI : 0); 195 } else { 196 angle = (float) Math.atan(dy / dx); 197 if (dx > 0) { 198 if (dy < 0) { 199 angle += Math.PI * 2; 200 } 201 } else { 202 angle += Math.PI; 203 } 204 } 205 206 // convert to 0-1. value and get color 207 data[index++] = getGradientColor((float) (angle / (2 * Math.PI))); 208 } 209 } 210 211 image.setRGB(0 /*startX*/, 0 /*startY*/, w, h, data, 0 /*offset*/, w /*scansize*/); 212 213 return image.getRaster(); 214 } 215 216 } 217 } 218 } 219