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