在Android開發(fā)中,我們不光可以使用已有的實現(xiàn)方式,而且,我們還可以利用Android這個智能手機平臺,實現(xiàn)一些比較有特色的功能。本篇文章介紹使用GestureOverlayView這個控件,實現(xiàn)簡單的手勢識別的小例子。
創(chuàng)新互聯(lián)服務項目包括永福網(wǎng)站建設、永福網(wǎng)站制作、永福網(wǎng)頁制作以及永福網(wǎng)絡營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,永福網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務的客戶以成都為中心已經(jīng)輻射到永福省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!
首先,在使用手勢識別之前,我們需要建立一個手勢庫,創(chuàng)建手勢庫,我們可以找到sdk自帶的實例程序,比如我本地的路徑為sdk\samples\android-18\input\gestures,找到這個程序,然后建立一個新項目,將其整合之后,就可以用于產(chǎn)生手勢庫。
整合之后的項目結(jié)構(gòu)如下
下面是運行界面
產(chǎn)生的手勢庫,默認存放在sd卡的根目錄下面,我們將生成的手勢庫文件放在我們的raw目錄下面
下面,我們就開始寫布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <android.gesture.GestureOverlayView android:id="@+id/gestures" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1.0" android:gestureStrokeType="multiple" /> </LinearLayout>
布局很簡單,就是一個用于接受手勢的控件,下面是邏輯代碼的實現(xiàn)
public class MainActivity extends Activity { private boolean success; // 定義手勢庫 private GestureLibrary library; private GestureOverlayView gestureView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 找到手勢庫 library = GestureLibraries.fromRawResource(this, R.raw.gestures); // 加載手勢庫 success = library.load(); gestureView = (GestureOverlayView) this.findViewById(R.id.gestures); // 添加事件監(jiān)聽器 gestureView.addOnGesturePerformedListener(new GestureListener()); } private final class GestureListener implements OnGesturePerformedListener { @Override public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { // 如果手勢庫加載成功 if (success) { // 從手勢庫中查找匹配的手勢,最匹配的記錄會放在最前面 ArrayList<Prediction> predictions = library.recognize(gesture); if (!predictions.isEmpty()) { // 獲取第一個匹配的手勢 Prediction prediction = predictions.get(0); // 如果匹配度>30%,就執(zhí)行下面的操作 if (prediction.score > 3) { // 關閉應用 if ("agree".equals(prediction.name)) { android.os.Process.killProcess(android.os.Process .myPid()); // 撥打電話 } else if ("5556".equals(prediction.name)) { Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:5556")); startActivity(intent); } } } } } } }
經(jīng)過這幾個步驟,我們就實現(xiàn)了最簡單的手勢識別的功能了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
名稱欄目:Android使用GestureOverlayView控件實現(xiàn)手勢識別
文章源于:http://sd-ha.com/article36/geessg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信公眾號、電子商務、企業(yè)網(wǎng)站制作、靜態(tài)網(wǎng)站、營銷型網(wǎng)站建設、網(wǎng)站導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)