NinePatchDrawable.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1/*
2 * Copyright (C) 2006 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.*;
20
21/**
22 *
23 * A resizeable bitmap, with stretchable areas that you define. This type of image
24 * is defined in a .png file with a special format, described in <a link="../../../resources.html#ninepatch">
25 * Resources</a>.
26 *
27 */
28public class NinePatchDrawable extends Drawable {
29
30    public NinePatchDrawable(Bitmap bitmap, byte[] chunk,
31                             Rect padding, String srcName) {
32        this(new NinePatchState(new NinePatch(bitmap, chunk, srcName), padding));
33    }
34
35    public NinePatchDrawable(NinePatch patch) {
36        this(new NinePatchState(patch, null));
37    }
38
39    // overrides
40
41    @Override
42    public void draw(Canvas canvas) {
43        mNinePatch.draw(canvas, getBounds(), mPaint);
44    }
45
46    @Override
47    public int getChangingConfigurations() {
48        return super.getChangingConfigurations()
49                | mNinePatchState.mChangingConfigurations;
50    }
51
52    @Override
53    public boolean getPadding(Rect padding) {
54        padding.set(mPadding);
55        return true;
56    }
57
58    @Override
59    public void setAlpha(int alpha) {
60        getPaint().setAlpha(alpha);
61    }
62
63    @Override
64    public void setColorFilter(ColorFilter cf) {
65        getPaint().setColorFilter(cf);
66    }
67
68    @Override
69    public void setDither(boolean dither) {
70        getPaint().setDither(dither);
71    }
72
73    public Paint getPaint() {
74        if (mPaint == null) {
75            mPaint = new Paint();
76        }
77        return mPaint;
78    }
79
80    /**
81     * Retrieves the width of the source .png file (before resizing).
82     */
83    @Override
84    public int getIntrinsicWidth() {
85        return mNinePatch.getWidth();
86    }
87
88    /**
89     * Retrieves the height of the source .png file (before resizing).
90     */
91    @Override
92    public int getIntrinsicHeight() {
93        return mNinePatch.getHeight();
94    }
95
96    @Override
97    public int getMinimumWidth() {
98        return mNinePatch.getWidth();
99    }
100
101    @Override
102    public int getMinimumHeight() {
103        return mNinePatch.getHeight();
104    }
105
106    /**
107     * Returns a {@link android.graphics.PixelFormat graphics.PixelFormat} value of OPAQUE or TRANSLUCENT.
108     */
109    @Override
110    public int getOpacity() {
111        return mNinePatch.hasAlpha() || (mPaint != null && mPaint.getAlpha() < 255)
112            ? PixelFormat.TRANSLUCENT : PixelFormat.OPAQUE;
113    }
114
115    @Override
116    public Region getTransparentRegion() {
117        return mNinePatch.getTransparentRegion(getBounds());
118    }
119
120    @Override
121    public ConstantState getConstantState() {
122        mNinePatchState.mChangingConfigurations = super.getChangingConfigurations();
123        return mNinePatchState;
124    }
125
126    final static class NinePatchState extends ConstantState {
127        NinePatchState(NinePatch ninePatch, Rect padding)
128        {
129            mNinePatch = ninePatch;
130            mPadding = padding;
131        }
132
133        @Override
134        public Drawable newDrawable()
135        {
136            return new NinePatchDrawable(this);
137        }
138
139        @Override
140        public int getChangingConfigurations() {
141            return mChangingConfigurations;
142        }
143
144        final NinePatch mNinePatch;
145        final Rect      mPadding;
146        int             mChangingConfigurations;
147    }
148
149    private NinePatchDrawable(NinePatchState state) {
150        mNinePatchState = state;
151        mNinePatch = state.mNinePatch;
152        mPadding = state.mPadding;
153    }
154
155    private final NinePatchState    mNinePatchState;
156    private final NinePatch         mNinePatch;
157    private final Rect              mPadding;
158    private Paint                   mPaint;
159}
160
161