Hi guy
I am trying write a reusable custom view to move it on screen according to user gesture. How can I do that? I tried to write based on some example, I found that I can only listen only one object.
here is my main activity code.
Please help!
public class GestureFunActivity extends Activity implements OnTouchListener, OnGestureListener {
/** Called when the activity is first created. */
private PlayAreaView view;
private GestureDetector mGestureDetector;
public GestureFunActivity() {
mGestureDetector = new GestureDetector(this);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FrameLayout frame = (FrameLayout) findViewById(R.id.graphics_holder);
PlayAreaView image = new PlayAreaView(this);
image.setOnTouchListener(this);
image.setContentDescription("image");
Log.i("MyGesture", "onCreate" + image.getContentDescription().toString());
frame.addView(image);
}
public boolean onTouch(View v, MotionEvent event) {
view = (PlayAreaView) v;
Log.i("MyGesture", "onTouch [" + v.getContentDescription().toString() + "]");
Toast.makeText(this, "onTouch [" + v.getContentDescription().toString() +"]" , Toast.LENGTH_SHORT).show();
return mGestureDetector.onTouchEvent(event);
}
public boolean onDown(MotionEvent arg0) {
Log.i("MyGesture", "onDown" );
return true;
}
public void onShowPress(MotionEvent e) {
Log.i("MyGesture", "onShowPress");
}
public boolean onSingleTapUp(MotionEvent e) {
Log.i("MyGesture", "onSingleTapUp");
return true;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
final int FLING_MIN_DISTANCE = 10, FLING_MIN_VELOCITY = 20;
if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
// Fling left
Log.i("MyGesture", "Fling left");
} else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
// Fling right
Log.i("MyGesture", "Fling right");
}
final float distanceTimeFactor = 0.4f;
final float totalDx = (distanceTimeFactor * velocityX/2);
final float totalDy = (distanceTimeFactor * velocityY/2);
view.onAnimateMove(totalDx, totalDy,
(long) (10 * distanceTimeFactor));
// return true;
return false;
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.i("MyGesture", "onScroll");
return true;
}
public void onLongPress(MotionEvent e) {
Log.i("MyGesture", "onLongPress");
}
}