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        init(nativeCreate1(x0, y0, x1, y1, colors, positions, tile.nativeInt));
69    }
70
71    /** Create a shader that draws a linear gradient along a line.
72        @param x0       The x-coordinate for the start of the gradient line
73        @param y0       The y-coordinate for the start of the gradient line
74        @param x1       The x-coordinate for the end of the gradient line
75        @param y1       The y-coordinate for the end of the gradient line
76        @param  color0  The color at the start of the gradient line.
77        @param  color1  The color at the end of the gradient line.
78        @param  tile    The Shader tiling mode
79    */
80    public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1,
81            TileMode tile) {
82        mType = TYPE_COLOR_START_AND_COLOR_END;
83        mX0 = x0;
84        mY0 = y0;
85        mX1 = x1;
86        mY1 = y1;
87        mColor0 = color0;
88        mColor1 = color1;
89        mTileMode = tile;
90        init(nativeCreate2(x0, y0, x1, y1, color0, color1, tile.nativeInt));
91    }
92
93    /**
94     * @hide
95     */
96    @Override
97    protected Shader copy() {
98        final LinearGradient copy;
99        switch (mType) {
100            case TYPE_COLORS_AND_POSITIONS:
101                copy = new LinearGradient(mX0, mY0, mX1, mY1, mColors.clone(),
102                        mPositions != null ? mPositions.clone() : null, mTileMode);
103                break;
104            case TYPE_COLOR_START_AND_COLOR_END:
105                copy = new LinearGradient(mX0, mY0, mX1, mY1, mColor0, mColor1, mTileMode);
106                break;
107            default:
108                throw new IllegalArgumentException("LinearGradient should be created with either " +
109                        "colors and positions or start color and end color");
110        }
111        copyLocalMatrix(copy);
112        return copy;
113    }
114
115    private native long nativeCreate1(float x0, float y0, float x1, float y1,
116            int colors[], float positions[], int tileMode);
117    private native long nativeCreate2(float x0, float y0, float x1, float y1,
118            int color0, int color1, int tileMode);
119}
120