ViewFlipper的使用(图片切换)

当监听到滑动,自动播放就会暂停。

示例图1(轮播)
ViewFlipper的使用(图片切换)_第1张图片

Activity:


public class MainActivity extends AppCompatActivity implements android.view.GestureDetector.OnGestureListener{
    private int[] imgs={R.drawable.img1,R.drawable.img2,R.drawable.img3};
    private GestureDetector gestureDetector=null;
    private ViewFlipper viewFlipper=null;
    private AppCompatActivity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mActivity=this;
        viewFlipper= (ViewFlipper) findViewById(R.id.viewflipper);
        gestureDetector=new GestureDetector(this);
        for(int i=0;inew ImageView(this);
            imageView.setImageResource(imgs[i]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            viewFlipper.addView(imageView,new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            viewFlipper.setAutoStart(true);
            viewFlipper.setFlipInterval(3000);//设置View播放的时间间隔,如3000(3秒)
            if(viewFlipper.isAutoStart()&&!viewFlipper.isFlipping()){
                viewFlipper.startFlipping();
            }
        }

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }


    @Override
    public boolean onDown(MotionEvent e) {
        return false;//表示按下事件
    }

    @Override
    public void onShowPress(MotionEvent e) {
     //down事件发生而move或则up还没发生前触发该事件,此事件一般用于通知用户press按击事件已发生
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;//一次点击up事件,表示按下后的抬起事件
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;//:在屏幕上拖动事件,即down按下点——scroll拖动——up抬起点的move移动事件
    }

    @Override
    public void onLongPress(MotionEvent e) {
    //长按事件,down事件后up事件前的一段时间间隔后(由系统分配,也可自定义),如果仍然按住屏幕则视为长按事件
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        viewFlipper.stopFlipping();             // 点击事件后,停止自动播放
        viewFlipper.setAutoStart(false);
        return gestureDetector.onTouchEvent(event);         // 注册手势事件
    }

    @Override//滑动手势事件,例如scroll事件后突然up,fling的速度大小由e每秒x和y改变大小决定
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (e2.getX() - e1.getX() > 120) {            // 从左向右滑动(左进右出)
            Animation rInAnim = AnimationUtils.loadAnimation(mActivity, R.anim.push_right_in);  // 向右滑动左侧进入的渐变效果(alpha  0.1 -> 1.0)
            Animation rOutAnim = AnimationUtils.loadAnimation(mActivity, R.anim.push_right_out); // 向右滑动右侧滑出的渐变效果(alpha 1.0  -> 0.1)

            viewFlipper.setInAnimation(rInAnim);
            viewFlipper.setOutAnimation(rOutAnim);
            viewFlipper.showPrevious();
            return true;
        } else if (e2.getX() - e1.getX() < -120) {        // 从右向左滑动(右进左出)
            Animation lInAnim = AnimationUtils.loadAnimation(mActivity, R.anim.push_left_in);       // 向左滑动左侧进入的渐变效果(alpha 0.1  -> 1.0)
            Animation lOutAnim = AnimationUtils.loadAnimation(mActivity, R.anim.push_left_out);     // 向左滑动右侧滑出的渐变效果(alpha 1.0  -> 0.1)

            viewFlipper.setInAnimation(lInAnim);
            viewFlipper.setOutAnimation(lOutAnim);
            viewFlipper.showNext();
            return true;
        }
        return true;
    }
}

XML:


<LinearLayout 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"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.it.zzb.viewflipperdemo.MainActivity"
    tools:showIn="@layout/activity_main">

    <ViewFlipper
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewflipper"
        >ViewFlipper>
LinearLayout>

滑动内容:push_left_in


<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="100%p"
        android:toXDelta="0" />

    <alpha
        android:duration="500"
        android:fromAlpha="0.1"
        android:toAlpha="1.0" />
set>

滑动内容:push_left_out


<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />

    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />
set>

滑动内容:push_right_in


<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="-100%p"
        android:toXDelta="0" />

    <alpha
        android:duration="500"
        android:fromAlpha="0.1"
        android:toAlpha="1.0" />
set>

滑动内容:push_right_out


<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
           android:duration="500"
           android:fromXDelta="0"
           android:toXDelta="100%p" />

       <alpha
           android:duration="500"
           android:fromAlpha="1.0"
           android:toAlpha="0.1" />
set>

你可能感兴趣的:(android)