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