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

首頁 > 系統 > Android > 正文

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

2019-10-22 18:27:05
字體:
來源:轉載
供稿:網友

本文實例講述了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開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福鼎市| 晋中市| 类乌齐县| 闸北区| 洪江市| 榆中县| 绍兴县| 石台县| 宜君县| 大城县| 祁阳县| 巧家县| 澎湖县| 霍林郭勒市| 周宁县| 阿尔山市| 云梦县| 南岸区| 儋州市| 盐源县| 辰溪县| 昌黎县| 景宁| 西乌珠穆沁旗| 黎平县| 徐州市| 江门市| 五华县| 武安市| 大宁县| 商水县| 泾源县| 上犹县| 丁青县| 卢湾区| 巴林左旗| 廊坊市| 宾川县| 珲春市| 青田县| 侯马市|