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