PictureDrawable.java revision cda212d79d449468384cc7744878b8c99984059c
1/*
2 * Copyright (C) 2008 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.drawable;
18
19import android.graphics.Canvas;
20import android.graphics.ColorFilter;
21import android.graphics.drawable.Drawable;
22import android.graphics.Picture;
23import android.graphics.PixelFormat;
24import android.graphics.Rect;
25
26/**
27 * Drawable subclass that wraps a Picture, allowing the picture to be used
28 * whereever a Drawable is supported.
29 */
30public class PictureDrawable extends Drawable {
31
32    private Picture mPicture;
33
34    /**
35     * Construct a new drawable referencing the specified picture. The picture
36     * may be null.
37     *
38     * @param picture The picture to associate with the drawable. May be null.
39     */
40    public PictureDrawable(Picture picture) {
41        mPicture = picture;
42    }
43
44    /**
45     * Return the picture associated with the drawable. May be null.
46     *
47     * @return the picture associated with the drawable, or null.
48     */
49    public Picture getPicture() {
50        return mPicture;
51    }
52
53    /**
54     * Associate a picture with this drawable. The picture may be null.
55     *
56     * @param picture The picture to associate with the drawable. May be null.
57     */
58    public void setPicture(Picture picture) {
59        mPicture = picture;
60    }
61
62    @Override
63    public void draw(Canvas canvas) {
64        if (mPicture != null) {
65            Rect bounds = getBounds();
66            canvas.save();
67            canvas.clipRect(bounds);
68            canvas.translate(bounds.left, bounds.top);
69            canvas.drawPicture(mPicture);
70            canvas.restore();
71        }
72    }
73
74    @Override
75    public int getIntrinsicWidth() {
76        return mPicture != null ? mPicture.getWidth() : -1;
77    }
78
79    @Override
80    public int getIntrinsicHeight() {
81        return mPicture != null ? mPicture.getHeight() : -1;
82    }
83
84    @Override
85    public int getOpacity() {
86        // not sure, so be safe
87        return PixelFormat.TRANSLUCENT;
88    }
89
90    @Override
91    public void setFilterBitmap(boolean filter) {}
92
93    @Override
94    public void setDither(boolean dither) {}
95
96    @Override
97    public void setColorFilter(ColorFilter colorFilter) {}
98
99    @Override
100    public void setAlpha(int alpha) {}
101}
102
103