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