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 17 package 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 */ 25 public class Shader { 26 /** 27 * This is set by subclasses, but don't make it public. 28 */ 29 private long native_instance; 30 31 /** 32 * Initialization step that should be called by subclasses in their 33 * constructors. Calling again may result in memory leaks. 34 * @hide 35 */ init(long ni)36 protected void init(long ni) { 37 native_instance = ni; 38 } 39 40 private Matrix mLocalMatrix; 41 42 public enum TileMode { 43 /** 44 * replicate the edge color if the shader draws outside of its 45 * original bounds 46 */ 47 CLAMP (0), 48 /** 49 * repeat the shader's image horizontally and vertically 50 */ 51 REPEAT (1), 52 /** 53 * repeat the shader's image horizontally and vertically, alternating 54 * mirror images so that adjacent images always seam 55 */ 56 MIRROR (2); 57 TileMode(int nativeInt)58 TileMode(int nativeInt) { 59 this.nativeInt = nativeInt; 60 } 61 final int nativeInt; 62 } 63 64 /** 65 * Return true if the shader has a non-identity local matrix. 66 * @param localM If not null, it is set to the shader's local matrix. 67 * @return true if the shader has a non-identity local matrix 68 */ getLocalMatrix(Matrix localM)69 public boolean getLocalMatrix(Matrix localM) { 70 if (mLocalMatrix != null) { 71 localM.set(mLocalMatrix); 72 return !mLocalMatrix.isIdentity(); 73 } 74 return false; 75 } 76 77 /** 78 * Set the shader's local matrix. Passing null will reset the shader's 79 * matrix to identity. 80 * 81 * @param localM The shader's new local matrix, or null to specify identity 82 */ setLocalMatrix(Matrix localM)83 public void setLocalMatrix(Matrix localM) { 84 mLocalMatrix = localM; 85 nativeSetLocalMatrix(native_instance, localM == null ? 0 : localM.native_instance); 86 } 87 finalize()88 protected void finalize() throws Throwable { 89 try { 90 super.finalize(); 91 } finally { 92 nativeDestructor(native_instance); 93 } 94 } 95 96 /** 97 * @hide 98 */ copy()99 protected Shader copy() { 100 final Shader copy = new Shader(); 101 copyLocalMatrix(copy); 102 return copy; 103 } 104 105 /** 106 * @hide 107 */ copyLocalMatrix(Shader dest)108 protected void copyLocalMatrix(Shader dest) { 109 if (mLocalMatrix != null) { 110 final Matrix lm = new Matrix(); 111 getLocalMatrix(lm); 112 dest.setLocalMatrix(lm); 113 } else { 114 dest.setLocalMatrix(null); 115 } 116 } 117 getNativeInstance()118 /* package */ long getNativeInstance() { 119 return native_instance; 120 } 121 nativeDestructor(long native_shader)122 private static native void nativeDestructor(long native_shader); nativeSetLocalMatrix(long native_shader, long matrix_instance)123 private static native void nativeSetLocalMatrix(long native_shader, long matrix_instance); 124 } 125