安卓实例:手势操作

一.实现效果
当我们在屏幕上滑动时就能改变图片(我用的软件鼠标录不进去)
在这里插入图片描述
二.涉及知识点
1、线性布局(LinearLayout)
2、图像视图(ImageView)
3、单点触摸事件(MotionEvent)
三.实现步骤
1.布局文件
因为我们只是做滑动切换图片,所以只需要一个简单的布局

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

2.主界面类

public class gesture extends Activity {
    private GestureDetector detector;//手势监测器
    private int[] ImgIds;//图片id数组,将图片id储存起来方便使用
    private int imgIndex;//图片索引
    private static final int IMG_COUNT=13;//图片数量
    private LinearLayout root;
    private final String TAG="net.hw.deom0505";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shoushi);

        root=findViewById(R.id.root);

        ImgIds=new int[IMG_COUNT];放入数量
        for (int i=0;i<IMG_COUNT;i++){
            ImgIds[i]=getResources().getIdentifier("img"+(i+1),"mipmap","net.hw.deom0505");//储存id
        }
		//手势监测器,可以监测按下,长短距离滑动等
        detector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                Log.i(TAG, "onDown: ");
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {
                Log.i(TAG, "onShowPress: ");
            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                Log.i(TAG, "onSingleTapUp: ");
                return false;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                Log.i(TAG, "onScroll: ");
                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                Log.i(TAG, "onLongPress: ");
            }

            @Override//onFling fling是扔,抛的意思,当手在屏幕上滑动是触发的事件
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                Log.i(TAG, "onFling: ");
                //监测左右滑动,当某个方向滑动超过20索引变化
                if(e2.getX()<e1.getX()-20){
                    if (imgIndex<IMG_COUNT-1){
                        imgIndex++;
                    }else {
                        imgIndex=0;
                    }
                }else if(e2.getX()>e1.getX()+20){
                    if (imgIndex>0){
                        imgIndex--;
                    }else {
                        imgIndex=IMG_COUNT-1;
                    }
                }
				//将变化后的索引放入,来改变图片
                root.setBackgroundResource(ImgIds[imgIndex]);
                return false;
            }
        });
    }
    @Override//要将触摸事件交接给滑动事件,让滑动事件接受到
    public boolean onTouchEvent(MotionEvent event){
        return detector.onTouchEvent(event);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41858200/article/details/83820196
今日推荐