BitmapShader.java revision 866cf65cc3c53f67836c9157d5c661adfdbd25e1
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 long b = bitmap.ni();
46        init(nativeCreate(b, tileX.nativeInt, tileY.nativeInt));
47    }
48
49    /**
50     * @hide
51     */
52    @Override
53    protected Shader copy() {
54        final BitmapShader copy = new BitmapShader(mBitmap, mTileX, mTileY);
55        copyLocalMatrix(copy);
56        return copy;
57    }
58
59    private static native long nativeCreate(long native_bitmap, int shaderTileModeX,
60            int shaderTileModeY);
61}
62