一:循環播放一組圖檔。
animation_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="2000" />
<alpha android:fromAlpha="0"
android:toAlpha="1"
android:duration="2000"/>
</set>
animation_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="2000" />
<alpha android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="2000"/>
</set>
java代碼:
private void showViewFlipper() {
// TODO Auto-generated method stub
viewFlipper = new ViewFlipper(this);
viewFlipper.addView(getImageViewById(R.drawable.froad_icon));
viewFlipper.addView(getImageViewById(R.drawable.froad_icon));
viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.animation_out));
viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.animation_in));
viewFlipper.setFlipInterval(5000);
viewFlipper.startFlipping();
}
ImageView getImageViewById(int id){
ImageView imageView = new ImageView(this);
Drawable draw = this.getResources().getDrawable(id);
imageView.setBackgroundDrawable(draw);
return imageView;
};
在onCreat() 裡面調用ok了
showViewFlipper();
setContentView(viewFlipper);
這段就是利用ViewFlipper來自動播放froad_icon圖檔。
二:在viewflipper上加入手勢操作。
需要一個類:private GestureDetector mGestureDetector 和 實作兩個接口 implements OnGestureListener ,OnDoubleTapListener
以下是兩個需要添加和自己寫的方法。
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
if(e1 !=null && e2 !=null){
if(e1.getX() - e2.getX()> FLING_MIN_DISTANCE &&
Math.abs(velocityX)> 150){
viewFlipper.setOutAnimation(this,R.anim.animation_out);
viewFlipper.setInAnimation(this, R.anim.animation_in);
viewFlipper.showNext();
int indext = viewFlipper.getDisplayedChild();
}
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return this.mGestureDetector.onTouchEvent(event);
}
在oncreate()裡面:
showViewFlipper();
setContentView(viewFlipper);
mGestureDetector = new GestureDetector(this);
在onFling方法中隻寫了一個向左滑動的代碼,還可以添加其他滑動。