Shader.java revision d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895
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     * Local matrix native instance.
28     *
29     * @hide
30     */
31    public int mLocalMatrix;
32
33    /**
34     * This is set by subclasses, but don't make it public.
35     *
36     * @hide
37     */
38    public int native_instance;
39
40    public enum TileMode {
41        /**
42         * replicate the edge color if the shader draws outside of its
43         * original bounds
44         */
45        CLAMP   (0),
46        /**
47         * repeat the shader's image horizontally and vertically
48         */
49        REPEAT  (1),
50        /**
51         * repeat the shader's image horizontally and vertically, alternating
52         * mirror images so that adjacent images always seam
53         */
54        MIRROR  (2);
55
56        TileMode(int nativeInt) {
57            this.nativeInt = nativeInt;
58        }
59        final int nativeInt;
60    }
61
62    /**
63     * Return true if the shader has a non-identity local matrix.
64     * @param localM If not null, it is set to the shader's local matrix.
65     * @return true if the shader has a non-identity local matrix
66     */
67    public boolean getLocalMatrix(Matrix localM) {
68        return nativeGetLocalMatrix(native_instance, localM.native_instance);
69    }
70
71    /**
72     * Set the shader's local matrix. Passing null will reset the shader's
73     * matrix to identity
74     * @param localM The shader's new local matrix, or null to specify identity
75     */
76    public void setLocalMatrix(Matrix localM) {
77        mLocalMatrix = localM != null ? localM.native_instance : 0;
78        nativeSetLocalMatrix(native_instance, mLocalMatrix);
79    }
80
81    protected void finalize() throws Throwable {
82        super.finalize();
83        nativeDestructor(native_instance);
84    }
85
86    private static native void nativeDestructor(int native_shader);
87    private static native boolean nativeGetLocalMatrix(int native_shader,
88                                                       int matrix_instance);
89    private static native void nativeSetLocalMatrix(int native_shader,
90                                                    int matrix_instance);
91}
92