OSDN Git Service

Fix swiping in categories in landscape mode
authornicolasroard <nicolasroard@google.com>
Wed, 14 Aug 2013 18:14:47 +0000 (11:14 -0700)
committernicolasroard <nicolasroard@google.com>
Wed, 14 Aug 2013 18:14:47 +0000 (11:14 -0700)
Change-Id: I58ecb212792e3e8432791ee29f8a0ef26a819362

src/com/android/gallery3d/filtershow/FilterShowActivity.java
src/com/android/gallery3d/filtershow/category/CategoryView.java

index 28fd809..43f1325 100644 (file)
@@ -63,6 +63,7 @@ import com.android.gallery3d.data.LocalAlbum;
 import com.android.gallery3d.filtershow.cache.ImageLoader;
 import com.android.gallery3d.filtershow.category.Action;
 import com.android.gallery3d.filtershow.category.CategoryAdapter;
+import com.android.gallery3d.filtershow.category.CategoryView;
 import com.android.gallery3d.filtershow.category.MainPanel;
 import com.android.gallery3d.filtershow.category.SwipableView;
 import com.android.gallery3d.filtershow.data.UserPresetsManager;
@@ -168,6 +169,7 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL
     private boolean mHandlingSwipeButton = false;
     private View mHandledSwipeView = null;
     private float mHandledSwipeViewLastDelta = 0;
+    private float mSwipeStartX = 0;
     private float mSwipeStartY = 0;
 
     private ProcessingService mBoundService;
@@ -1279,7 +1281,7 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL
         return mSelectedImageUri;
     }
 
-    public void setHandlesSwipeForView(View view, float startY) {
+    public void setHandlesSwipeForView(View view, float startX, float startY) {
         if (view != null) {
             mHandlingSwipeButton = true;
         } else {
@@ -1288,25 +1290,42 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL
         mHandledSwipeView = view;
         int[] location = new int[2];
         view.getLocationInWindow(location);
+        mSwipeStartX = location[0] + startX;
         mSwipeStartY = location[1] + startY;
     }
 
     public boolean dispatchTouchEvent (MotionEvent ev) {
         if (mHandlingSwipeButton) {
+            int direction = CategoryView.HORIZONTAL;
+            if (mHandledSwipeView instanceof CategoryView) {
+                direction = ((CategoryView) mHandledSwipeView).getOrientation();
+            }
             if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
                 float delta = ev.getY() - mSwipeStartY;
-                mHandledSwipeView.setTranslationY(delta);
+                float distance = mHandledSwipeView.getHeight();
+                if (direction == CategoryView.VERTICAL) {
+                    delta = ev.getX() - mSwipeStartX;
+                    mHandledSwipeView.setTranslationX(delta);
+                    distance = mHandledSwipeView.getWidth();
+                } else {
+                    mHandledSwipeView.setTranslationY(delta);
+                }
                 delta = Math.abs(delta);
-                float transparency = Math.min(1, delta / mHandledSwipeView.getHeight());
+                float transparency = Math.min(1, delta / distance);
                 mHandledSwipeView.setAlpha(1.f - transparency);
                 mHandledSwipeViewLastDelta = delta;
             }
             if (ev.getActionMasked() == MotionEvent.ACTION_CANCEL
                     || ev.getActionMasked() == MotionEvent.ACTION_UP) {
+                mHandledSwipeView.setTranslationX(0);
                 mHandledSwipeView.setTranslationY(0);
                 mHandledSwipeView.setAlpha(1.f);
                 mHandlingSwipeButton = false;
-                if (mHandledSwipeViewLastDelta > mHandledSwipeView.getHeight()) {
+                float distance = mHandledSwipeView.getHeight();
+                if (direction == CategoryView.VERTICAL) {
+                    distance = mHandledSwipeView.getWidth();
+                }
+                if (mHandledSwipeViewLastDelta > distance) {
                     ((SwipableView) mHandledSwipeView).delete();
                 }
             }
index 7e7fc17..1addbde 100644 (file)
@@ -43,6 +43,7 @@ public class CategoryView extends IconView
     private int mSelectionStroke;
     private Paint mBorderPaint;
     private int mBorderStroke;
+    private float mStartTouchX = 0;
     private float mStartTouchY = 0;
     private float mDeleteSlope = 20;
     private int mSelectionColor = Color.WHITE;
@@ -147,15 +148,20 @@ public class CategoryView extends IconView
         }
         if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
             mStartTouchY = event.getY();
+            mStartTouchX = event.getX();
         }
         if (event.getActionMasked() == MotionEvent.ACTION_UP) {
+            setTranslationX(0);
             setTranslationY(0);
         }
         if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
             float delta = event.getY() - mStartTouchY;
+            if (getOrientation() == CategoryView.VERTICAL) {
+                delta = event.getX() - mStartTouchX;
+            }
             if (Math.abs(delta) > mDeleteSlope) {
                 FilterShowActivity activity = (FilterShowActivity) getContext();
-                activity.setHandlesSwipeForView(this, mStartTouchY);
+                activity.setHandlesSwipeForView(this, mStartTouchX, mStartTouchY);
             }
         }
         return true;