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 com.android.camera;
18
19import com.android.gallery.R;
20
21import android.content.Context;
22import android.graphics.Canvas;
23import android.graphics.Rect;
24import android.graphics.drawable.Drawable;
25import android.util.AttributeSet;
26import android.widget.ImageView;
27
28class GalleryPickerItem extends ImageView {
29    private Drawable mFrame;
30    private Rect mFrameBounds = new Rect();
31    private Drawable mOverlay;
32
33    public GalleryPickerItem(Context context) {
34        this(context, null);
35    }
36
37    public GalleryPickerItem(Context context, AttributeSet attrs) {
38        this(context, attrs, 0);
39    }
40
41    public GalleryPickerItem(Context context,
42                             AttributeSet attrs,
43                             int defStyle) {
44        super(context, attrs, defStyle);
45
46        mFrame = getResources().getDrawable(R.drawable.frame_gallery_preview);
47        mFrame.setCallback(this);
48    }
49
50    @Override
51    protected boolean verifyDrawable(Drawable who) {
52        return super.verifyDrawable(who) || (who == mFrame)
53                || (who == mOverlay);
54    }
55
56    @Override
57    protected void drawableStateChanged() {
58        super.drawableStateChanged();
59        if (mFrame != null) {
60            int[] drawableState = getDrawableState();
61            mFrame.setState(drawableState);
62        }
63    }
64
65    @Override
66    protected void onDraw(Canvas canvas) {
67        super.onDraw(canvas);
68        final Rect frameBounds = mFrameBounds;
69        if (frameBounds.isEmpty()) {
70            final int w = getWidth();
71            final int h = getHeight();
72
73            frameBounds.set(0, 0, w, h);
74            mFrame.setBounds(frameBounds);
75            if (mOverlay != null) {
76                mOverlay.setBounds(w - mOverlay.getIntrinsicWidth(),
77                        h - mOverlay.getIntrinsicHeight(), w, h);
78            }
79        }
80
81        mFrame.draw(canvas);
82        if (mOverlay != null) {
83            mOverlay.draw(canvas);
84        }
85    }
86
87
88    @Override
89    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
90        super.onSizeChanged(w, h, oldw, oldh);
91
92        mFrameBounds.setEmpty();
93    }
94
95    public void setOverlay(int overlayId) {
96        if (overlayId >= 0) {
97            mOverlay = getResources().getDrawable(overlayId);
98            mFrameBounds.setEmpty();
99        } else {
100            mOverlay = null;
101        }
102    }
103}
104