AdapterViewFlipper继承了AdapterViewAnimator,它会显示一个View组件,可以通过showPrevious()和showNext()方法控制组件显示上一个、下一个组件。
效果图:
XML属性:
- android:animateFirstView:设置显示该组件的第一个View时是否使用动画
- android:inAnimation:设置组件显示时使用的动画
- android:loopViews:设置循环到最后一个组件后是否自动“转头”到第一个组件
- android:outAnimation:设置组件隐藏时使用的动画
- android:autoStart:设置显示该组件是否是自动播放
- android:flipInterval:设置自动播放的时间间隔
相关方法:
- startFlipping();设置显示该组件是否是自动播放
- setFlipInterval(int);设置自动播放的时间间隔
代码中使用:
1,布局中
<android.support.constraint.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=".MainActivity">
<AdapterViewFlipper
android:layout_width="0dp"
android:id="@+id/flipper"
android:flipInterval="3000"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/auto"
app:layout_constraintVertical_weight="5"
android:layout_height="0dp">
</AdapterViewFlipper>
<Button
android:layout_width="0dp"
android:text="上一张"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/flipper"
app:layout_constraintRight_toLeftOf="@id/auto"
app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/prev"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintVertical_weight="1"
android:layout_height="0dp" />
<Button
android:layout_width="0dp"
android:text="自动播放"
app:layout_constraintLeft_toRightOf="@id/prev"
app:layout_constraintTop_toBottomOf="@id/flipper"
app:layout_constraintRight_toLeftOf="@id/next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintVertical_weight="1"
android:id="@+id/auto"
android:layout_height="0dp" />
<Button
android:layout_width="0dp"
app:layout_constraintLeft_toRightOf="@id/auto"
app:layout_constraintTop_toBottomOf="@id/flipper"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintVertical_weight="1"
android:text="下一张"
android:id="@+id/next"
android:layout_height="0dp" />
</android.support.constraint.ConstraintLayout>
2,Java中
images = new int[]{R.drawable.aa, R.drawable.bb, R.drawable.cc, R.drawable.dd};
flipper = findViewById(R.id.flipper);
prev = findViewById(R.id.prev);
auto = findViewById(R.id.auto);
next = findViewById(R.id.next);
prev.setOnClickListener(this);
auto.setOnClickListener(this);
next.setOnClickListener(this);
flipper.setAdapter(new BaseAdapter() {
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
holder = null;
if (view == null) {
view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
holder = new ViewHolder(view);
view.setTag(view);
} else {
holder = (ViewHolder) view.getTag();
}
holder.iv.setBackgroundResource(images[i]);
return view;
}
});
ObjectAnimator oa = ObjectAnimator.ofFloat(holder.iv,"alpha",new float[]{1, 0,(float) 0.5,1});
flipper.setInAnimation(oa);
// flipper.setOutAnimation(oa);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.prev:
flipper.stopFlipping();
flipper.showPrevious();
break;
case R.id.auto:
flipper.startFlipping();
break;
case R.id.next:
flipper.stopFlipping();
flipper.showNext();
break;
default:
}
}
public class ViewHolder {
ImageView iv;
ViewHolder(View view) {
iv = view.findViewById(R.id.iv);
}
}