OSDN Git Service

Merge "Fade in/out the undo bar." into jb-dev
authorWei Huang <weih@google.com>
Wed, 20 Jun 2012 18:35:46 +0000 (11:35 -0700)
committerAndroid (Google) Code Review <android-gerrit@google.com>
Wed, 20 Jun 2012 18:35:46 +0000 (11:35 -0700)
src/com/android/gallery3d/app/PhotoPage.java
src/com/android/gallery3d/ui/PhotoView.java
src/com/android/gallery3d/ui/UndoBarView.java

index 1d9344b..c944543 100644 (file)
@@ -721,7 +721,7 @@ public class PhotoPage extends ActivityState implements
         mDeletePath = path;
         mDeleteIsFocus = (offset == 0);
         mMediaSet.setDeletion(path, mCurrentIndex + offset);
-        mPhotoView.showUndoButton(true);
+        mPhotoView.showUndoBar();
     }
 
     @Override
@@ -731,14 +731,14 @@ public class PhotoPage extends ActivityState implements
         if (mDeleteIsFocus) mModel.setFocusHintPath(mDeletePath);
         mMediaSet.setDeletion(null, 0);
         mDeletePath = null;
-        mPhotoView.showUndoButton(false);
+        mPhotoView.hideUndoBar();
     }
 
     @Override
     public void onCommitDeleteImage() {
         if (mDeletePath == null) return;
         commitDeletion();
-        mPhotoView.showUndoButton(false);
+        mPhotoView.hideUndoBar();
     }
 
     private void commitDeletion() {
index 3fba261..496d34d 100644 (file)
@@ -141,6 +141,7 @@ public class PhotoView extends GLView {
     private static final int MSG_CAPTURE_ANIMATION_DONE = 4;
     private static final int MSG_DELETE_ANIMATION_DONE = 5;
     private static final int MSG_DELETE_DONE = 6;
+    private static final int MSG_HIDE_UNDO_BAR = 7;
 
     private static final int MOVE_THRESHOLD = 256;
     private static final float SWIPE_THRESHOLD = 300f;
@@ -221,6 +222,10 @@ public class PhotoView extends GLView {
     // Whether the box indicated by mTouchBoxIndex is deletable. Only meaningful
     // if mTouchBoxIndex is not Integer.MAX_VALUE.
     private boolean mTouchBoxDeletable;
+    // This is the index of the last deleted item. This is only used as a hint
+    // to hide the undo button when we are too far away from the deleted
+    // item. The value Integer.MAX_VALUE means there is no such hint.
+    private int mUndoIndexHint = Integer.MAX_VALUE;
 
     public PhotoView(GalleryActivity activity) {
         mTileView = new TileImageView(activity);
@@ -334,6 +339,10 @@ public class PhotoView extends GLView {
                     }
                     break;
                 }
+                case MSG_HIDE_UNDO_BAR: {
+                    checkHideUndoBar(UNDO_BAR_TIMEOUT);
+                    break;
+                }
                 default: throw new AssertionError(message.what);
             }
         }
@@ -359,6 +368,13 @@ public class PhotoView extends GLView {
             }
         }
 
