手机屏幕尺寸不大,为了在有限空间中展示尽可能多的信息,Android设计了多种方式显示超出屏幕尺寸的界面,包括上下滚动、左右滑动等。飞掠视图ViewFlipper的层次翻动就是其中一项技术。两者都是一系列类似视图的组合,与ViewPager(翻页视图)相比,ViewFlipper更像是视图的立体排列(如现实生活中的书籍),从上往下翻页;ViewPager更像是一幅长长的平面画卷从左往右翻页。
一、ViewFlipper的常用方法
- addView:添加飞掠视图的内页视图。
- setFlipInterval:设置每次翻页的时间间隔。单位毫秒。
- setAutoStart:设置是否自动开始翻页。为true表示自动开始。
- startFlipping:开始翻页。
- stopFlipping:停止翻页。
- isFlipping:判断当前是否正在翻页。
- showNext:显示下一个视图。
- showPrevious:显示上一个视图。
- setDisplayedChild:设置当前展示第几个视图。
- getDisplayedChild:获取当前展示的是第几个视图。
- setInAnimation:设置视图的移入动画。
- getInAnimation:获取移入动画的动画对象。
- setOutAnimation:设置视图的移出动画。
- getOutAnimation:获取移出动画的动画对象。
二、代码实例
1. XML代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewFlipperActivity">
<RelativeLayout
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"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/viewFlipper_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="null"
android:textSize="80dp"
android:textStyle="bold"/>
</RelativeLayout>
2. Java代码
public class ViewFlipperActivity extends AppCompatActivity {
private ViewFlipper viewFlipper;
@SuppressLint("MissingInflatedId")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_flipper);
//获取控件
viewFlipper=findViewById(R.id.viewFlipper);
//设置飞掠视图内页
for(int i=1;i<=6;i++){
View view= LayoutInflater.from(ViewFlipperActivity.this).inflate(R.layout.view_flipper_layout,null);
TextView textView=view.findViewById(R.id.viewFlipper_textView);
textView.setText("VF"+i);
viewFlipper.addView(view);
}
//设置翻页时间间隔
viewFlipper.setFlipInterval(1500);
//设置移入视图动画
viewFlipper.setInAnimation(ViewFlipperActivity.this, android.R.anim.fade_in);
//设置移出视图动画
viewFlipper.setOutAnimation(ViewFlipperActivity.this, android.R.anim.fade_out);
//设置是否自动开启翻页
viewFlipper.setAutoStart(true);
//启动翻页
viewFlipper.startFlipping();
//停止翻页
viewFlipper.stopFlipping();
/*
//获取当前视图指针
int index=viewFlipper.getDisplayedChild();
//设置当前视图指针
viewFlipper.setDisplayedChild(index);
//显示下一个视图
viewFlipper.showNext();
//显示上一个视图
viewFlipper.showPrevious();
//判断是否在翻页
boolean bool=viewFlipper.isFlipping();
//获取移入动画对象
Animation inAnimation=viewFlipper.getInAnimation();
//获取移出动画对象
Animation outAnimation=viewFlipper.getOutAnimation();
*/
}
}
tag:翻页视图、ViewPager、飞掠视图、ViewFlipper