首先看一下layout布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ViewFlipper
android:id="@+id/viewflepper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:flipInterval="3000"
android:persistentDrawingCache="animation"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/iv1"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="match_parent"
android:src="@drawable/a"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/iv2"
android:layout_width="300dp"
android:layout_height="200dp"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
</ViewFlipper>
</RelativeLayout>
//anim 文件夹下的 动作文件
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="240%p"
android:fromYDelta="0%p"
android:toXDelta="0%p"
android:toYDelta="0%p"
android:duration="2000"
>
</translate>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-240%p"
android:fromYDelta="0%p"
android:toXDelta="0%p"
android:toYDelta="0%p"
android:duration="2000"
>
</translate>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0%p"
android:fromYDelta="0%p"
android:toXDelta="-240%p"
android:toYDelta="0%p"
android:duration="2000"
>
</translate>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0%p"
android:fromYDelta="0%p"
android:toXDelta="-240%p"
android:toYDelta="0%p"
android:duration="2000"
>
</translate>
//主要操作手势代码
package com.example.class2_viewflipper;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
private GestureDetector mGd;
private ViewFlipper mVf;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVf=(ViewFlipper) findViewById(R.id.viewflepper);
mGd=new GestureDetector(new OnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
/**
*滑动
* e1 :起点
* e2 :终点
*/
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float arg2,
float arg3) {
if(e1.getX()- e2.getX()>120){
mVf.setInAnimation(MainActivity.this, R.anim.from_left_in);
mVf.setOutAnimation(MainActivity.this, R.anim.go_right_out);
mVf.showPrevious();
}
if(e2.getX()- e1.getX()>120){
mVf.setInAnimation(MainActivity.this, R.anim.from_right_in);
mVf.setOutAnimation(MainActivity.this, R.anim.go_left_out);
mVf.showNext();
}
return false;
}
@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
});
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
mVf.onTouchEvent(ev);
mGd.onTouchEvent(ev);
return super.dispatchTouchEvent(ev);
}
}