用ViewFlipper与GestureDetector来实现图片的左右滑动效果。
首先在布局xml中添加ViewFlipper控件:
<ViewFlipper
android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ViewFlipper>
在Activity中实现GestureDetector的OnGestureListener监听:
mGesture = new GestureDetector(this, this);
public boolean onDown(MotionEvent e) {
return false;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}
public void onLongPress(MotionEvent e) {
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}
public void onShowPress(MotionEvent e) {
}
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
为ViewFilpper加入View,如图片、文字等:
mFilpper = (ViewFlipper) findViewById(R.id.flipper);
mFilpper.addView(addImageView(R.drawable.chrysanthemum));
mFilpper.addView(addImageView(R.drawable.desert));
mFilpper.addView(addImageView(R.drawable.ic_launcher));
mFilpper.addView(addImageView(R.drawable.koala));
mFilpper.addView(addImageView(R.drawable.penguins));
mFilpper.addView(addImageView(R.drawable.lighthouse));
mFilpper.addView(addText("你好"));
public ImageView addImageView(int resId) {
ImageView p_w_picpath = new ImageView(this);
p_w_picpath.setImageResource(resId);
return p_w_picpath;
}
private View addText(String string) {
TextView textView = new TextView(this);
textView.setText(string);
return textView;
}
在onFling()中编写滑动效果:
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.e("onFling", "onFling");
if (e1.getX() - e2.getX() > 120) {
mFilpper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.left_in));
mFilpper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.left_out));
mFilpper.showPrevious();
} else if (e2.getX() - e1.getX() > 120) {
mFilpper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.rigth_in));
mFilpper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.right_out));
mFilpper.showNext();
}
return false;
}
其中的left_in.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1" />
<translate
android:duration="1000"
android:fromXDelta="100%p"
android:toXDelta="0" />
</set>
left_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="1000"
android:fromAlpha="1"
android:toAlpha="0" />
<translate
android:duration="1000"
android:fromXDelta="0"
android:toXDelta="-100%p" />
</set>
right_in.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1" />
<translate
android:duration="1000"
android:fromXDelta="-100%p"
android:toXDelta="0" />
</set>
right_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="1000"
android:fromAlpha="1"
android:toAlpha="0" />
<translate
android:duration="1000"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>
最后还要重写onTouchEvent()方法在其中调用GestureDetector的onTouchEvent()方法:
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.e("onTouchEvent", "onTouchEvent");
return mGesture.onTouchEvent(event);
}
转载于:https://blog.51cto.com/10976921/1723555