public class SampleGuest implements OnGestureListener {
SlideExample se;
private static final int SWIPE_MAX_OFF_PATH = 100;
private static final int SWIPE_MIN_DISTANCE = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
public SampleGuest(SlideExample se) {
this.se = se;
}
// 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发
public boolean onDown(MotionEvent e) {
Log.d("TAG", "[onDown]");
return false;
}
// 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN,
// 多个ACTION_MOVE, 1个ACTION_UP触发
// e1:第1个ACTION_DOWN MotionEvent
// e2:最后一个ACTION_MOVE MotionEvent
// velocityX:X轴上的移动速度,像素/秒
// velocityY:Y轴上的移动速度,像素/秒
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH){
return false;
}
if ((e1.getRawX() - e2.getRawX()) > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if (se.container1.getVisibility() == View.VISIBLE) {
se.container1.setAnimation(AnimationUtils.loadAnimation(se,
R.anim.push_left_out));
se.container2.setVisibility(View.VISIBLE);
se.container2.setAnimation(AnimationUtils.loadAnimation(se,
R.anim.push_right_in));
se.container1.setVisibility(View.GONE);
return true;
} else {
se.container3.setVisibility(View.VISIBLE);
se.container2.setAnimation(AnimationUtils.loadAnimation(se,
R.anim.push_left_out));
se.container3.setAnimation(AnimationUtils.loadAnimation(se,
R.anim.push_right_in));
se.container2.setVisibility(View.GONE);
return true;
}
} else if ((e2.getRawX() - e1.getRawX()) > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if(se.container3.getVisibility() == View.VISIBLE){
se.container2.setVisibility(View.VISIBLE);
se.container3.setAnimation(AnimationUtils.loadAnimation(se,
R.anim.push_right_out));
se.container2.setAnimation(AnimationUtils.loadAnimation(se,
R.anim.push_left_in));
se.container3.setVisibility(View.GONE);
return true;
}
else{
se.container1.setVisibility(View.VISIBLE);
se.container1.setAnimation(AnimationUtils.loadAnimation(se,
R.anim.push_left_in));
se.container2.setAnimation(AnimationUtils.loadAnimation(se,
R.anim.push_right_out));
se.container2.setVisibility(View.GONE);
return true;
}
}
else{
return false;
}
}
// 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发
public void onLongPress(MotionEvent e) {
Log.d("TAG", "[onLongPress]");
}
// 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.d("TAG", "[onScroll]");
return true;
}
// 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发
// 注意和onDown()的区别,强调的是没有松开或者拖动的状态
public void onShowPress(MotionEvent e) {
Log.d("TAG", "[onShowPress]");
}
// 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发
public boolean onSingleTapUp(MotionEvent e) {
Log.d("TAG", "[onSingleTapUp]");
return true;
}
}
//SlideExample类
public class SlideExample extends Activity {
/** Called when the activity is first created. */
public ViewGroup container1, container2, container3;
public WebView view,view1,view2;
private GestureDetector gestureDetector;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 监听屏幕动作事件
SampleGuest gestureListener = new SampleGuest(this);
gestureDetector = new GestureDetector(gestureListener);
container1 = (ViewGroup) findViewById(R.id.container1);
container2 = (ViewGroup) findViewById(R.id.container2);
container3 = (ViewGroup) findViewById(R.id.container3);
view = (WebView) findViewById(R.id.webview);
view.loadUrl("http://www.google.com");
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new WebViewClient() {
@Override
// 在WebView中而不是默认浏览器中显示页面
public boolean shouldOverrideUrlLoading(WebView view,
String url) {
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
view.requestFocus();//获取焦点即可
}
});
view.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}
});
view1 = (WebView) findViewById(R.id.webview1);
view1.loadUrl("http://www.baidu.com");
view1.getSettings().setJavaScriptEnabled(true);
view1.setWebViewClient(new WebViewClient() {
@Override
// 在WebView中而不是默认浏览器中显示页面
public boolean shouldOverrideUrlLoading(WebView view,
String url) {
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
view1.requestFocus();//获取焦点即可
}
});
view1.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}
});
view2 = (WebView) findViewById(R.id.webview2);
view2.loadUrl("http://www.hao123.com");
view2.getSettings().setJavaScriptEnabled(true);
view2.setWebViewClient(new WebViewClient() {
@Override
// 在WebView中而不是默认浏览器中显示页面
public boolean shouldOverrideUrlLoading(WebView view,
String url) {
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
view2.requestFocus();//获取焦点即可
}
});
view2.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}
});
}
// called automatically, any screen action will Triggered it
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}
}
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/container">
<RelativeLayout android:layout_width="fill_parent"
android:id="@+id/container1" android:layout_height="fill_parent"
android:visibility="visible" android:background="#ffffff">
<ScrollView android:id="@+id/scrollview1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</ScrollView>
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:background="#ffffff" android:id="@+id/container2"
android:visibility="gone" android:layout_height="fill_parent">
<ScrollView android:id="@+id/scrollview2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView android:id="@+id/webview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</ScrollView>
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:background="#ffffff" android:id="@+id/container3"
android:visibility="gone" android:layout_height="fill_parent">
<ScrollView android:id="@+id/scrollview3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView android:id="@+id/webview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</ScrollView>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zoneking.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SlideExample"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
//push_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="1000"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="100" />
</set>
//push_left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="1000" />
<alpha android:fromAlpha="1.0" android:toAlpha="1.0"
android:duration="400" />
</set>
//push_right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="1000"/>
<alpha android:fromAlpha="1.0" android:toAlpha="1.0" android:duration="400" />
</set>
//push_right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="1000"/>
<alpha android:fromAlpha="1.0" android:toAlpha="1.0" android:duration="400" />
</set>