++++++++++++++通过手势切换图片+++++++++++++

通过安卓触屏手势可作出相对的操作,在安卓系统中常见的事件有:按键事件、单击事件、焦点事件、触摸事件等,我们可以通过这些事件来完成相应的操作。

代码:

单击事件  View.OnClickListener  对应处理函数是onClick()

焦点事件 View.OnFocusChangeListener  事件处理方法是onFocusChange()

按键事件  View.OnKeyListener  事件处理方法是onKey()

触摸事件 View.OnTouchListener   事件处理方法是onTouch()

例:手势切换图片

package com.example.myapplication;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnGestureListener{
    /**
     * 手势侦测器
     */
    private GestureDetector detector;
    /**
     * 图像资源标识符数组
     */
    private int[] imgIds;
    /**
     * 图像索引,反映在图像资源标识符数组中的位置
     */
    private int imgIndex;
    /**
     * 根线性布局
     */
    private LinearLayout root;
    /**
     * 图像总数
     */
    private final static int IMG_COUNT = 13;
    /**
     * 定义程序标记
     */
    private final static String TAG = "net.hw.switch_image_by_gesture";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 利用布局资源文件设置用户界面
        setContentView(R.layout.activity_main);

        // 通过控件资源标识符获得控件实例
        root = (LinearLayout) findViewById(R.id.root);

        // 初始化图像资源标识符数组
        imgIds = new int[IMG_COUNT];
        for (int i = 0; i < IMG_COUNT; i++) {
            imgIds[i] = getResources().getIdentifier(
                    "img" + (i + 1), // 标识符名称
                    "drawable", // 定义类型
                    "net.hw.switch_image_by_gesture" // 定义包名
            );
        }

        // 实例化手势侦测器(参数1:上下文环境,参数2:手势监听器对象)
        detector = new GestureDetector(this, this);
    }

    /**
     * 将窗口的触摸事件交给手势侦测器处理
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return detector.onTouchEvent(event);
    }

    @SuppressLint("LongLogTag")
    @Override
    public boolean onDown(MotionEvent e) {
        Log.d(TAG, "onDown");
        return true;
    }

    @SuppressLint("LongLogTag")
    @Override
    public void onShowPress(MotionEvent e) {
        Log.d(TAG, "onShowPress");
    }

    @SuppressLint("LongLogTag")
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        Log.d(TAG, "onSingleTapUp");
        return true;
    }

    @SuppressLint("LongLogTag")
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        Log.d(TAG, "onScroll");
        return true;
    }

    @SuppressLint("LongLogTag")
    @Override
    public void onLongPress(MotionEvent e) {
        Log.d(TAG, "onLongPress");
    }

    @SuppressLint("LongLogTag")
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.d(TAG, "onFling");
        // 手势往左滑动10个像素,图片切换到下一张
        if (e1.getX() - e2.getX() >= 10) {
            // 更改图像索引
            if (imgIndex < IMG_COUNT - 1) {
                imgIndex++;
            } else {
                imgIndex = 0;
            }
        }
        // 手势往右滑动10个像素,图片切换到上一张
        if (e2.getX() - e1.getX() >= 10) {
            // 更改图像索引
            if (imgIndex > 0) {
                imgIndex--;
            } else {
                imgIndex = IMG_COUNT - 1;
            }
        }
        // 利用更改后的图像索引去设置根布局的背景图片
        root.setBackgroundResource(imgIds[imgIndex]);

        // 返回真,事件处理到此完毕
        return true;
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/img1"
    android:orientation="vertical" >

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/chenzhilin233/article/details/83999169