当前位置: 移动技术网 > IT编程>移动开发>Android > Android ViewFlipper的详解及实例

Android ViewFlipper的详解及实例

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

续三侠剑在线阅读,日耳曼英雄,红外球

android viewflipper的详解

前言:

view flipper,是viewanimator的子类,而viewanimator又是继承自framelayout,而framelayout就是平时基本上只显示一个子视图的布局,由于framelayout下不好确定子视图的位置,所以很多情况下子视图之前存在相互遮挡,这样就造成了很多时候我们基本上只要求framelayout显示一个子视图,然后通过某些控制来实现切换。正好,viewflipper帮我们实现了这个工作,我们需要做的就是,选择恰当的时机调用其恰当的方法即可

类结构

方法 意义
startflipping 开始浏览
stopflipping 停止浏览
setflipinterval 设置view之间切换的时间间隔
getaccessibilityclassname 获取类名称
isflipping 判断是否正在浏览
setautostart 设置是否自动开始浏览
isautostart 判断是否为自动开始浏览

基本使用

1. 动画定义

scalein.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <scale
    android:duration="1000"
    android:fromxscale="0.2"
    android:fromyscale="0.2"
    android:toyscale="1"
    android:toxscale="1"
    android:pivotx="50%"
    android:pivoty="50%"
    >
  </scale>
</set>

scaleout.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <scale
    android:duration="1000"
    android:fromxscale="1"
    android:fromyscale="1"
    android:toyscale="0.2"
    android:toxscale="0.2"
    android:pivotx="50%"
    android:pivoty="50%">
  </scale>
</set>

2. 布局文件

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="jzfp.gs.com.animationdemo.mainactivity">

  <android.support.v7.widget.toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionbarsize"
    android:background="@color/colorprimary"></android.support.v7.widget.toolbar>

  <!--渐入动画 和 渐出动画定义-->
  <viewflipper
    android:id="@+id/vf"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:inanimation="@anim/scalein"
    android:outanimation="@anim/scaleout">

    <imageview
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@mipmap/one" />

    <imageview
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@mipmap/two" />


    <imageview
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@mipmap/three" />

  </viewflipper>

</linearlayout>

3. 左右滑动切换

public class mainactivity extends appcompatactivity {

  private viewflipper viewflipper = null;
  float posx = 0, currentx = 0;

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

    toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);
    toolbar.setnavigationicon(r.drawable.left);
    setsupportactionbar(toolbar);//设置actionbar

    viewflipper = (viewflipper) findviewbyid(r.id.vf);
  }

  @override
  public boolean ontouchevent(motionevent event) {
    switch (event.getaction()) {
      case motionevent.action_down:
        posx = event.getx();
        break;
      case motionevent.action_move:
        currentx = event.getx();
        break;
      case motionevent.action_up:
        if (currentx - posx > 25.0) {//向右滑动切换到上一页
          viewflipper.showprevious();
        } else if (currentx - posx < -25.0) {//向左滑动,切换到下一页
          viewflipper.shownext();
        }
    }
    return true;
  }
}

实际效果

以上就是android viewflipper的使用方法,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

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

相关文章:

验证码:
移动技术网