//动画效果
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="500"
/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="500"
/>
</set>
//两个Activity
public class Main extends Activity implements OnTouchListener,OnGestureListener{
private static final float FLING_MIN_DISTANCE = 10;
private static final float FLING_MIN_VELOCITY = 10;
private GestureDetector gd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建OnTouchListener的监听器
gd=new GestureDetector(Main.this);
RelativeLayout r=(RelativeLayout)findViewById(R.id.relative);
r.setOnTouchListener(Main.this);
/*注意:若不加setLongClickable(true)的话OnFling会失效,
* 如果不写这句的话OnGestureListener的重写方法OnDown方法返回true也可以。
* 只有这样,view才///能够处理不同于Tap(轻触)的hold(即ACTION_MOVE,或者多个ACTION_DOWN),
* 我们同样可以通过layout定义中的android:longClickable来做到这一点。 */
// r.setLongClickable(true);
}
//OnTouch重写的方法
//将Acityvity的TouchEvent事件交给GestureDetector处理
@Override
public boolean onTouch(View v, MotionEvent event) {
return gd.onTouchEvent(event);
}
//以下为OnGesture重写的方法
@Override
public boolean onDown(MotionEvent e) {
return true;
//return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
if (e2.getX()-e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
Intent intent = new Intent(Main.this,OtherActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.enter, R.anim.out);
Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();
}else {
//....
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
}
//
public class OtherActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.otheractivity);
}
}