Shader.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1/*
2 * Copyright (C) 2006 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
19/**
20 * Shader is the based class for objects that return horizontal spans of colors
21 * during drawing. A subclass of Shader is installed in a Paint calling
22 * paint.setShader(shader). After that any object (other than a bitmap) that is
23 * drawn with that paint will get its color(s) from the shader.
24 */
25public class Shader {
26
27    // this is set by subclasses, but don't make it public
28    /* package */ int native_instance;
29
30    public enum TileMode {
31        /**
32         * replicate the edge color if the shader draws outside of its
33         * original bounds
34         */
35        CLAMP   (0),
36        /**
37         * repeat the shader's image horizontally and vertically
38         */
39        REPEAT  (1),
40        /**
41         * repeat the shader's image horizontally and vertically, alternating
42         * mirror images so that adjacent images always seam
43         */
44        MIRROR  (2);
45
46        TileMode(int nativeInt) {
47            this.nativeInt = nativeInt;
48        }
49        final int nativeInt;
50    }
51
52    /**
53     * Return true if the shader has a non-identity local matrix.
54     * @param localM If not null, it is set to the shader's local matrix.
55     * @return true if the shader has a non-identity local matrix
56     */
57    public boolean getLocalMatrix(Matrix localM) {
58        return nativeGetLocalMatrix(native_instance, localM.native_instance);
59    }
60
61    /**
62     * Set the shader's local matrix. Passing null will reset the shader's
63     * matrix to identity
64     * @param localM The shader's new local matrix, or null to specify identity
65     */
66    public void setLocalMatrix(Matrix localM) {
67        nativeSetLocalMatrix(native_instance,
68                             localM != null ? localM.native_instance : 0);
69    }
70
71    protected void finalize() throws Throwable {
72        nativeDestructor(native_instance);
73    }
74
75    private static native void nativeDestructor(int native_shader);
76    private static native boolean nativeGetLocalMatrix(int native_shader,
77                                                       int matrix_instance);
78    private static native void nativeSetLocalMatrix(int native_shader,
79                                                    int matrix_instance);
80}
81