Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2008 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 public class SweepGradient extends GradientShader {
     20 
     21     private SweepGradientPaint mPaint;
     22 
     23     /**
     24      * A subclass of Shader that draws a sweep gradient around a center point.
     25      *
     26      * @param cx       The x-coordinate of the center
     27      * @param cy       The y-coordinate of the center
     28      * @param colors   The colors to be distributed between around the center.
     29      *                 There must be at least 2 colors in the array.
     30      * @param positions May be NULL. The relative position of
     31      *                 each corresponding color in the colors array, beginning
     32      *                 with 0 and ending with 1.0. If the values are not
     33      *                 monotonic, the drawing may produce unexpected results.
     34      *                 If positions is NULL, then the colors are automatically
     35      *                 spaced evenly.
     36      */
     37     public SweepGradient(float cx, float cy,
     38                          int colors[], float positions[]) {
     39         super(colors, positions);
     40 
     41         mPaint = new SweepGradientPaint(cx, cy, mColors, mPositions);
     42     }
     43 
     44     /**
     45      * A subclass of Shader that draws a sweep gradient around a center point.
     46      *
     47      * @param cx       The x-coordinate of the center
     48      * @param cy       The y-coordinate of the center
     49      * @param color0   The color to use at the start of the sweep
     50      * @param color1   The color to use at the end of the sweep
     51      */
     52     public SweepGradient(float cx, float cy, int color0, int color1) {
     53         this(cx, cy, new int[] { color0, color1}, null /*positions*/);
     54     }
     55 
     56     @Override
     57     java.awt.Paint getJavaPaint() {
     58         return mPaint;
     59     }
     60 
     61     private static class SweepGradientPaint extends GradientPaint {
     62 
     63         private final float mCx;
     64         private final float mCy;
     65 
     66         public SweepGradientPaint(float cx, float cy, int[] colors, float[] positions) {
     67             super(colors, positions, null /*tileMode*/);
     68             mCx = cx;
     69             mCy = cy;
     70         }
     71 
     72         public java.awt.PaintContext createContext(
     73                 java.awt.image.ColorModel     colorModel,
     74                 java.awt.Rectangle            deviceBounds,
     75                 java.awt.geom.Rectangle2D     userBounds,
     76                 java.awt.geom.AffineTransform xform,
     77                 java.awt.RenderingHints       hints) {
     78             precomputeGradientColors();
     79             return new SweepGradientPaintContext(colorModel);
     80         }
     81 
     82         private class SweepGradientPaintContext implements java.awt.PaintContext {
     83 
     84             private final java.awt.image.ColorModel mColorModel;
     85 
     86             public SweepGradientPaintContext(java.awt.image.ColorModel colorModel) {
     87                 mColorModel = colorModel;
     88             }
     89 
     90             public void dispose() {
     91             }
     92 
     93             public java.awt.image.ColorModel getColorModel() {
     94                 return mColorModel;
     95             }
     96 
     97             public java.awt.image.Raster getRaster(int x, int y, int w, int h) {
     98                 java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(w, h,
     99                         java.awt.image.BufferedImage.TYPE_INT_ARGB);
    100 
    101                 int[] data = new int[w*h];
    102 
    103                 // compute angle from each point to the center, and figure out the distance from
    104                 // it.
    105                 int index = 0;
    106                 for (int iy = 0 ; iy < h ; iy++) {
    107                     for (int ix = 0 ; ix < w ; ix++) {
    108                         float dx = x + ix - mCx;
    109                         float dy = y + iy - mCy;
    110                         float angle;
    111                         if (dx == 0) {
    112                             angle = (float) (dy < 0 ? 3 * Math.PI / 2 : Math.PI / 2);
    113                         } else if (dy == 0) {
    114                             angle = (float) (dx < 0 ? Math.PI : 0);
    115                         } else {
    116                             angle = (float) Math.atan(dy / dx);
    117                             if (dx > 0) {
    118                                 if (dy < 0) {
    119                                     angle += Math.PI * 2;
    120                                 }
    121                             } else {
    122                                 angle += Math.PI;
    123                             }
    124                         }
    125 
    126                         // convert to 0-1. value and get color
    127                         data[index++] = getGradientColor((float) (angle / (2 * Math.PI)));
    128                     }
    129                 }
    130 
    131                 image.setRGB(0 /*startX*/, 0 /*startY*/, w, h, data, 0 /*offset*/, w /*scansize*/);
    132 
    133                 return image.getRaster();
    134             }
    135 
    136         }
    137     }
    138 
    139 }
    140 
    141