GestureDetector手势检测

fengyeezju 2020-05-01

Android手势检测器GestureDetector,要创建一个GestureDetector需要传入一个监听器GestureDetector.OnGestureListener。

案例(实现手机相册侧滑):
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
    <ViewFlipper
        android:id="@+id/flipper"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ViewFlipper>
</RelativeLayout>

准备4个动画资源文件

GestureDetector手势检测

slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:fromXDelta="-100%p"
    android:toXDelta="0"
    android:duration="500"/>
</set>

slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="100%p"
        android:toXDelta="0"/>
</set>

slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="100%p"
        android:toXDelta="0"/>
</set>

slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="100%p"
        android:toXDelta="0"/>
</set>

MainActivity.java

package com.yifenqing.seepicture;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

//第一步:让MainActivity实现GestureDetector.OnGestureListener接口,
// 并实现所有方法
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {

    Animation[] animations = new Animation[4];
    final int distance = 50;
    private int[] images = new int[]{
        R.mipmap.tx1,R.mipmap.tx2,R.mipmap.tx3,R.mipmap.tx4
    };

    //第二步:定义一个全局的手势检测器
    GestureDetector detector;
    ViewFlipper flipper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        detector = new GestureDetector(MainActivity.this,this);
        //第三步:将要显示的图片加载到ViewFliper中,并且初始化动画数组
        flipper = findViewById(R.id.flipper);
        for(int i=0;i<images.length;i++){
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(images[i]);
            flipper.addView(imageView);
        }

        animations[0] = AnimationUtils.
                loadAnimation(this,R.anim.slide_in_left);
        animations[1] = AnimationUtils.
                loadAnimation(this,R.anim.slide_out_left);
        animations[2] = AnimationUtils.
                loadAnimation(this,R.anim.slide_in_right);
        animations[3] = AnimationUtils.
                loadAnimation(this,R.anim.slide_out_right);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        //第4步:在onFling()方法中通过触摸事件的X坐标判断是向左还是向右滑动,并设置动画
       //从右向左滑
        if(e1.getX() - e2.getX() > distance){
            flipper.setInAnimation(animations[2]);
            flipper.setOutAnimation(animations[1]);
            flipper.showPrevious();
            return true;
        }else if(e2.getX() - e1.getX() > distance){
            flipper.setInAnimation(animations[0]);
            flipper.setOutAnimation(animations[3]);
            flipper.showNext();
            return true;
        }
        return false;
    }

    //第5步:将Activity上的触摸事件交给GestureDetectory

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return detector.onTouchEvent(event);
    }
}

这样我们就实现了相册手机的侧滑效果。

相关推荐