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.RadialGradient 28 * 29 * Through the layoutlib_create tool, the original native methods of RadialGradient 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 RadialGradient 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 class RadialGradient_Delegate extends Gradient_Delegate { 43 44 // ---- delegate data ---- 45 private java.awt.Paint mJavaPaint; 46 47 // ---- Public Helper methods ---- 48 49 @Override 50 public java.awt.Paint getJavaPaint() { 51 return mJavaPaint; 52 } 53 54 // ---- native methods ---- 55 56 @LayoutlibDelegate 57 /*package*/ static int nativeCreate1(float x, float y, float radius, 58 int colors[], float positions[], int tileMode) { 59 RadialGradient_Delegate newDelegate = new RadialGradient_Delegate(x, y, radius, 60 colors, positions, Shader_Delegate.getTileMode(tileMode)); 61 return sManager.addNewDelegate(newDelegate); 62 } 63 64 @LayoutlibDelegate 65 /*package*/ static int nativeCreate2(float x, float y, float radius, 66 int color0, int color1, int tileMode) { 67 return nativeCreate1(x, y, radius, new int[] { color0, color1 }, null /*positions*/, 68 tileMode); 69 } 70 71 // ---- Private delegate/helper methods ---- 72 73 /** 74 * Create a shader that draws a radial gradient given the center and radius. 75 * 76 * @param x The x-coordinate of the center of the radius 77 * @param y The y-coordinate of the center of the radius 78 * @param radius Must be positive. The radius of the circle for this 79 * gradient 80 * @param colors The colors to be distributed between the center and edge of 81 * the circle 82 * @param positions May be NULL. The relative position of each corresponding 83 * color in the colors array. If this is NULL, the the colors are 84 * distributed evenly between the center and edge of the circle. 85 * @param tile The Shader tiling mode 86 */ 87 private RadialGradient_Delegate(float x, float y, float radius, int colors[], float positions[], 88 TileMode tile) { 89 super(colors, positions); 90 mJavaPaint = new RadialGradientPaint(x, y, radius, mColors, mPositions, tile); 91 } 92 93 private class RadialGradientPaint extends GradientPaint { 94 95 private final float mX; 96 private final float mY; 97 private final float mRadius; 98 99 public RadialGradientPaint(float x, float y, float radius, 100 int[] colors, float[] positions, TileMode mode) { 101 super(colors, positions, mode); 102 mX = x; 103 mY = y; 104 mRadius = radius; 105 } 106 107 public java.awt.PaintContext createContext( 108 java.awt.image.ColorModel colorModel, 109 java.awt.Rectangle deviceBounds, 110 java.awt.geom.Rectangle2D userBounds, 111 java.awt.geom.AffineTransform xform, 112 java.awt.RenderingHints hints) { 113 precomputeGradientColors(); 114 115 java.awt.geom.AffineTransform canvasMatrix; 116 try { 117 canvasMatrix = xform.createInverse(); 118 } catch (java.awt.geom.NoninvertibleTransformException e) { 119 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_INVERSE, 120 "Unable to inverse matrix in RadialGradient", e, null /*data*/); 121 canvasMatrix = new java.awt.geom.AffineTransform(); 122 } 123 124 java.awt.geom.AffineTransform localMatrix = getLocalMatrix(); 125 try { 126 localMatrix = localMatrix.createInverse(); 127 } catch (java.awt.geom.NoninvertibleTransformException e) { 128 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_INVERSE, 129 "Unable to inverse matrix in RadialGradient", e, null /*data*/); 130 localMatrix = new java.awt.geom.AffineTransform(); 131 } 132 133 return new RadialGradientPaintContext(canvasMatrix, localMatrix, colorModel); 134 } 135 136 private class RadialGradientPaintContext implements java.awt.PaintContext { 137 138 private final java.awt.geom.AffineTransform mCanvasMatrix; 139 private final java.awt.geom.AffineTransform mLocalMatrix; 140 private final java.awt.image.ColorModel mColorModel; 141 142 public RadialGradientPaintContext( 143 java.awt.geom.AffineTransform canvasMatrix, 144 java.awt.geom.AffineTransform localMatrix, 145 java.awt.image.ColorModel colorModel) { 146 mCanvasMatrix = canvasMatrix; 147 mLocalMatrix = localMatrix; 148 mColorModel = colorModel; 149 } 150 151 public void dispose() { 152 } 153 154 public java.awt.image.ColorModel getColorModel() { 155 return mColorModel; 156 } 157 158 public java.awt.image.Raster getRaster(int x, int y, int w, int h) { 159 java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(w, h, 160 java.awt.image.BufferedImage.TYPE_INT_ARGB); 161 162 int[] data = new int[w*h]; 163 164 // compute distance from each point to the center, and figure out the distance from 165 // it. 166 int index = 0; 167 float[] pt1 = new float[2]; 168 float[] pt2 = new float[2]; 169 for (int iy = 0 ; iy < h ; iy++) { 170 for (int ix = 0 ; ix < w ; ix++) { 171 // handle the canvas transform 172 pt1[0] = x + ix; 173 pt1[1] = y + iy; 174 mCanvasMatrix.transform(pt1, 0, pt2, 0, 1); 175 176 // handle the local matrix 177 pt1[0] = pt2[0] - mX; 178 pt1[1] = pt2[1] - mY; 179 mLocalMatrix.transform(pt1, 0, pt2, 0, 1); 180 181 float _x = pt2[0]; 182 float _y = pt2[1]; 183 float distance = (float) Math.sqrt(_x * _x + _y * _y); 184 185 data[index++] = getGradientColor(distance / mRadius); 186 } 187 } 188 189 image.setRGB(0 /*startX*/, 0 /*startY*/, w, h, data, 0 /*offset*/, w /*scansize*/); 190 191 return image.getRaster(); 192 } 193 194 } 195 } 196 197 } 198