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 
17 package android.graphics.drawable;
18 
19 import android.graphics.Canvas;
20 import android.graphics.ColorFilter;
21 import android.graphics.drawable.Drawable;
22 import android.graphics.Picture;
23 import android.graphics.PixelFormat;
24 import android.graphics.Rect;
25 
26 /**
27  * Drawable subclass that wraps a Picture, allowing the picture to be used
28  * wherever a Drawable is supported.
29  */
30 public 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      */
PictureDrawable(Picture picture)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      */
getPicture()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      */
setPicture(Picture picture)58     public void setPicture(Picture picture) {
59         mPicture = picture;
60     }
61 
62     @Override
draw(Canvas canvas)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
getIntrinsicWidth()75     public int getIntrinsicWidth() {
76         return mPicture != null ? mPicture.getWidth() : -1;
77     }
78 
79     @Override
getIntrinsicHeight()80     public int getIntrinsicHeight() {
81         return mPicture != null ? mPicture.getHeight() : -1;
82     }
83 
84     @Override
getOpacity()85     public int getOpacity() {
86         // not sure, so be safe
87         return PixelFormat.TRANSLUCENT;
88     }
89 
90     @Override
setColorFilter(ColorFilter colorFilter)91     public void setColorFilter(ColorFilter colorFilter) {}
92 
93     @Override
setAlpha(int alpha)94     public void setAlpha(int alpha) {}
95 }
96 
97