diff options
author | Andy Doan <andy.doan@linaro.org> | 2011-10-14 18:20:06 -0500 |
---|---|---|
committer | Andy Doan <doanac@gmail.com> | 2011-10-14 18:20:06 -0500 |
commit | 9c08bb63e27284963e4c0b7c7cef7c722c0bba2f (patch) | |
tree | 5660aa68afaa41705e25bba942e5fbd9660e8503 | |
parent | f94a4bb45cd0ee909ae38c02fd9a60b14da30ed6 (diff) | |
download | LinaroConnect-9c08bb63e27284963e4c0b7c7cef7c722c0bba2f.tar.gz |
support for flinging in images
this is a bad attempt at getting the image viewer activity
to support swiping back/forward with your finger
-rw-r--r-- | src/org/linaro/connect/FlingListener.java | 89 | ||||
-rw-r--r-- | src/org/linaro/connect/ImageViewActivity.java | 35 |
2 files changed, 120 insertions, 4 deletions
diff --git a/src/org/linaro/connect/FlingListener.java b/src/org/linaro/connect/FlingListener.java new file mode 100644 index 0000000..515060f --- /dev/null +++ b/src/org/linaro/connect/FlingListener.java @@ -0,0 +1,89 @@ +package org.linaro.connect; + +import android.view.GestureDetector; +import android.view.GestureDetector.OnDoubleTapListener; +import android.view.GestureDetector.OnGestureListener; +import android.view.MotionEvent; +import android.view.View; +import android.view.View.OnTouchListener; + +public class FlingListener implements OnGestureListener, OnDoubleTapListener { + + private final FlingEvent mEvent; + private boolean mEnableForward = true; + private boolean mEnableBack = true; + + public FlingListener(View v, FlingEvent fe) { + mEvent = fe; + + final GestureDetector gd = new GestureDetector(this); + v.setOnTouchListener(new OnTouchListener() { + @Override + public boolean onTouch(View v, MotionEvent me) { + return gd.onTouchEvent(me); + } + }); + } + + public void enableForward(boolean enabled) { + mEnableForward = enabled; + } + + public void enableBack(boolean enabled) { + mEnableBack = enabled; + } + + @Override + public boolean onDown(MotionEvent e) { + return true; + } + + @Override + public boolean onFling(MotionEvent e1, MotionEvent e2, + final float velocityX, final float velocityY) { + if(mEnableForward && velocityX < 0) + mEvent.onFlingForward(); + else if( mEnableBack && velocityX > 0 ) + mEvent.onFlingBack(); + return true; + } + + @Override + public boolean onDoubleTap(MotionEvent e) { + return true; + } + + @Override + public void onLongPress(MotionEvent e) { + } + + @Override + public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, + float distanceY) { + return true; + } + + @Override + public void onShowPress(MotionEvent e) { + } + + @Override + public boolean onSingleTapUp(MotionEvent e) { + return false; + } + + @Override + public boolean onDoubleTapEvent(MotionEvent e) { + return false; + } + + @Override + public boolean onSingleTapConfirmed(MotionEvent e) { + return false; + } + + public interface FlingEvent { + void onFlingForward(); + void onFlingBack(); + } +} diff --git a/src/org/linaro/connect/ImageViewActivity.java b/src/org/linaro/connect/ImageViewActivity.java index f6b3721..94411eb 100644 --- a/src/org/linaro/connect/ImageViewActivity.java +++ b/src/org/linaro/connect/ImageViewActivity.java @@ -3,6 +3,7 @@ package org.linaro.connect; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.linaro.connect.FlingListener.FlingEvent; import android.app.Activity; import android.content.Intent; @@ -25,6 +26,8 @@ public class ImageViewActivity extends Activity { private ImageButton mBack; private ImageButton mForward; + private FlingListener mFlingListener; + @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); @@ -51,6 +54,8 @@ public class ImageViewActivity extends Activity { } }); + setFlingListener(findViewById(R.id.image_view_id)); + Intent i = getIntent(); String json = i.getStringExtra("json"); JSONObject jso = JSONUtils.toJSON(json); @@ -64,6 +69,20 @@ public class ImageViewActivity extends Activity { } } + private void setFlingListener(View v) { + mFlingListener = new FlingListener(v, new FlingEvent() { + @Override + public void onFlingForward() { + setSelected(mSelected+1); + } + + @Override + public void onFlingBack() { + setSelected(mSelected-1); + } + }); + } + private void setSelected(int pos) { disableNavigation(); @@ -103,6 +122,8 @@ public class ImageViewActivity extends Activity { mForward.setEnabled(false); mBack.setVisibility(View.INVISIBLE); mForward.setVisibility(View.INVISIBLE); + mFlingListener.enableForward(false); + mFlingListener.enableBack(false); } private void enableNavigation() { @@ -110,15 +131,21 @@ public class ImageViewActivity extends Activity { mBack.setEnabled(true); mForward.setEnabled(true); - if( mSelected < 1 ) + if( mSelected < 1 ) { mBack.setVisibility(View.INVISIBLE); - else + mFlingListener.enableBack(false); + } else { mBack.setVisibility(View.VISIBLE); + mFlingListener.enableBack(true); + } - if( mSelected >= getNumImages()-1 ) + if( mSelected >= getNumImages()-1 ) { mForward.setVisibility(View.INVISIBLE); - else + mFlingListener.enableForward(false); + } else { mForward.setVisibility(View.VISIBLE); + mFlingListener.enableForward(true); + } } private class DownloadTask extends AsyncTask<JSONImageItem, Void, Drawable> { |