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 private static final String TAG = "UndoBarView";
29 private static final int WHITE = 0xFFFFFFFF;
30 private static final int GRAY = 0xFFAAAAAA;
32 private final NinePatchTexture mPanel;
33 private final StringTexture mUndoText;
34 private final StringTexture mDeletedText;
35 private final ResourceTexture mUndoIcon;
36 private final int mBarHeight;
37 private final int mBarMargin;
38 private final int mUndoTextMargin;
39 private final int mIconSize;
40 private final int mIconMargin;
41 private final int mSeparatorTopMargin;
42 private final int mSeparatorBottomMargin;
43 private final int mSeparatorRightMargin;
44 private final int mSeparatorWidth;
45 private final int mDeletedTextMargin;
46 private final int mClickRegion;
48 private OnClickListener mOnClickListener;
49 private boolean mDownOnButton;
51 // This is the layout of UndoBarView. The unit is dp.
53 // +-+----+----------------+-+--+----+-+------+--+-+
54 // 48 | | | Deleted | | | <- | | UNDO | | |
55 // +-+----+----------------+-+--+----+-+------+--+-+
56 // 4 16 1 12 32 8 16 4
57 public UndoBarView(Context context) {
58 mBarHeight = (int) GalleryUtils.dpToPixel(48);
59 mBarMargin = (int) GalleryUtils.dpToPixel(4);
60 mUndoTextMargin = (int) GalleryUtils.dpToPixel(16);
61 mIconMargin = (int) GalleryUtils.dpToPixel(8);
62 mIconSize = (int) GalleryUtils.dpToPixel(32);
63 mSeparatorRightMargin = (int) GalleryUtils.dpToPixel(12);
64 mSeparatorTopMargin = (int) GalleryUtils.dpToPixel(10);
65 mSeparatorBottomMargin = (int) GalleryUtils.dpToPixel(10);
66 mSeparatorWidth = (int) GalleryUtils.dpToPixel(1);
67 mDeletedTextMargin = (int) GalleryUtils.dpToPixel(16);
69 mPanel = new NinePatchTexture(context, R.drawable.panel_undo_holo);
70 mUndoText = StringTexture.newInstance(context.getString(R.string.undo),
71 GalleryUtils.dpToPixel(12), GRAY, 0, true);
72 mDeletedText = StringTexture.newInstance(
73 context.getString(R.string.deleted),
74 GalleryUtils.dpToPixel(16), WHITE);
75 mUndoIcon = new ResourceTexture(
76 context, R.drawable.ic_menu_revert_holo_dark);
77 mClickRegion = mBarMargin + mUndoTextMargin + mUndoText.getWidth()
78 + mIconMargin + mIconSize + mSeparatorRightMargin;
81 public void setOnClickListener(OnClickListener listener) {
82 mOnClickListener = listener;
86 protected void onMeasure(int widthSpec, int heightSpec) {
87 setMeasuredSize(0 /* unused */, mBarHeight);
91 protected void render(GLCanvas canvas) {
95 canvas.save(GLCanvas.SAVE_FLAG_ALPHA);
96 canvas.multiplyAlpha(mAlpha);
100 mPanel.draw(canvas, mBarMargin, 0, w - mBarMargin * 2, mBarHeight);
102 int x = w - mBarMargin;
105 x -= mUndoTextMargin + mUndoText.getWidth();
106 y = (mBarHeight - mUndoText.getHeight()) / 2;
107 mUndoText.draw(canvas, x, y);
109 x -= mIconMargin + mIconSize;
110 y = (mBarHeight - mIconSize) / 2;
111 mUndoIcon.draw(canvas, x, y, mIconSize, mIconSize);
113 x -= mSeparatorRightMargin + mSeparatorWidth;
114 y = mSeparatorTopMargin;
115 canvas.fillRect(x, y, mSeparatorWidth,
116 mBarHeight - mSeparatorTopMargin - mSeparatorBottomMargin, GRAY);
118 x = mBarMargin + mDeletedTextMargin;
119 y = (mBarHeight - mDeletedText.getHeight()) / 2;
120 mDeletedText.draw(canvas, x, y);
126 protected boolean onTouch(MotionEvent event) {
127 switch (event.getAction()) {
128 case MotionEvent.ACTION_DOWN:
129 mDownOnButton = inUndoButton(event);
131 case MotionEvent.ACTION_UP:
133 if (mOnClickListener != null && inUndoButton(event)) {
134 mOnClickListener.onClick(this);
136 mDownOnButton = false;
139 case MotionEvent.ACTION_CANCEL:
140 mDownOnButton = false;
146 // Check if the event is on the right of the separator
147 private boolean inUndoButton(MotionEvent event) {
148 float x = event.getX();
149 float y = event.getY();
152 return (x >= w - mClickRegion && x < w && y >= 0 && y < h);
155 ////////////////////////////////////////////////////////////////////////////
157 ////////////////////////////////////////////////////////////////////////////
159 private static final long NO_ANIMATION = -1;
160 private static long ANIM_TIME = 200;
161 private long mAnimationStartTime = NO_ANIMATION;
162 private float mFromAlpha, mToAlpha;
163 private float mAlpha;
165 private static float getTargetAlpha(int visibility) {
166 return (visibility == VISIBLE) ? 1f : 0f;
169 public void setVisibility(int visibility) {
170 mAlpha = getTargetAlpha(visibility);
171 mAnimationStartTime = NO_ANIMATION;
172 super.setVisibility(visibility);
176 public void animateVisibility(int visibility) {
177 float target = getTargetAlpha(visibility);
178 if (mAnimationStartTime == NO_ANIMATION && mAlpha == target) return;
179 if (mAnimationStartTime != NO_ANIMATION && mToAlpha == target) return;
183 mAnimationStartTime = AnimationTime.startTime();
185 super.setVisibility(VISIBLE);
189 private void advanceAnimation() {
190 if (mAnimationStartTime == NO_ANIMATION) return;
192 float delta = (float) (AnimationTime.get() - mAnimationStartTime) /
194 mAlpha = mFromAlpha + ((mToAlpha > mFromAlpha) ? delta : -delta);
195 mAlpha = Utils.clamp(mAlpha, 0f, 1f);
197 if (mAlpha == mToAlpha) {
198 mAnimationStartTime = NO_ANIMATION;
200 super.setVisibility(INVISIBLE);