OSDN Git Service

am 89127bbe: am 41185c4f: Fix downsampling check in crop.
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / ui / UndoBarView.java
index 9ddd1d7..42f12ae 100644 (file)
@@ -20,9 +20,15 @@ import android.content.Context;
 import android.view.MotionEvent;
 
 import com.android.gallery3d.R;
+import com.android.gallery3d.common.Utils;
+import com.android.gallery3d.glrenderer.GLCanvas;
+import com.android.gallery3d.glrenderer.NinePatchTexture;
+import com.android.gallery3d.glrenderer.ResourceTexture;
+import com.android.gallery3d.glrenderer.StringTexture;
 import com.android.gallery3d.util.GalleryUtils;
 
 public class UndoBarView extends GLView {
+    @SuppressWarnings("unused")
     private static final String TAG = "UndoBarView";
 
     private static final int WHITE = 0xFFFFFFFF;
@@ -54,16 +60,16 @@ public class UndoBarView extends GLView {
     //    +-+----+----------------+-+--+----+-+------+--+-+
     //     4  16                   1 12  32  8        16 4
     public UndoBarView(Context context) {
-        mBarHeight = (int) GalleryUtils.dpToPixel(48);
-        mBarMargin = (int) GalleryUtils.dpToPixel(4);
-        mUndoTextMargin = (int) GalleryUtils.dpToPixel(16);
-        mIconMargin = (int) GalleryUtils.dpToPixel(8);
-        mIconSize = (int) GalleryUtils.dpToPixel(32);
-        mSeparatorRightMargin = (int) GalleryUtils.dpToPixel(12);
-        mSeparatorTopMargin = (int) GalleryUtils.dpToPixel(10);
-        mSeparatorBottomMargin = (int) GalleryUtils.dpToPixel(10);
-        mSeparatorWidth = (int) GalleryUtils.dpToPixel(1);
-        mDeletedTextMargin = (int) GalleryUtils.dpToPixel(16);
+        mBarHeight = GalleryUtils.dpToPixel(48);
+        mBarMargin = GalleryUtils.dpToPixel(4);
+        mUndoTextMargin = GalleryUtils.dpToPixel(16);
+        mIconMargin = GalleryUtils.dpToPixel(8);
+        mIconSize = GalleryUtils.dpToPixel(32);
+        mSeparatorRightMargin = GalleryUtils.dpToPixel(12);
+        mSeparatorTopMargin = GalleryUtils.dpToPixel(10);
+        mSeparatorBottomMargin = GalleryUtils.dpToPixel(10);
+        mSeparatorWidth = GalleryUtils.dpToPixel(1);
+        mDeletedTextMargin = GalleryUtils.dpToPixel(16);
 
         mPanel = new NinePatchTexture(context, R.drawable.panel_undo_holo);
         mUndoText = StringTexture.newInstance(context.getString(R.string.undo),
@@ -89,6 +95,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 +123,8 @@ public class UndoBarView extends GLView {
         x = mBarMargin + mDeletedTextMargin;
         y = (mBarHeight - mDeletedText.getHeight()) / 2;
         mDeletedText.draw(canvas, x, y);
+
+        canvas.restore();
     }
 
     @Override
@@ -143,4 +156,56 @@ 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;
+    }
+
+    @Override
+    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();
+    }
 }