+        // Hide undo button if we are too far away
+        if (mUndoIndexHint != Integer.MAX_VALUE) {
+            if (Math.abs(mUndoIndexHint - mModel.getCurrentIndex()) >= 3) {
+                hideUndoBar();
+            }
+        }
+
         // Update the ScreenNails.
         for (int i = -SCREEN_NAIL_MAX; i <= SCREEN_NAIL_MAX; i++) {
             Picture p =  mPictures.get(i);
@@ -1041,6 +1057,8 @@ public class PhotoView extends GLView {
         private void deleteAfterAnimation(int duration) {
             MediaItem item = mModel.getMediaItem(mTouchBoxIndex);
             if (item == null) return;
+            mListener.onCommitDeleteImage();
+            mUndoIndexHint = mModel.getCurrentIndex() + mTouchBoxIndex;
             mHolding |= HOLD_DELETE;
             Message m = mHandler.obtainMessage(MSG_DELETE_ANIMATION_DONE);
             m.obj = item.getPath();
@@ -1136,9 +1154,10 @@ public class PhotoView extends GLView {
 
         @Override
         public void onDown(float x, float y) {
+            checkHideUndoBar(UNDO_BAR_TOUCHED);
+
             mDeltaY = 0;
             mSeenScaling = false;
-            mListener.onCommitDeleteImage();
 
             if (mIgnoreSwipingGesture) return;
 
@@ -1248,8 +1267,39 @@ public class PhotoView extends GLView {
         setFilmMode(false);
     }
 
-    public void showUndoButton(boolean show) {
-        mUndoBar.setVisibility(show ? GLView.VISIBLE : GLView.INVISIBLE);
+    ////////////////////////////////////////////////////////////////////////////
+    //  Undo Bar
+    ////////////////////////////////////////////////////////////////////////////
+
+    private int mUndoBarState;
+    private static final int UNDO_BAR_SHOW = 1;
+    private static final int UNDO_BAR_TIMEOUT = 2;
+    private static final int UNDO_BAR_TOUCHED = 4;
+
+    public void showUndoBar() {
+        mHandler.removeMessages(MSG_HIDE_UNDO_BAR);
+        mUndoBarState = UNDO_BAR_SHOW;
+        mUndoBar.animateVisibility(GLView.VISIBLE);
+        mHandler.sendEmptyMessageDelayed(MSG_HIDE_UNDO_BAR, 3000);
+    }
+
+    public void hideUndoBar() {
+        mHandler.removeMessages(MSG_HIDE_UNDO_BAR);
+        mListener.onCommitDeleteImage();
+        mUndoBar.animateVisibility(GLView.INVISIBLE);
+        mUndoBarState = 0;
+        mUndoIndexHint = Integer.MAX_VALUE;
+    }
+
+    // Check if the all conditions for hiding the undo bar have been met. The
+    // conditions are: it has been three seconds since last showing, and the
+    // user has touched.
+    private void checkHideUndoBar(int addition) {
+        mUndoBarState |= addition;
+        if (mUndoBarState ==
+                (UNDO_BAR_SHOW | UNDO_BAR_TIMEOUT | UNDO_BAR_TOUCHED)) {
+            hideUndoBar();
+        }
     }
 
     ////////////////////////////////////////////////////////////////////////////
index 9ddd1d7..f5ea5d1 100644 (file)
@@ -20,6 +20,7 @@ import android.content.Context;
 import android.view.MotionEvent;
 
 import com.android.gallery3d.R;
+import com.android.gallery3d.common.Utils;
 import com.android.gallery3d.util.GalleryUtils;
 
 public class UndoBarView extends GLView {
@@ -89,6 +90,11 @@ public class UndoBarView extends GLView {
     @Override
     protected void render(GLCanvas canvas) {
         super.render(canvas);
+        advanceAnimation();
+
+        canvas.save(GLCanvas.SAVE_FLAG_ALPHA);
+        canvas.multiplyAlpha(mAlpha);
+
         int w = getWidth();
         int h = getHeight();
         mPanel.draw(canvas, mBarMargin, 0, w - mBarMargin * 2, mBarHeight);
@@ -112,6 +118,8 @@ public class UndoBarView extends GLView {
         x = mBarMargin + mDeletedTextMargin;
         y = (mBarHeight - mDeletedText.getHeight()) / 2;
         mDeletedText.draw(canvas, x, y);
+
+        canvas.restore();
     }
 
     @Override
@@ -143,4 +151,55 @@ public class UndoBarView extends GLView {
         int h = getHeight();
         return (x >= w - mClickRegion && x < w && y >= 0 && y < h);
     }
+
+    ////////////////////////////////////////////////////////////////////////////
+    //  Alpha Animation
+    ////////////////////////////////////////////////////////////////////////////
+
+    private static final long NO_ANIMATION = -1;
+    private static long ANIM_TIME = 200;
+    private long mAnimationStartTime = NO_ANIMATION;
+    private float mFromAlpha, mToAlpha;
+    private float mAlpha;
+
+    private static float getTargetAlpha(int visibility) {
+        return (visibility == VISIBLE) ? 1f : 0f;
+    }
+
+    public void setVisibility(int visibility) {
+        mAlpha = getTargetAlpha(visibility);
+        mAnimationStartTime = NO_ANIMATION;
+        super.setVisibility(visibility);
+        invalidate();
+    }
+
+    public void animateVisibility(int visibility) {
+        float target = getTargetAlpha(visibility);
+        if (mAnimationStartTime == NO_ANIMATION && mAlpha == target) return;
+        if (mAnimationStartTime != NO_ANIMATION && mToAlpha == target) return;
+
+        mFromAlpha = mAlpha;
+        mToAlpha = target;
+        mAnimationStartTime = AnimationTime.startTime();
+
+        super.setVisibility(VISIBLE);
+        invalidate();
+    }
+
+    private void advanceAnimation() {
+        if (mAnimationStartTime == NO_ANIMATION) return;
+
+        float delta = (float) (AnimationTime.get() - mAnimationStartTime) /
+                ANIM_TIME;
+        mAlpha = mFromAlpha + ((mToAlpha > mFromAlpha) ? delta : -delta);
+        mAlpha = Utils.clamp(mAlpha, 0f, 1f);
+
+        if (mAlpha == mToAlpha) {
+            mAnimationStartTime = NO_ANIMATION;
+            if (mAlpha == 0) {
+                super.setVisibility(INVISIBLE);
+            }
+        }
+        invalidate();
+    }
 }