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 RadialGradient extends GradientShader {
     20 
     21     private RadialGradientPaint mPaint;
     22 
     23     /**
     24      * Create a shader that draws a radial gradient given the center and radius.
     25      *
     26      * @param x The x-coordinate of the center of the radius
     27      * @param y The y-coordinate of the center of the radius
     28      * @param radius Must be positive. The radius of the circle for this
     29      *            gradient
     30      * @param colors The colors to be distributed between the center and edge of
     31      *            the circle
     32      * @param positions May be NULL. The relative position of each corresponding
     33      *            color in the colors array. If this is NULL, the the colors are
     34      *            distributed evenly between the center and edge of the circle.
     35      * @param tile The Shader tiling mode
     36      */
     37     public RadialGradient(float x, float y, float radius, int colors[], float positions[],
     38             TileMode tile) {
     39         super(colors, positions);
     40         if (radius <= 0) {
     41             throw new IllegalArgumentException("radius must be > 0");
     42         }
     43 
     44         mPaint = new RadialGradientPaint(x, y, radius, mColors, mPositions, tile);
     45     }
     46 
     47     /**
     48      * Create a shader that draws a radial gradient given the center and radius.
     49      *
     50      * @param x The x-coordinate of the center of the radius
     51      * @param y The y-coordinate of the center of the radius
     52      * @param radius Must be positive. The radius of the circle for this
     53      *            gradient
     54      * @param color0 The color at the center of the circle.
     55      * @param color1 The color at the edge of the circle.
     56      * @param tile The Shader tiling mode
     57      */
     58     public RadialGradient(float x, float y, float radius, int color0, int color1, TileMode tile) {
     59         this(x, y, radius, new int[] { color0, color1 }, null /* positions */, tile);
     60     }
     61 
     62     @Override
     63     java.awt.Paint getJavaPaint() {
     64         return mPaint;
     65     }
     66 
     67     private static class RadialGradientPaint extends GradientPaint {
     68 
     69         private final float mX;
     70         private final float mY;
     71         private final float mRadius;
     72 
     73         public RadialGradientPaint(float x, float y, float radius, int[] colors, float[] positions, TileMode mode) {
     74             super(colors, positions, mode);
     75             mX = x;
     76             mY = y;
     77             mRadius = radius;
     78         }
     79 
     80         public java.awt.PaintContext createContext(
     81                 java.awt.image.ColorModel     colorModel,
     82                 java.awt.Rectangle            deviceBounds,
     83                 java.awt.geom.Rectangle2D     userBounds,
     84                 java.awt.geom.AffineTransform xform,
     85                 java.awt.RenderingHints       hints) {
     86             precomputeGradientColors();
     87             return new RadialGradientPaintContext(colorModel);
     88         }
     89 
     90         private class RadialGradientPaintContext implements java.awt.PaintContext {
     91 
     92             private final java.awt.image.ColorModel mColorModel;
     93 
     94             public RadialGradientPaintContext(java.awt.image.ColorModel colorModel) {
     95                 mColorModel = colorModel;
     96             }
     97 
     98             public void dispose() {
     99             }
    100 
    101             public java.awt.image.ColorModel getColorModel() {
    102                 return mColorModel;
    103             }
    104 
    105             public java.awt.image.Raster getRaster(int x, int y, int w, int h) {
    106                 java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(w, h,
    107                         java.awt.image.BufferedImage.TYPE_INT_ARGB);
    108 
    109                 int[] data = new int[w*h];
    110 
    111                 // compute distance from each point to the center, and figure out the distance from
    112                 // it.
    113                 int index = 0;
    114                 for (int iy = 0 ; iy < h ; iy++) {
    115                     for (int ix = 0 ; ix < w ; ix++) {
    116                         float _x = x + ix - mX;
    117                         float _y = y + iy - mY;
    118                         float distance = (float) Math.sqrt(_x * _x + _y * _y);
    119 
    120                         data[index++] = getGradientColor(distance / mRadius);
    121                     }
    122                 }
    123 
    124                 image.setRGB(0 /*startX*/, 0 /*startY*/, w, h, data, 0 /*offset*/, w /*scansize*/);
    125 
    126                 return image.getRaster();
    127             }
    128 
    129         }
    130     }
    131 
    132 }
    133