ViewFlipper(翻转视图)的使用

android developers

java.lang.Object

--android.view.View

----android.view.ViewGroup

------android.widget.FrameLayout

--------android.widget.ViewAnimator

----------android.widget.ViewFlipper

Simple ViewAnimator that will animate between two or more views that have been added to it. Only one child is shown at a time. If requested, can automatically flip between each child at a regular interval.

理解

ViewFlipper,它是Android自带的一个多页面管理控件,且可以自动播放! 和ViewPager不同,ViewPager是一页页的,而ViewFlipper则是一层层的,和ViewPager一样,很多时候, 用来实现进入应用后的引导页,或者用于图片轮播

使用

1.为ViewFlipper加入view

(1)静态导入

在xml布局中,将页面添加到viewFlipper中

    <ViewFlipper
        android:id="@+id/hlep"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <include layout="@layout/page_one"></include>
        <include layout="@layout/page_two"></include>
        <include layout="@layout/page_three"></include>
    </ViewFlipper>

(2)动态导入

通过addview加入view

hlep = (ViewFlipper)findViewById(R.id.hlep);
hlep.addView(V1);
hlep.addView(V2);

2.常用方法

3.动画控制

进入动画

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

    <translate
        android:duration="2000"
        android:fromXDelta="100%p"
        android:toXDelta="0" />

</set>

滑出动画

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

    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />

</set>

其中:

    translate 位置转移动画效果  
            整型值:  
                fromXDelta 属性为动画起始时 X坐标上的位置  
                toXDelta   属性为动画结束时 X坐标上的位置  
                fromYDelta 属性为动画起始时 Y坐标上的位置  
                toYDelta   属性为动画结束时 Y坐标上的位置  
                注意:  
                         没有指定,  
                         默认是以自己为相对参照物  
            长整型值:  
                duration  属性为动画持续时间  

                说明:   时间以毫秒为单位  

在这些属性里面还可以加上%和p,例如:

android:toXDelta="100%",表示自身的100%,也就是从View自己的位置开始。

android:toXDelta="80%p",表示父层View的80%,是以它父层View为参照的。

4.静态布局设计

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ViewFlipper
        android:id="@+id/help"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inAnimation="@anim/in"
        android:outAnimation="@anim/out"
        android:flipInterval="3000">

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

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

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

    </ViewFlipper>

</RelativeLayout>

5.静态使用

public class MainActivity extends AppCompatActivity {

    private ViewFlipper help;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       help = (ViewFlipper) findViewById(R.id.help);
        help.startFlipping();
    }
}

动态需要通过手势判断,后续用到再做讨论~~~~~~

posted on 2016-10-19 10:51  不二家的瓶子  阅读(167)  评论(0编辑  收藏  举报

导航