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