• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.res.Resources;
20 import android.graphics.Canvas;
21 import android.graphics.ColorFilter;
22 import android.graphics.Matrix;
23 import android.graphics.Matrix.ScaleToFit;
24 import android.graphics.drawable.Drawable;
25 import android.graphics.Picture;
26 import android.graphics.PixelFormat;
27 import android.graphics.Rect;
28 import 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  */
34 public 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      */
PictureDrawable(Picture picture)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      */
getPicture()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      */
setPicture(Picture picture)62     public void setPicture(Picture picture) {
63         mPicture = picture;
64     }
65 
66     @Override
draw(Canvas canvas)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
getIntrinsicWidth()79     public int getIntrinsicWidth() {
80         return mPicture != null ? mPicture.getWidth() : -1;
81     }
82 
83     @Override
getIntrinsicHeight()84     public int getIntrinsicHeight() {
85         return mPicture != null ? mPicture.getHeight() : -1;
86     }
87 
88     @Override
getOpacity()89     public int getOpacity() {
90         // not sure, so be safe
91         return PixelFormat.TRANSLUCENT;
92     }
93 
94     @Override
setFilterBitmap(boolean filter)95     public void setFilterBitmap(boolean filter) {}
96 
97     @Override
setDither(boolean dither)98     public void setDither(boolean dither) {}
99 
100     @Override
setColorFilter(ColorFilter colorFilter)101     public void setColorFilter(ColorFilter colorFilter) {}
102 
103     @Override
setAlpha(int alpha)104     public void setAlpha(int alpha) {}
105 }
106 
107