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