安卓手势操作编程
一、思维结构
二、涉及知识点
1、线性布局—LinearLayout
2、动作事件—MotionEvent
2、手势侦测器—GestureDetector
3、手势监听器—OnGestureListener
三、步骤
1、创建安卓应用SwtichBelleImageByGesture
2、将13张美女图片命名为img1.jpg、img2.jpg、……、img13.jpg,拷贝到mipmap目录里
3、主布局资源文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/img1"
android:orientation="vertical"
tools:context="net.hw.image_by_guesture.MainActivity">
</LinearLayout>
4、主界面类MainActivity
/**
* 功能:利用手势切换图像
*/
package net.hw.image_by_guesture;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
/**
* 手势侦测器
*/
private GestureDetector detector;
/**
* 图像资源标识符数组
*/
private int[] imgIds;
/**
* 图像索引,反映在图像资源标识符数组中的位置
*/
private int imgIndex;
/**
* 图像总数(准备的图片数)
*/
private static final int IMG_COUNT = 13;
/**
* 根线性布局
*/
private LinearLayout root;
/**
* 定义程序标记
*/
private final static String TAG = "image";
@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), "mipmap", "net.hw.image_by_guesture"
);
}
// 实例化手势侦测器(参数1:上下文环境,参数2:手势监听器对象)
detector = new GestureDetector(this, new GestureDetector.OnGestureListener(){
@Override
public boolean onDown(MotionEvent e) {
Log.d(TAG, "onDown");
Toast.makeText(MainActivity.this, "温馨提示:向左或向右滑动,可以切换图片", Toast.LENGTH_LONG).show();
return false;
}
@Override
public void onShowPress(MotionEvent e) {
Log.d(TAG, "onShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d(TAG, "onSingleTapUp");
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d(TAG, "onScroll");
return false;
}
@Override
public void onLongPress(MotionEvent e) {
Log.d(TAG, "onLongPress");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d(TAG, "onFling");
// 手势往左滑动20个像素,图片切换到下一张
if (e2.getX() <= e1.getX() - 20) {
// 更改图像索引
if (imgIndex < IMG_COUNT - 1) {
imgIndex++;
} else {
imgIndex = 0;
}
}
// 手势往右滑动20个像素,图片切换到上一张
if (e2.getX() >= e1.getX() + 10) {
// 更改图像索引
if (imgIndex > 0) {
imgIndex--;
} else {
imgIndex = IMG_COUNT - 1;
}
}
// 利用更改后的图像索引去设置根布局的背景图片
root.setBackgroundResource(imgIds[imgIndex]);
// 返回真,事件处理到此完毕
return true;
}
});
}
/**
* 将窗口的触摸事件交给手势侦测器处理
*
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
return detector.onTouchEvent(event);
}
}