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