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.common.Utils;
24 import com.android.gallery3d.util.GalleryUtils;
26 public class UndoBarView extends GLView {
27 @SuppressWarnings("unused")
28 private static final String TAG = "UndoBarView";
30 private static final int WHITE = 0xFFFFFFFF;
31 private static final int GRAY = 0xFFAAAAAA;
33 private final NinePatchTexture mPanel;
34 private final StringTexture mUndoText;
35 private final StringTexture mDeletedText;
36 private final ResourceTexture mUndoIcon;
37 private final int mBarHeight;
38 private final int mBarMargin;
39 private final int mUndoTextMargin;
40 private final int mIconSize;
41 private final int mIconMargin;
42 private final int mSeparatorTopMargin;
43 private final int mSeparatorBottomMargin;
44 private final int mSeparatorRightMargin;
45 private final int mSeparatorWidth;
46 private final int mDeletedTextMargin;
47 private final int mClickRegion;
49 private OnClickListener mOnClickListener;
50 private boolean mDownOnButton;
52 // This is the layout of UndoBarView. The unit is dp.
54 // +-+----+----------------+-+--+----+-+------+--+-+
55 // 48 | | | Deleted | | | <- | | UNDO | | |
56 // +-+----+----------------+-+--+----+-+------+--+-+
57 // 4 16 1 12 32 8 16 4
58 public UndoBarView(Context context) {
59 mBarHeight = GalleryUtils.dpToPixel(48);
60 mBarMargin = GalleryUtils.dpToPixel(4);
61 mUndoTextMargin = GalleryUtils.dpToPixel(16);
62 mIconMargin = GalleryUtils.dpToPixel(8);
63 mIconSize = GalleryUtils.dpToPixel(32);
64 mSeparatorRightMargin = GalleryUtils.dpToPixel(12);
65 mSeparatorTopMargin = GalleryUtils.dpToPixel(10);
66 mSeparatorBottomMargin = GalleryUtils.dpToPixel(10);
67 mSeparatorWidth = GalleryUtils.dpToPixel(1);
68 mDeletedTextMargin = GalleryUtils.dpToPixel(16);
70 mPanel = new NinePatchTexture(context, R.drawable.panel_undo_holo);
71 mUndoText = StringTexture.newInstance(context.getString(R.string.undo),
72 GalleryUtils.dpToPixel(12), GRAY, 0, true);
73 mDeletedText = StringTexture.newInstance(
74 context.getString(R.string.deleted),
75 GalleryUtils.dpToPixel(16), WHITE);
76 mUndoIcon = new ResourceTexture(
77 context, R.drawable.ic_menu_revert_holo_dark);
78 mClickRegion = mBarMargin + mUndoTextMargin + mUndoText.getWidth()
79 + mIconMargin + mIconSize + mSeparatorRightMargin;
82 public void setOnClickListener(OnClickListener listener) {
83 mOnClickListener = listener;
87 protected void onMeasure(int widthSpec, int heightSpec) {
88 setMeasuredSize(0 /* unused */, mBarHeight);
92 protected void render(GLCanvas canvas) {
96 canvas.save(GLCanvas.SAVE_FLAG_ALPHA);
97 canvas.multiplyAlpha(mAlpha);
101 mPanel.draw(canvas, mBarMargin, 0, w - mBarMargin * 2, mBarHeight);
103 int x = w - mBarMargin;
106 x -= mUndoTextMargin + mUndoText.getWidth();
107 y = (mBarHeight - mUndoText.getHeight()) / 2;
108 mUndoText.draw(canvas, x, y);
110 x -= mIconMargin + mIconSize;
111 y = (mBarHeight - mIconSize) / 2;
112 mUndoIcon.draw(canvas, x, y, mIconSize, mIconSize);
114 x -= mSeparatorRightMargin + mSeparatorWidth;
115 y = mSeparatorTopMargin;
116 canvas.fillRect(x, y, mSeparatorWidth,
117 mBarHeight - mSeparatorTopMargin - mSeparatorBottomMargin, GRAY);
119 x = mBarMargin + mDeletedTextMargin;
120 y = (mBarHeight - mDeletedText.getHeight()) / 2;
121 mDeletedText.draw(canvas, x, y);
127 protected boolean onTouch(MotionEvent event) {
128 switch (event.getAction()) {
129 case MotionEvent.ACTION_DOWN:
130 mDownOnButton = inUndoButton(event);
132 case MotionEvent.ACTION_UP:
134 if (mOnClickListener != null && inUndoButton(event)) {
135 mOnClickListener.onClick(this);
137 mDownOnButton = false;
140 case MotionEvent.ACTION_CANCEL:
141 mDownOnButton = false;
147 // Check if the event is on the right of the separator
148 private boolean inUndoButton(MotionEvent event) {
149 float x = event.getX();
150 float y = event.getY();
153 return (x >= w - mClickRegion && x < w && y >= 0 && y < h);
156 ////////////////////////////////////////////////////////////////////////////
158 ////////////////////////////////////////////////////////////////////////////
160 private static final long NO_ANIMATION = -1;
161 private static long ANIM_TIME = 200;
162 private long mAnimationStartTime = NO_ANIMATION;
163 private float mFromAlpha, mToAlpha;
164 private float mAlpha;
166 private static float getTargetAlpha(int visibility) {
167 return (visibility == VISIBLE) ? 1f : 0f;
171 public void setVisibility(int visibility) {
172 mAlpha = getTargetAlpha(visibility);
173 mAnimationStartTime = NO_ANIMATION;
174 super.setVisibility(visibility);
178 public void animateVisibility(int visibility) {
179 float target = getTargetAlpha(visibility);
180 if (mAnimationStartTime == NO_ANIMATION && mAlpha == target) return;
181 if (mAnimationStartTime != NO_ANIMATION && mToAlpha == target) return;
185 mAnimationStartTime = AnimationTime.startTime();
187 super.setVisibility(VISIBLE);
191 private void advanceAnimation() {
192 if (mAnimationStartTime == NO_ANIMATION) return;
194 float delta = (float) (AnimationTime.get() - mAnimationStartTime) /
196 mAlpha = mFromAlpha + ((mToAlpha > mFromAlpha) ? delta : -delta);
197 mAlpha = Utils.clamp(mAlpha, 0f, 1f);
199 if (mAlpha == mToAlpha) {
200 mAnimationStartTime = NO_ANIMATION;
202 super.setVisibility(INVISIBLE);