2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.ui;
19 import android.content.Context;
20 import android.view.MotionEvent;
22 import com.android.gallery3d.R;
23 import com.android.gallery3d.util.GalleryUtils;
25 public class UndoBarView extends GLView {
26 private static final String TAG = "UndoBarView";
28 private static final int WHITE = 0xFFFFFFFF;
29 private static final int GRAY = 0xFFAAAAAA;
31 private final NinePatchTexture mPanel;
32 private final StringTexture mUndoText;
33 private final StringTexture mDeletedText;
34 private final ResourceTexture mUndoIcon;
35 private final int mBarHeight;
36 private final int mBarMargin;
37 private final int mUndoTextMargin;
38 private final int mIconSize;
39 private final int mIconMargin;
40 private final int mSeparatorTopMargin;
41 private final int mSeparatorBottomMargin;
42 private final int mSeparatorRightMargin;
43 private final int mSeparatorWidth;
44 private final int mDeletedTextMargin;
45 private final int mClickRegion;
47 private OnClickListener mOnClickListener;
48 private boolean mDownOnButton;
50 // This is the layout of UndoBarView. The unit is dp.
52 // +-+----+----------------+-+--+----+-+------+--+-+
53 // 48 | | | Deleted | | | <- | | UNDO | | |
54 // +-+----+----------------+-+--+----+-+------+--+-+
55 // 4 16 1 12 32 8 16 4
56 public UndoBarView(Context context) {
57 mBarHeight = (int) GalleryUtils.dpToPixel(48);
58 mBarMargin = (int) GalleryUtils.dpToPixel(4);
59 mUndoTextMargin = (int) GalleryUtils.dpToPixel(16);
60 mIconMargin = (int) GalleryUtils.dpToPixel(8);
61 mIconSize = (int) GalleryUtils.dpToPixel(32);
62 mSeparatorRightMargin = (int) GalleryUtils.dpToPixel(12);
63 mSeparatorTopMargin = (int) GalleryUtils.dpToPixel(10);
64 mSeparatorBottomMargin = (int) GalleryUtils.dpToPixel(10);
65 mSeparatorWidth = (int) GalleryUtils.dpToPixel(1);
66 mDeletedTextMargin = (int) GalleryUtils.dpToPixel(16);
68 mPanel = new NinePatchTexture(context, R.drawable.panel_undo_holo);
69 mUndoText = StringTexture.newInstance(context.getString(R.string.undo),
70 GalleryUtils.dpToPixel(12), GRAY, 0, true);
71 mDeletedText = StringTexture.newInstance(
72 context.getString(R.string.deleted),
73 GalleryUtils.dpToPixel(16), WHITE);
74 mUndoIcon = new ResourceTexture(
75 context, R.drawable.ic_menu_revert_holo_dark);
76 mClickRegion = mBarMargin + mUndoTextMargin + mUndoText.getWidth()
77 + mIconMargin + mIconSize + mSeparatorRightMargin;
80 public void setOnClickListener(OnClickListener listener) {
81 mOnClickListener = listener;
85 protected void onMeasure(int widthSpec, int heightSpec) {
86 setMeasuredSize(0 /* unused */, mBarHeight);
90 protected void render(GLCanvas canvas) {
94 mPanel.draw(canvas, mBarMargin, 0, w - mBarMargin * 2, mBarHeight);
96 int x = w - mBarMargin;
99 x -= mUndoTextMargin + mUndoText.getWidth();
100 y = (mBarHeight - mUndoText.getHeight()) / 2;
101 mUndoText.draw(canvas, x, y);
103 x -= mIconMargin + mIconSize;
104 y = (mBarHeight - mIconSize) / 2;
105 mUndoIcon.draw(canvas, x, y, mIconSize, mIconSize);
107 x -= mSeparatorRightMargin + mSeparatorWidth;
108 y = mSeparatorTopMargin;
109 canvas.fillRect(x, y, mSeparatorWidth,
110 mBarHeight - mSeparatorTopMargin - mSeparatorBottomMargin, GRAY);
112 x = mBarMargin + mDeletedTextMargin;
113 y = (mBarHeight - mDeletedText.getHeight()) / 2;
114 mDeletedText.draw(canvas, x, y);
118 protected boolean onTouch(MotionEvent event) {
119 switch (event.getAction()) {
120 case MotionEvent.ACTION_DOWN:
121 mDownOnButton = inUndoButton(event);
123 case MotionEvent.ACTION_UP:
125 if (mOnClickListener != null && inUndoButton(event)) {
126 mOnClickListener.onClick(this);
128 mDownOnButton = false;
131 case MotionEvent.ACTION_CANCEL:
132 mDownOnButton = false;
138 // Check if the event is on the right of the separator
139 private boolean inUndoButton(MotionEvent event) {
140 float x = event.getX();
141 float y = event.getY();
144 return (x >= w - mClickRegion && x < w && y >= 0 && y < h);