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 java.awt.image.DataBufferInt;
     25 import java.awt.image.Raster;
     26 import java.awt.image.SampleModel;
     27 
     28 /**
     29  * Delegate implementing the native methods of android.graphics.SweepGradient
     30  *
     31  * Through the layoutlib_create tool, the original native methods of SweepGradient 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 SweepGradient 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 SweepGradient_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, int colors[], float
     60             positions[]) {
     61         SweepGradient_Delegate newDelegate = new SweepGradient_Delegate(matrix, x, y, colors,
     62                 positions);
     63         return sManager.addNewDelegate(newDelegate);
     64     }
     65 
     66     @LayoutlibDelegate
     67     /*package*/ static long nativeCreate2(long matrix, float x, float y, int color0, int color1) {
     68         return nativeCreate1(matrix, x, y, new int[] { color0, color1 },
     69                 null /*positions*/);
     70     }
     71 
     72     // ---- Private delegate/helper methods ----
     73 
     74     /**
     75      * A subclass of Shader that draws a sweep gradient around a center point.
     76      *
     77      * @param nativeMatrix reference to the shader's native transformation matrix
     78      * @param cx       The x-coordinate of the center
     79      * @param cy       The y-coordinate of the center
     80      * @param colors   The colors to be distributed between around the center.
     81      *                 There must be at least 2 colors in the array.
     82      * @param positions May be NULL. The relative position of
     83      *                 each corresponding color in the colors array, beginning
     84      *                 with 0 and ending with 1.0. If the values are not
     85      *                 monotonic, the drawing may produce unexpected results.
     86      *                 If positions is NULL, then the colors are automatically
     87      *                 spaced evenly.
     88      */
     89     private SweepGradient_Delegate(long nativeMatrix, float cx, float cy,
     90             int colors[], float positions[]) {
     91         super(nativeMatrix, colors, positions);
     92         mJavaPaint = new SweepGradientPaint(cx, cy, mColors, mPositions);
     93     }
     94 
     95     private class SweepGradientPaint extends GradientPaint {
     96 
     97         private final float mCx;
     98         private final float mCy;
     99 
    100         public SweepGradientPaint(float cx, float cy, int[] colors,
    101                 float[] positions) {
    102             super(colors, positions, null /*tileMode*/);
    103             mCx = cx;
    104             mCy = cy;
    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 SweepGradient", 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 SweepGradient", e, null /*data*/);
    131                 localMatrix = new java.awt.geom.AffineTransform();
    132             }
    133 
    134             return new SweepGradientPaintContext(canvasMatrix, localMatrix, colorModel);
    135         }
    136 
    137         private class SweepGradientPaintContext 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 SweepGradientPaintContext(
    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 
    164                 int[] data = new int[w*h];
    165 
    166                 // compute angle from each point to the center, and figure out the distance from
    167                 // it.
    168                 int index = 0;
    169                 float[] pt1 = new float[2];
    170                 float[] pt2 = new float[2];
    171                 for (int iy = 0 ; iy < h ; iy++) {
    172                     for (int ix = 0 ; ix < w ; ix++) {
    173                         // handle the canvas transform
    174                         pt1[0] = x + ix;
    175                         pt1[1] = y + iy;
    176                         mCanvasMatrix.transform(pt1, 0, pt2, 0, 1);
    177 
    178                         // handle the local matrix
    179                         pt1[0] = pt2[0] - mCx;
    180                         pt1[1] = pt2[1] - mCy;
    181                         mLocalMatrix.transform(pt1, 0, pt2, 0, 1);
    182 
    183                         float dx = pt2[0];
    184                         float dy = pt2[1];
    185 
    186                         float angle;
    187                         if (dx == 0) {
    188                             angle = (float) (dy < 0 ? 3 * Math.PI / 2 : Math.PI / 2);
    189                         } else if (dy == 0) {
    190                             angle = (float) (dx < 0 ? Math.PI : 0);
    191                         } else {
    192                             angle = (float) Math.atan(dy / dx);
    193                             if (dx > 0) {
    194                                 if (dy < 0) {
    195                                     angle += Math.PI * 2;
    196                                 }
    197                             } else {
    198                                 angle += Math.PI;
    199                             }
    200                         }
    201 
    202                         // convert to 0-1. value and get color
    203                         data[index++] = getGradientColor((float) (angle / (2 * Math.PI)));
    204                     }
    205                 }
    206 
    207                 DataBufferInt dataBuffer = new DataBufferInt(data, data.length);
    208                 SampleModel colorModel = mColorModel.createCompatibleSampleModel(w, h);
    209                 return Raster.createWritableRaster(colorModel, dataBuffer, null);
    210             }
    211 
    212         }
    213     }
    214 }
    215