当前位置: 移动技术网 > IT编程>移动开发>Android > Android ViewFlipper翻转视图使用详解

Android ViewFlipper翻转视图使用详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

身骑白马伴奏,青岛西海岸新闻网,河北62岁男子梳清朝辫子

简介

viewflipper是android自带的一个多页面管理控件且可以自动播放!它和viewpager有所不同,viewpager继承自viewgroup,是一页一页的,可以带动画效果,可以兼容低版本;而viewflipper继承viewanimator,是一层一层的,切换view的时候可以设置动画效果,是android 4.0才引入的新控件。使用场景和viewpager基本一样,在很多时候都是用来实现进入应用后的引导页或者用于图片轮播显示。

常用方法

setinanimation:view进入屏幕时使用动画;
setoutanimation:view退出屏幕时使用动画;
shownext:显示viewflipper里的下一个view视图;
showprevious:显示viewflipper里的上一个view视图;
setflipinterval:view之间切换的时间间隔;
setautostart:是否可以自动播放,true为自动播放,false为不自动播放;
startflipping:自动循环切换播放;
stopflipping:停止自动切换播放;

viewflipper加入view的两种方法

1.静态导入

所谓的静态导入就是像以下方式那样,将一个个页面添加到viewflipper的中间!

<viewflipper
 android:id="@+id/vf_help"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:flipinterval="3000"
 android:inanimation="@anim/right_in"
 android:outanimation="@anim/right_out">

 <include layout="@layout/page_help_one" />

 <include layout="@layout/page_help_two" />

 <include layout="@layout/page_help_three" />

 <include layout="@layout/page_help_four" />
</viewflipper>

2.动态导入

所谓的动态导入就是像以下方式那样,通过addview方法填充view!

mvfhelp = (viewflipper) findviewbyid(r.id.vf_help);
for (int i = 0; i < resid.length; i++) {
 mvfhelp.addview(getimageview(resid[i]));
}

使用示例

效果图:

使用viewflipper实现图片轮播 - 静态导入

切换动画:

1.right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

 <translate
 android:duration="1000"
 android:fromxdelta="100%p"
 android:toxdelta="0" />

</set>

2.right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

 <translate
 android:duration="1000"
 android:fromxdelta="0"
 android:toxdelta="-100%p" />

</set>


布局文件:

<?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">

 <viewflipper
 android:id="@+id/vf_help"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:flipinterval="3000"
 android:inanimation="@anim/right_in"
 android:outanimation="@anim/right_out">

 <include layout="@layout/page_help_one" />

 <include layout="@layout/page_help_two" />

 <include layout="@layout/page_help_three" />

 <include layout="@layout/page_help_four" />
 </viewflipper>
</linearlayout>


java文件调用:

public class methodoneactivity extends baseactivity {

 private viewflipper mvfhelp;

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_method_one);

 mvfhelp = (viewflipper) findviewbyid(r.id.vf_help);
 mvfhelp.startflipping();
 }
}


支持手势滑动的viewflipper - 动态导入

切换动画:

1.left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

 <translate
 android:duration="1000"
 android:fromxdelta="-100%p"
 android:toxdelta="0" />

</set>

2.left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

 <translate
 android:duration="1000"
 android:fromxdelta="0"
 android:toxdelta="100%p" />

</set>

布局文件:

<?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">

 <viewflipper
 android:id="@+id/vf_help"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />
</linearlayout>


java文件调用:

private void initdata() {
 //实例化simpleongesturelistener与gesturedetector对象
 mglistener = new mygesturelistener();
 mdetector = new gesturedetector(mactivity, mglistener);

 //动态添加子view
 for (int i = 0; i < resid.length; i++) {
 mvfhelp.addview(getimageview(resid[i]));
 }
}

/**
 * @description 重写ontouchevent触发mygesturelistener里的方法
 */
@override
public boolean ontouchevent(motionevent event) {
 return mdetector.ontouchevent(event);
}

/**
 * @description 自定义一个view类下的gesturedetector
 */
private class mygesturelistener extends gesturedetector.simpleongesturelistener {

 @override
 public boolean onfling(motionevent e1, motionevent e2, float v, float v1) {
 if (e1.getx() - e2.getx() > min_move) {
  mvfhelp.setinanimation(mactivity, r.anim.right_in);
  mvfhelp.setoutanimation(mactivity, r.anim.right_out);
  mvfhelp.shownext();
 } else if (e2.getx() - e1.getx() > min_move) {
  mvfhelp.setinanimation(mactivity, r.anim.left_in);
  mvfhelp.setoutanimation(mactivity, r.anim.left_out);
  mvfhelp.showprevious();
 }
 return true;
 }
}

项目地址 ☞ 传送门

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网