Shader.java revision ab87983a11e0bd2e08d752d86d5e945ea7d39a04
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 * 74 * Starting with {@link android.os.Build.VERSION_CODES#L}, this does not 75 * modify any Paints which use this Shader. In order to modify the Paint, 76 * you need to call {@link Paint#setShader} again. 77 * 78 * @param localM The shader's new local matrix, or null to specify identity 79 */ 80 public void setLocalMatrix(Matrix localM) { 81 mLocalMatrix = localM; 82 native_instance = nativeSetLocalMatrix(native_instance, 83 localM == null ? 0 : localM.native_instance); 84 } 85 86 protected void finalize() throws Throwable { 87 try { 88 super.finalize(); 89 } finally { 90 nativeDestructor(native_instance); 91 } 92 } 93 94 /** 95 * @hide 96 */ 97 protected Shader copy() { 98 final Shader copy = new Shader(); 99 copyLocalMatrix(copy); 100 return copy; 101 } 102 103 /** 104 * @hide 105 */ 106 protected void copyLocalMatrix(Shader dest) { 107 if (mLocalMatrix != null) { 108 final Matrix lm = new Matrix(); 109 getLocalMatrix(lm); 110 dest.setLocalMatrix(lm); 111 } else { 112 dest.setLocalMatrix(null); 113 } 114 } 115 116 private static native void nativeDestructor(long native_shader); 117 private static native long nativeSetLocalMatrix(long native_shader, 118 long matrix_instance); 119} 120