Android幼功:手势

Android基本功:手势

一、概念 

  • 手势:其实是指用户手指或触摸笔在屏幕上的连续触碰行为,Andoird对两种手势行为都提供了支持 

    • Andorid提供了手势检测,并为手势检测提供了相应的监听器 

    • Android允许开发者添加手势,并提供了相应的API识别用户手势 

     

二、手势检测 

  • Gesture类:代表了一个手势检测器 

  • GestureDectector.OnGestureListener类:代表一个监听器、负责对用户的手势行为提供响应 

    • boolean onDown(MotionEvent e):当触碰事件按下时触发的方法 

    • boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY):当用户在触摸屏上”拖过”时触发该方法,velocityX,velocityY代表“拖过”动作的横向、纵向上的速度 

    • abstract void onLongPress(MotionEvent e):当用户在屏幕上长按时触发该方法 

    • abstract void onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float diastanceY):当用户在屏幕上“滚动”时触发该方法 

    • void onShowPress(MotionEvent e):当用户在屏幕上按下,而且还未移动和松动的时候触发该方法 

    • boolean onSingleTapUp(MotionEvent e):当用户在触摸屏上的轻击事件将会触发该方法 

 

三、使用步骤 

  1. 创建一个GestureDetector对象,创建对象时候必须创建一个GestureDectector.OnGestureListener监听器实例; 

  2. 为应用程序的Activity(偶尔也可以为特定组件)的TouchEvent事件绑定监听器,在事件处理中制定把Activity(或特定组件)上的TouchEvent事件交给GestureDetector处理; 

 

  • 使用实例(chapter08/GestureTest 

MainActivity.java文件 

public class MainActivity extends Activity implements OnGestureListener { 
    // 定义手势检测实例 
    GestureDetector detector; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main_activity); 
 
        // 创建手势检测器 
        detector = new GestureDetector(this, this); 
    } 
 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
        // 将该Activity上的触碰事件交给GestureDetector处理 
        return detector.onTouchEvent(event); 
    } 
 
    @Override 
    public boolean onDown(MotionEvent e) { 
         // 触碰时间按下时触发该方法 
        Toast.makeText(this, "OnDown", Toast.LENGTH_LONG).show(); 
        return false; 
    } 
 
    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
float velocityY) { 
         // 当用户在屏幕上“拖动”时触发该方法 
        Toast.makeText(this, "onFling", Toast.LENGTH_LONG).show(); 
        return false; 
    } 
 
    @Override 
    public void onLongPress(MotionEvent e) { 
        // 当用户在屏幕上长按时触发该方法 
        Toast.makeText(this, "onLongPress", Toast.LENGTH_LONG).show(); 
    } 
 
    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
float distanceY) { 
        // 当屏幕“滚动”时触发该方法 
        Toast.makeText(this, "onScroll", Toast.LENGTH_LONG).show(); 
        return false; 
    } 
 
    @Override 
    public void onShowPress(MotionEvent e) { 
        // 当用户在触摸屏幕上按下、而且还未移动和松开时触发该方法 
        Toast.makeText(this, "onShowPress", Toast.LENGTH_LONG).show(); 
    } 
 
    @Override 
    public boolean onSingleTapUp(MotionEvent e) { 
        // 在屏幕上的轻击事件将会触发该方法 
       Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_LONG).show(); 
        return false; 
    } 
} 

