国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程使用GestureDetector實現簡單手勢監聽與處理的方法

2019-12-12 01:59:15
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程使用GestureDetector實現簡單手勢監聽與處理的方法。分享給大家供大家參考,具體如下:

添加手勢識別監聽步驟:

一、給相應的控件添加觸摸監聽事件,

二、利用GestureDetector轉發這個觸摸事件。

三、事先定義好一個實現simpleongestureListener這個監聽的接口的類

四、在這個監聽中處理各種事件。

具體代碼如下:

MainActivity代碼如下:

package com.example.gesturedetector;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.GestureDetector;import android.view.MotionEvent;import android.view.View;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends AppCompatActivity {   ImageView img;  GestureDetector myGestureDetector;  class myGestureListener extends GestureDetector.SimpleOnGestureListener {    @Override    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {      if (e1.getX()-e2.getX()>50)      {        Toast.makeText(MainActivity.this,"從右往左滑動",Toast.LENGTH_LONG).show();      }else if(e2.getX()-e1.getX()>50){        Toast.makeText(MainActivity.this,"從左往右滑動",Toast.LENGTH_LONG).show();      }      return super.onFling(e1, e2, velocityX, velocityY);    }  }  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    img = (ImageView) findViewById(R.id.img);    myGestureDetector = new GestureDetector(new myGestureListener());    img.setOnTouchListener(new View.OnTouchListener() {      //motionEvent可以捕捉我們觸摸屏幕的event事件      @Override      public boolean onTouch(View view, MotionEvent motionEvent) {        myGestureDetector.onTouchEvent(motionEvent);        return true;      }    });  }}

activity_main的代碼如下:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:paddingBottom="@dimen/activity_vertical_margin"  android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  tools:context="com.example.gesturedetector.MainActivity">  <ImageView    android:id="@+id/img"    android:src="@mipmap/ic_launcher"    android:layout_centerHorizontal="true"    android:layout_centerVertical="true"    android:layout_width="match_parent"    android:layout_height="match_parent" /></RelativeLayout>

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android手勢操作技巧匯總》、《Android基本組件用法總結》、《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 孙吴县| 西城区| 平利县| 英超| 玛多县| 平江县| 灌阳县| 平塘县| 周宁县| 三都| 鄂州市| 鄂托克旗| 万宁市| 嘉义县| 宿迁市| 凤山县| 东乡| 库尔勒市| 黄山市| 东乌珠穆沁旗| 轮台县| 余庆县| 乐昌市| 都昌县| 页游| 湾仔区| 上林县| 辽阳县| 龙门县| 景谷| 台江县| 米泉市| 赤水市| 共和县| 大庆市| 隆林| 东阳市| 安西县| 许昌县| 伊金霍洛旗| 嵊州市|