1/*
2 * Copyright (C) 2007 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
17package android.graphics;
18
19public class LinearGradient extends Shader {
20
21    private static final int TYPE_COLORS_AND_POSITIONS = 1;
22    private static final int TYPE_COLOR_START_AND_COLOR_END = 2;
23
24    /**
25     * Type of the LinearGradient: can be either TYPE_COLORS_AND_POSITIONS or
26     * TYPE_COLOR_START_AND_COLOR_END.
27     */
28    private int mType;
29
30    private float mX0;
31    private float mY0;
32    private float mX1;
33    private float mY1;
34    private int[] mColors;
35    private float[] mPositions;
36    private int mColor0;
37    private int mColor1;
38
39    private TileMode mTileMode;
40
41	/**	Create a shader that draws a linear gradient along a line.
42        @param x0           The x-coordinate for the start of the gradient line
43        @param y0           The y-coordinate for the start of the gradient line
44        @param x1           The x-coordinate for the end of the gradient line
45        @param y1           The y-coordinate for the end of the gradient line
46        @param  colors      The colors to be distributed along the gradient line
47        @param  positions   May be null. The relative positions [0..1] of
48                            each corresponding color in the colors array. If this is null,
49                            the the colors are distributed evenly along the gradient line.
50        @param  tile        The Shader tiling mode
51	*/
52	public LinearGradient(float x0, float y0, float x1, float y1, int colors[], float positions[],
53            TileMode tile) {
54        if (colors.length < 2) {
55            throw new IllegalArgumentException("needs >= 2 number of colors");
56        }
57        if (positions != null && colors.length != positions.length) {
58            throw new IllegalArgumentException("color and position arrays must be of equal length");
59        }
60        mType = TYPE_COLORS_AND_POSITIONS;
61        mX0 = x0;
62        mY0 = y0;
63        mX1 = x1;
64        mY1 = y1;
65        mColors = colors;
66        mPositions = positions;
67        mTileMode = tile;
68        native_instance = nativeCreate1(x0, y0, x1, y1, colors, positions, tile.nativeInt);
69        native_shader = nativePostCreate1(native_instance, x0, y0, x1, y1, colors, positions,
70                tile.nativeInt);
71    }
72
73	/**	Create a shader that draws a linear gradient along a line.
74        @param x0       The x-coordinate for the start of the gradient line
75        @param y0       The y-coordinate for the start of the gradient line
76        @param x1       The x-coordinate for the end of the gradient line
77        @param y1       The y-coordinate for the end of the gradient line
78        @param  color0  The color at the start of the gradient line.
79        @param  color1  The color at the end of the gradient line.
80        @param  tile    The Shader tiling mode
81	*/
82	public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1,
83            TileMode tile) {
84        mType = TYPE_COLOR_START_AND_COLOR_END;
85        mX0 = x0;
86        mY0 = y0;
87        mX1 = x1;
88        mY1 = y1;
89        mColor0 = color0;
90        mColor1 = color1;
91        mTileMode = tile;
92        native_instance = nativeCreate2(x0, y0, x1, y1, color0, color1, tile.nativeInt);
93        native_shader = nativePostCreate2(native_instance, x0, y0, x1, y1, color0, color1,
94                tile.nativeInt);
95    }
96
97    /**
98     * @hide
99     */
100    @Override
101    protected Shader copy() {
102        final LinearGradient copy;
103        switch (mType) {
104            case TYPE_COLORS_AND_POSITIONS:
105                copy = new LinearGradient(mX0, mY0, mX1, mY1, mColors.clone(),
106                        mPositions != null ? mPositions.clone() : null, mTileMode);
107                break;
108            case TYPE_COLOR_START_AND_COLOR_END:
109                copy = new LinearGradient(mX0, mY0, mX1, mY1, mColor0, mColor1, mTileMode);
110                break;
111            default:
112                throw new IllegalArgumentException("LinearGradient should be created with either " +
113                        "colors and positions or start color and end color");
114        }
115        copyLocalMatrix(copy);
116        return copy;
117    }
118
119    private native int nativeCreate1(float x0, float y0, float x1, float y1,
120            int colors[], float positions[], int tileMode);
121	private native int nativeCreate2(float x0, float y0, float x1, float y1,
122            int color0, int color1, int tileMode);
123    private native int nativePostCreate1(int native_shader, float x0, float y0, float x1, float y1,
124            int colors[], float positions[], int tileMode);
125    private native int nativePostCreate2(int native_shader, float x0, float y0, float x1, float y1,
126            int color0, int color1, int tileMode);
127}
128