ViewFlipper 和 GestureDetector实现:
手势滑屏,当向左滑动的时候,平行左移动换下一屏。向右滑动的时候就向右移动换屏。
主要用到ViewFlipper和GestureDetector.
ViewFlipper变化当前显示内容,GestureDetector监听手势
实现代码如下:
========================================================================
src/com.page/Pages.java
-----------------------------------------------------
package litsoft.orderdishes;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class Pages extends Activity implements OnGestureListener{
private ViewFlipper flipper;//ViewFlipper实例
private GestureDetector detector;//触摸监听实例
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pages);
flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);//获得ViewFlipper实例
detector = new GestureDetector(this);
flipper.addView(addTextView("step 1",Color.GREEN));//将View添加到flipper队列中
flipper.addView(addTextView("step 2",Color.BLUE));
flipper.addView(addTextView("step 3",Color.YELLOW));
flipper.addView(addTextView("step 4",Color.RED));
flipper.addView(addTextView("step 5",Color.GRAY));
}
public View addTextView(String text,int color) {
TextView tv = new TextView(this);
tv.setText(text);
tv.setTextColor(Color.BLACK);
tv.setBackgroundColor(color);
tv.setTextSize(50);
tv.setGravity(1);
return (View)tv;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
// return super.onTouchEvent(event);
return this.detector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() - e2.getX() > 120) {//如果是从右向左滑动
//注册flipper的进出效果
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));
this.flipper.showNext();
return true;
} else if (e1.getX() - e2.getX() < -120) {//如果是从左向右滑动
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.right_out));
this.flipper.showPrevious();
return true;
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}
------------------------------------------------------------------------------
------------------------------------------------------------------------------
res/layout/pages.xml
-----------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FF93E3B0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ViewFlipper
android:id="@+id/ViewFlipper01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ViewFlipper>
</LinearLayout>
------------------------------------------------------------------------------
------------------------------------------------------------------------------
res/anim/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="500" />
</set>
------------------------------------------------------------------------------
------------------------------------------------------------------------------
res/anim/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="500" />
</set>
------------------------------------------------------------------------------
------------------------------------------------------------------------------
res/anim/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="500" />
</set>
------------------------------------------------------------------------------
------------------------------------------------------------------------------
res/anim/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="500" />
</set>
------------------------------------------------------------------------------
------------------------------------------------------------------------------
手势滑屏,当向左滑动的时候,平行左移动换下一屏。向右滑动的时候就向右移动换屏。
主要用到ViewFlipper和GestureDetector.
ViewFlipper变化当前显示内容,GestureDetector监听手势
实现代码如下:
========================================================================
src/com.page/Pages.java
-----------------------------------------------------
package litsoft.orderdishes;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class Pages extends Activity implements OnGestureListener{
private ViewFlipper flipper;//ViewFlipper实例
private GestureDetector detector;//触摸监听实例
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pages);
flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);//获得ViewFlipper实例
detector = new GestureDetector(this);
flipper.addView(addTextView("step 1",Color.GREEN));//将View添加到flipper队列中
flipper.addView(addTextView("step 2",Color.BLUE));
flipper.addView(addTextView("step 3",Color.YELLOW));
flipper.addView(addTextView("step 4",Color.RED));
flipper.addView(addTextView("step 5",Color.GRAY));
}
public View addTextView(String text,int color) {
TextView tv = new TextView(this);
tv.setText(text);
tv.setTextColor(Color.BLACK);
tv.setBackgroundColor(color);
tv.setTextSize(50);
tv.setGravity(1);
return (View)tv;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
// return super.onTouchEvent(event);
return this.detector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() - e2.getX() > 120) {//如果是从右向左滑动
//注册flipper的进出效果
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));
this.flipper.showNext();
return true;
} else if (e1.getX() - e2.getX() < -120) {//如果是从左向右滑动
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.right_out));
this.flipper.showPrevious();
return true;
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}
------------------------------------------------------------------------------
------------------------------------------------------------------------------
res/layout/pages.xml
-----------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FF93E3B0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ViewFlipper
android:id="@+id/ViewFlipper01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ViewFlipper>
</LinearLayout>
------------------------------------------------------------------------------
------------------------------------------------------------------------------
res/anim/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="500" />
</set>
------------------------------------------------------------------------------
------------------------------------------------------------------------------
res/anim/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="500" />
</set>
------------------------------------------------------------------------------
------------------------------------------------------------------------------
res/anim/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="500" />
</set>
------------------------------------------------------------------------------
------------------------------------------------------------------------------
res/anim/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="500" />
</set>
------------------------------------------------------------------------------
------------------------------------------------------------------------------