LinearGradient.java revision 7fac2e18339f765320d759e8d4c090f92431959e
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     * These fields are manipulated by the JNI layer, don't touch!
22     * @hide
23     */
24    public int bounds;
25    /**
26     * @hide
27     */
28    public int colors;
29    /**
30     * @hide
31     */
32    public int positions;
33    /**
34     * @hide
35     */
36    public int count;
37    /**
38     * @hide
39     */
40    public int tileMode;
41
42	/**	Create a shader that draws a linear gradient along a line.
43        @param x0           The x-coordinate for the start of the gradient line
44        @param y0           The y-coordinate for the start of the gradient line
45        @param x1           The x-coordinate for the end of the gradient line
46        @param y1           The y-coordinate for the end of the gradient line
47        @param  colors      The colors to be distributed along the gradient line
48        @param  positions   May be null. The relative positions [0..1] of
49                            each corresponding color in the colors array. If this is null,
50                            the the colors are distributed evenly along the gradient line.
51        @param  tile        The Shader tiling mode
52	*/
53	public LinearGradient(float x0, float y0, float x1, float y1,
54                          int colors[], float positions[], TileMode tile) {
55        if (colors.length < 2) {
56            throw new IllegalArgumentException("needs >= 2 number of colors");
57        }
58        if (positions != null && colors.length != positions.length) {
59            throw new IllegalArgumentException("color and position arrays must be of equal length");
60        }
61        native_instance = nativeCreate1(x0, y0, x1, y1, colors, positions, tile.nativeInt);
62        count = colors.length;
63        tileMode = tile.nativeInt;
64    }
65
66	/**	Create a shader that draws a linear gradient along a line.
67        @param x0       The x-coordinate for the start of the gradient line
68        @param y0       The y-coordinate for the start of the gradient line
69        @param x1       The x-coordinate for the end of the gradient line
70        @param y1       The y-coordinate for the end of the gradient line
71        @param  color0  The color at the start of the gradient line.
72        @param  color1  The color at the end of the gradient line.
73        @param  tile    The Shader tiling mode
74	*/
75	public LinearGradient(float x0, float y0, float x1, float y1,
76                          int color0, int color1, TileMode tile) {
77        native_instance = nativeCreate2(x0, y0, x1, y1, color0, color1, tile.nativeInt);
78        count = 2;
79        tileMode = tile.nativeInt;
80    }
81
82    protected void finalize() throws Throwable {
83        super.finalize();
84        nativeDestructor(native_instance);
85    }
86
87    private native void nativeDestructor(int native_shader);
88	private native int nativeCreate1(float x0, float y0, float x1, float y1,
89            int colors[], float positions[], int tileMode);
90	private native int nativeCreate2(float x0, float y0, float x1, float y1,
91            int color0, int color1, int tileMode);
92}
93