ViewPlipper可以用作幻灯片的播放或者是欢迎页面的切换等,当前ViewPager也可以,先来介绍ViewPlipper的循环播放效果:
1.ViewPlipper循环播放:
布局:(没咋可说的)
view_flipper_test.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">
<ViewFlipper
android:id="@+id/viewFlipper"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ViewFlipper>
</LinearLayout>
主要代码:对于ViewFlipper这里我们使用代码动态添加View:
private ViewFlipper mViewFlipper;
private int[] mIds = {R.mipmap.guid_01,R.mipmap.guid_02,R.mipmap.guid_03,R.mipmap.guid_04};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_flipper_test);
mViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
for (int i = 0 ; i < mIds.length; i ++) {
mViewFlipper.addView(getImageView(i));
}
// 设置动画
mViewFlipper.setInAnimation(this,R.anim.slide_in_rigth); mViewFlipper.setOutAnimation(this,R.anim.slide_out_left);
// 切换的时间间隔
mViewFlipper.setFlipInterval(2000);
//开始切换
mViewFlipper.startFlipping();
}
这样就可播放了。
2.切换页面使用
布局还是上面的布局,下面看主要的实现代码:
ViewFlipperActivity.java
public class ViewFlipperActivity extends Activity implements GestureDetector.OnGestureListener {
private ViewFlipper mViewFlipper;
private int[] mIds = {R.mipmap.guid_01,R.mipmap.guid_02,R.mipmap.guid_03,R.mipmap.guid_04};
private int currentPosition = 0;
private GestureDetector detector; //手势检测
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_flipper_test);
detector = new GestureDetector(this,this);
mViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
for (int i = 0 ; i < mIds.length; i ++) {
mViewFlipper.addView(getImageView(i));
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return detector.onTouchEvent(event);//touch事件交给手势处理。
}
private ImageView getImageView(int resourceId) {
ImageView imageView = new ImageView(this);
imageView.setBackgroundResource(mIds[resourceId]);
return imageView;
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if(e1.getX()-e2.getX()>120){
if (currentPosition >= mIds.length-1)
return false;
mViewFlipper.setInAnimation(this,R.anim.slide_in_rigth);
mViewFlipper.setOutAnimation(this,R.anim.slide_out_left);
mViewFlipper.showNext(); //向左滑动
currentPosition ++;
return true;
}else if(e1.getX()-e2.getY()<-120){
if (currentPosition <= 0)
return false;
mViewFlipper.setInAnimation(this,R.anim.slide_in_left);
mViewFlipper.setOutAnimation(this,R.anim.slide_out_rigth);
mViewFlipper.showPrevious();//向右滑动
currentPosition --;
return true;
}
return false;
}
}
在这里我们使用了android缝扎un个好了的手势识别工具类,当然也可以自己在onTouchEvent方法自己写也可以(既然有工具类,为什么不用0.0)。
当然对于这里还有四个动画:
slide_in_left.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="400"
/>
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="400"
/>
</set>
slide_out_left.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="400"
/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="400"
/>
</set>
slide_in_rigth.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="400"
/>
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="400"
/>
</set>
slide_out_rigth.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%"
android:duration="400"
/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="400"
/>
</set>
上边这四个动画,主要是平移与透明动画,效果图就不上了,跟市面上主流的欢迎页面切换一样。