1 /* 2 * Copyright (C) 2007 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 import android.annotation.NonNull; 20 21 /** 22 * Shader used to draw a bitmap as a texture. The bitmap can be repeated or 23 * mirrored by setting the tiling mode. 24 */ 25 public class BitmapShader extends Shader { 26 /** 27 * Prevent garbage collection. 28 * @hide 29 */ 30 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) 31 public final Bitmap mBitmap; 32 33 private TileMode mTileX; 34 private TileMode mTileY; 35 36 /** 37 * Call this to create a new shader that will draw with a bitmap. 38 * 39 * @param bitmap The bitmap to use inside the shader 40 * @param tileX The tiling mode for x to draw the bitmap in. 41 * @param tileY The tiling mode for y to draw the bitmap in. 42 */ BitmapShader(@onNull Bitmap bitmap, TileMode tileX, TileMode tileY)43 public BitmapShader(@NonNull Bitmap bitmap, TileMode tileX, TileMode tileY) { 44 mBitmap = bitmap; 45 mTileX = tileX; 46 mTileY = tileY; 47 init(nativeCreate(bitmap, tileX.nativeInt, tileY.nativeInt)); 48 } 49 50 /** 51 * @hide 52 */ 53 @Override copy()54 protected Shader copy() { 55 final BitmapShader copy = new BitmapShader(mBitmap, mTileX, mTileY); 56 copyLocalMatrix(copy); 57 return copy; 58 } 59 nativeCreate(Bitmap bitmap, int shaderTileModeX, int shaderTileModeY)60 private static native long nativeCreate(Bitmap bitmap, int shaderTileModeX, 61 int shaderTileModeY); 62 } 63