Friday, August 12, 2011

Gesture based action

Here we are discussing about how to set that as the gesture listener for the views that I add.
My requirement : I have list of images in a listview with url. When the user wants view the full image then user click the image on the list view
it will take to me the anonther activity and open in a imageview. and as a user wants to tee the next nexe or previous image with come back to listview page


Solution : I have created the HashMap Contains the list of images and pass the selected images name as bundle string. In a detail view created the async task the fetch image from the internet
implement the gestureListener. In this article we discussing about that


Related : gesture move action, gesture based action, example geatures


1. Create a Activity implements OnClickListener
public class MySample1Activity extends Activity implements OnClickListener {
........
}
2. Initialize below required object for gestureListener
ViewConfiguration vc;


static int SWIPE_MAX_OFF_PATH = 250;
static int SWIPE_MIN_DISTANCE = 100;
static int SWIPE_THRESHOLD_VELOCITY = 100;


private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;


3. Set Densitybased velocity
vc = ViewConfiguration.get(this);
SWIPE_MIN_DISTANCE = vc.getScaledTouchSlop();
SWIPE_THRESHOLD_VELOCITY = vc.getScaledMinimumFlingVelocity();


4. Create a Custom GestureListener class which extends SimpleOnGestureListener
class MyGestureDetector extends SimpleOnGestureListener {


}


5. Override the onFling() and implement the geature move action
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(MySample1Activity.this, "Left Swipe",
Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(MySample1Activity.this, "Right Swipe",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}


Full SourceCode
public class MySample1Activity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
TextView tView;


ViewConfiguration vc;


static int SWIPE_MAX_OFF_PATH = 250;
static int SWIPE_MIN_DISTANCE = 100;
static int SWIPE_THRESHOLD_VELOCITY = 100;


private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tView = (TextView) findViewById(R.id.textview1);
vc = ViewConfiguration.get(this);
SWIPE_MIN_DISTANCE = vc.getScaledTouchSlop();
SWIPE_THRESHOLD_VELOCITY = vc.getScaledMinimumFlingVelocity();


gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
tView.setOnClickListener(this);
tView.setOnTouchListener(gestureListener);
}


class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(MySample1Activity.this, "Left Swipe",
Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(MySample1Activity.this, "Right Swipe",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub


}


}


No comments:

Post a Comment