Android控件六:切换图片的播放AdapterViewFlipper

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);
       }
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值