四、增加手势 

  • Android除了提供了手势之外,还允许应用程序把用户手势(多个持续的触摸事件在屏幕上形成特定的形状)添加到制定文件中,以备以后使用; 

  • GestureLibrary类:代表手势库,并提供了GestureLibraries工具来创建手势库,提供了如下4个静态方法从不同的位置加载手势: 

    • static GestureLibrary from(String path):从path代表的文件中加载手势库 

    • static GestureLibrary fromFile(File path):从path代表的文件加载手势库 

    • static GestureLibrary fromPrivateFile(Context context,String name):从制定应用程序的数据文件夹中name文件中加载手势库 

    • static GestureLibrary fromRawResoure(Context context,int resourceId):从resourceId所代表的资源中加载手势库 

 

  • 获取GestureLibrary对象之后,该对象提供了如下方法来添加手势和识别手势 

    • void addGesture(String entryName,Gesture gesture):添加一个名为name的手势 

    • Set<String> getGestureEntries():获取手势库中所有的手势名称 

    • ArrayList<GuesturegetGestures(String entryName):获取entryName名称对应的全部手势 

    • ArrayList<Prediction> recongize(Guesture gesture):从当前手势库中识别与gesture匹配的全部手势 

    • void removeEntry(String entryName):删除手势库中entryName对应的手势 

    • void removeGesture(String entryName,Gesture gesture):删除手势库中entryName,gesture对应的手势库 

    • boolean save():当向手势库中添加手势或从中删除手势后调用该方法保存手势库 

 

  • 为了监听GestureOverlayView组件上的手势事件,Android为GestureOverlayView提供了OnGestureLisnter、OnGesturePerformedListener、OnGesturingListener三个监听器接口,分别用于响应手势事件开始、结束、完成、取消事件; 

     

  • 使用实例(chapter08/AddGesture 

main_activity.xml文件 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:text="请在下面屏幕上绘制手势" /> 
    <!-- android:gestureStrokeType手势是否需要一笔完成--> 
    <android.gesture.GestureOverlayView 
        android:id="@+id/gesture" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:gestureStrokeType="multiple" /> 
</LinearLayout> 
 

sava.xml文件 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" > 
        <TextView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_marginRight="8dip" 
            android:text="请输入手势名称" /> 
        <!-- 定义一个文本框让用户输入手势名 --> 
        <EditText 
            android:id="@+id/gesture_name" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" /> 
    </LinearLayout> 
    <!-- 定义一个图片框来显示手势 --> 
    <ImageView 
        android:id="@+id/show" 
        android:layout_width="match_parent" 
        android:layout_height="128dp" 
        android:layout_marginTop="10dp" 
        android:layout_weight="0.29" /> 
</LinearLayout> 
 

MainActivity.java文件 

public class MainActivity extends Activity { 
    EditText editText; 
    GestureOverlayView gestureOverlayView; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main_activity); 
 
        // 使用文本编辑器 
        editText = (EditText) findViewById(R.id.gesture_name); 
        // 获取手势编辑视图 
        gestureOverlayView = (GestureOverlayView) findViewById(R.id.gesture); 
        // 设置手势绘图的颜色 
        gestureOverlayView.setGestureColor(Color.RED); 
        // 设置手势的绘制宽度 
        gestureOverlayView.setGestureStrokeWidth(4); 
        // 为gesture的手势完成事件绑定事件监听器 
        gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() { 
 
        @Override 
        public void onGesturePerformed(GestureOverlayView overlay,final Gesture gesture) { 
            // 加载save.xml界面布局代表的视图 
            View saveDialog = getLayoutInflater().inflate(R.layout.save, null); 
            // 获取saveDialog里的show组件 
            ImageView imageView = (ImageView) saveDialog.findViewById(R.id.show); 
            // 获取saveDialog的gesture_name组件 
            final EditText gestureEditText = (EditText) saveDialog.findViewById(R.id.gesture_name); 
           // 根据Gesture包含的手势创建一个位图 
            Bitmap bitmap = gesture.toBitmap(128, 128, 10,0xffff0000); 
            imageView.setImageBitmap(bitmap); 
            // 使用对话框显示saveDialog组件 
            new AlertDialog.Builder(MainActivity.this).setView(saveDialog).setPositiveButton("保存", new OnClickListener() { 
                @Override 
                public void onClick(DialogInterface dialog,int which) { 
                   // 获取制定文件对应的手势库 
                   GestureLibrary guestureLibrary = GestureLibraries.fromFile(Environment 
.getExternalStorageDirectory().getPath()+ "/mygestures"); 
                  // 添加手势 
                  guestureLibrary.addGesture(gestureEditText.getText().toString(), gesture); 
                  guestureLibrary.save(); 
                } 
           }).setNegativeButton("取消", null).show(); } 
        }); 
    } 
} 

五、识别用户手势 

  • recoginze(Gesture guesture)方法:识别手势,该方法将会返回该手势库中所有与ges匹配的手势两个手势的图形越相似,相似度越高 

  • recogniza(Gusture ges)方法返回为ArrayList<Prediction>,启动Prediction封装了手势的匹配信息,Predictin对象的name属性代表了匹配的手势名,score属性代表了手势的相似度; 

 

  • 应用实例(/chapter08/RecognizeGesture 

main_activity.xml文件 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <android.gesture.GestureOverlayView 
        android:id="@+id/gesture" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:gestureStrokeType="multiple" /> 
</LinearLayout> 
 

MainActivity.java文件 

public class MainActivity extends Activity { 
    // 定义手机编辑组件 
    GestureOverlayView gestureOverlayView; 
    // 记录手机上已有的手势库 
    GestureLibrary gestureLibrariLibrary; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main_activity); 
 
        gestureOverlayView = (GestureOverlayView) findViewById(R.id.gesture); 
        gestureLibrariLibrary = GestureLibraries.fromFile(Environment.getExternalStorageDirectory().getPath() + "/mygestures"); 
 
        if (gestureLibrariLibrary.load()) { 
            Toast.makeText(MainActivity.this, "手势文件装在成功", Toast.LENGTH_LONG).show(); 
        } else { 
            Toast.makeText(MainActivity.this, "手势文件装在失败", Toast.LENGTH_LONG).show(); 
        } 
 
        // 定义手势编辑组件绑定监听器 
        gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() { 
 
        @Override 
        public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) { 
            // 识别用户刚刚所绘制的手势 
            ArrayList<Prediction> predictions = gestureLibrariLibrary.recognize(gesture); 
            ArrayList<String> result = new ArrayList<String>(); 
            // 遍历所有找到的Prediction对象 
            for (Prediction prediction : predictions) { 
                // 只有相似度大于0.2的手势才会被输出 
                if (prediction.score > 0.2) { 
                    result.add("与手势{" + prediction.name + "}相似度为:"+ prediction.score); 
                } 
            } 
 
            if (result.size() > 0) { 
                ArrayAdapter<Object> arrayAdapter = new ArrayAdapter<Object>(MainActivity.this, 
android.R.layout.simple_dropdown_item_1line, 
                result.toArray()); 
                new AlertDialog.Builder(MainActivity.this).setAdapter(arrayAdapter, null).setPositiveButton("确定", null).show(); 
            } else { 
               Toast.makeText(MainActivity.this, "无法能找到匹配的手势", 
               Toast.LENGTH_LONG).show(); 
            } 
        }}); 
    } 
}