Home | History | Annotate | Download | only in graphics
      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 long 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 long 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         @Override
    108         public java.awt.PaintContext createContext(
    109                 java.awt.image.ColorModel     colorModel,
    110                 java.awt.Rectangle            deviceBounds,
    111                 java.awt.geom.Rectangle2D     userBounds,
    112                 java.awt.geom.AffineTransform xform,
    113                 java.awt.RenderingHints       hints) {
    114             precomputeGradientColors();
    115 
    116             java.awt.geom.AffineTransform canvasMatrix;
    117             try {
    118                 canvasMatrix = xform.createInverse();
    119             } catch (java.awt.geom.NoninvertibleTransformException e) {
    120                 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_INVERSE,
    121                         "Unable to inverse matrix in RadialGradient", e, null /*data*/);
    122                 canvasMatrix = new java.awt.geom.AffineTransform();
    123             }
    124 
    125             java.awt.geom.AffineTransform localMatrix = getLocalMatrix();
    126             try {
    127                 localMatrix = localMatrix.createInverse();
    128             } catch (java.awt.geom.NoninvertibleTransformException e) {
    129                 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_INVERSE,
    130                         "Unable to inverse matrix in RadialGradient", e, null /*data*/);
    131                 localMatrix = new java.awt.geom.AffineTransform();
    132             }
    133 
    134             return new RadialGradientPaintContext(canvasMatrix, localMatrix, colorModel);
    135         }
    136 
    137         private class RadialGradientPaintContext implements java.awt.PaintContext {
    138 
    139             private final java.awt.geom.AffineTransform mCanvasMatrix;
    140             private final java.awt.geom.AffineTransform mLocalMatrix;
    141             private final java.awt.image.ColorModel mColorModel;
    142 
    143             public RadialGradientPaintContext(
    144                     java.awt.geom.AffineTransform canvasMatrix,
    145                     java.awt.geom.AffineTransform localMatrix,
    146                     java.awt.image.ColorModel colorModel) {
    147                 mCanvasMatrix = canvasMatrix;
    148                 mLocalMatrix = localMatrix;
    149                 mColorModel = colorModel;
    150             }
    151 
    152             @Override
    153             public void dispose() {
    154             }
    155 
    156             @Override
    157             public java.awt.image.ColorModel getColorModel() {
    158                 return mColorModel;
    159             }
    160 
    161             @Override
    162             public java.awt.image.Raster getRaster(int x, int y, int w, int h) {
    163                 java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(w, h,
    164                         java.awt.image.BufferedImage.TYPE_INT_ARGB);
    165 
    166                 int[] data = new int[w*h];
    167 
    168                 // compute distance from each point to the center, and figure out the distance from
    169                 // it.
    170                 int index = 0;
    171                 float[] pt1 = new float[2];
    172                 float[] pt2 = new float[2];
    173                 for (int iy = 0 ; iy < h ; iy++) {
    174                     for (int ix = 0 ; ix < w ; ix++) {
    175                         // handle the canvas transform
    176                         pt1[0] = x + ix;
    177                         pt1[1] = y + iy;
    178                         mCanvasMatrix.transform(pt1, 0, pt2, 0, 1);
    179 
    180                         // handle the local matrix
    181                         pt1[0] = pt2[0] - mX;
    182                         pt1[1] = pt2[1] - mY;
    183                         mLocalMatrix.transform(pt1, 0, pt2, 0, 1);
    184 
    185                         float _x = pt2[0];
    186                         float _y = pt2[1];
    187                         float distance = (float) Math.sqrt(_x * _x + _y * _y);
    188 
    189                         data[index++] = getGradientColor(distance / mRadius);
    190                     }
    191                 }
    192 
    193                 image.setRGB(0 /*startX*/, 0 /*startY*/, w, h, data, 0 /*offset*/, w /*scansize*/);
    194 
    195                 return image.getRaster();
    196             }
    197 
    198         }
    199     }
    200 
    201 }
    202