4个layout,2个anim,1个.java文件
其中3个layout随便写的比如layout1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="layout1"
android:textSize="80sp"
android:gravity="center"/>
</LinearLayout>
2个anim文件:
slide_in_right.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="50%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
slide_out_left.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-50%p"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
alpha透明度变化
translate平移变化,这里有3个:-50%p,0,50%p,分别代表左中右。
下面是MainActivity.java文件,着重注意获取和处理onTouchEvent分发的事件,除了onFling其他方法这里不用管:
package com.example.viewflipper;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class MainActivity extends Activity implements OnGestureListener {
ViewFlipper flipper;
LayoutInflater inflater;
GestureDetector detector;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater = LayoutInflater.from(this);
flipper = (ViewFlipper) findViewById(R.id.viewflipper);
flipper.addView(findView(R.layout.layout1));
flipper.addView(findView(R.layout.layout2));
flipper.addView(findView(R.layout.layout3));
tv = (TextView) findViewById(R.id.tv);
tv.setText("1");
detector = new GestureDetector(this);// 传入该类的OnGestureListener,MainActivity实现了该监听器
}
private View findView(int id) {
return inflater.inflate(id, null);
}
/**
* 通过该方法使用GestureDetector对象,GestureDetector对象将所获取的事件传递给OnGestureListener的实现方法,比如onFling
* 下面的其他@override方法都是实现的OnGestureListener的方法,我们只需重写onFling就可以了
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return this.detector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
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;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if ((e1.getX() - e2.getX()) > 120) {
//下面的anim文件时根据android.R.anim.slide_out_right比葫芦画的
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.slide_in_right));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.slide_out_left));
this.flipper.showNext();
} else if ((e1.getX() - e2.getX()) < -120) {
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right));
this.flipper.showPrevious();
} else {
return false;
}
int childid = flipper.getDisplayedChild();
tv.setText(childid+1+"");
//下面注释掉的内容显示的是-1,也就是说如果该View没有Id就返回-1
/*switch (v.getId()) {
case R.layout.layout1:
tv.setText("layout1");
break;
case R.layout.layout2:
tv.setText("layout2");
break;
case R.layout.layout3:
tv.setText("layout3");
break;
default:
tv.setText(v.getId()+"");
break;
}*/
return true;
}
}