1. main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rel1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ViewFlipper
android:id="@+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/view1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF68F" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="View 1"
android:textColor="#000000"
android:textSize="20pt" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/view2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E0FFFF" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="View 2"
android:textColor="#000000"
android:textSize="20pt" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/view3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF0F5" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="View 3"
android:textColor="#000000"
android:textSize="20pt" />
</RelativeLayout>
</ViewFlipper>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/flipper" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="next" >
</Button>
<Button
android:id="@+id/btnPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="previous" >
</Button>
</RelativeLayout>
</FrameLayout>
</RelativeLayout>
2. GAD01.java
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class GAD01 extends Activity {
private Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
private Animation slideRightOut;
private ViewFlipper viewFlipper;
private float preX;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
// when load, need to change to view 2 by default
viewFlipper.showNext();
// load animations
slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
slideLeftOut = AnimationUtils
.loadAnimation(this, R.anim.slide_left_out);
slideRightIn = AnimationUtils
.loadAnimation(this, R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(this,
R.anim.slide_right_out);
initialButtons();
}
void initialButtons() {
Button btnPrevious = (Button) findViewById(R.id.btnPrevious);
Button btnNext = (Button) findViewById(R.id.btnNext);
btnPrevious.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// set animations
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideRightOut);
// when move to left edge, then can't move further
if (!isLeftEdge()) {
// move to previous page
viewFlipper.showPrevious();
}
// show current page toast
currentPageToast(viewFlipper.getDisplayedChild(),
viewFlipper.getChildCount());
}
});
btnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// set animations
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
// when move to right edge, then can't move further
if (!isRightEdge()) {
// move to next page
viewFlipper.showNext();
}
// show current page toast
currentPageToast(viewFlipper.getDisplayedChild(),
viewFlipper.getChildCount());
}
});
}
private void currentPageToast(int id, int totalpage) {
id++;
String rtn = "";
for (int i = 1; i <= totalpage; i++) {
if (id == i) {
rtn += "o";
} else {
rtn += ".";
}
}
// make a string represent current page
Toast.makeText(this, rtn, Toast.LENGTH_SHORT).show();
}
private boolean isRightEdge() {
if (viewFlipper.getDisplayedChild() == viewFlipper.getChildCount() - 1) {
return true;
} else {
return false;
}
}
private boolean isLeftEdge() {
if (viewFlipper.getDisplayedChild() == 0) {
return true;
} else {
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
preX = event.getX();
}
if (event.getAction() == MotionEvent.ACTION_UP) {
float curX = event.getX();
if (curX < preX) {
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
if (!isRightEdge()) {
viewFlipper.showNext();
}
currentPageToast(viewFlipper.getDisplayedChild(),
viewFlipper.getChildCount());
} else {
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideRightOut);
if (!isLeftEdge()) {
viewFlipper.showPrevious();
}
currentPageToast(viewFlipper.getDisplayedChild(),
viewFlipper.getChildCount());
}
}
return super.onTouchEvent(event);
}
}
沒有留言:
張貼留言