欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程使用GestureDetector實(shí)現(xiàn)簡(jiǎn)單手勢(shì)監(jiān)聽與處理的方法

 更新時(shí)間:2017年09月27日 09:54:17   作者:ITzhongzi  
這篇文章主要介紹了Android編程使用GestureDetector實(shí)現(xiàn)簡(jiǎn)單手勢(shì)監(jiān)聽與處理的方法,簡(jiǎn)單講述了Android手勢(shì)監(jiān)聽的原理并結(jié)合實(shí)例形式分析了GestureDetector實(shí)現(xiàn)手勢(shì)監(jiān)聽與處理的相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android編程使用GestureDetector實(shí)現(xiàn)簡(jiǎn)單手勢(shì)監(jiān)聽與處理的方法。分享給大家供大家參考,具體如下:

添加手勢(shì)識(shí)別監(jiān)聽步驟:

一、給相應(yīng)的控件添加觸摸監(jiān)聽事件,

二、利用GestureDetector轉(zhuǎn)發(fā)這個(gè)觸摸事件。

三、事先定義好一個(gè)實(shí)現(xiàn)simpleongestureListener這個(gè)監(jiān)聽的接口的類

四、在這個(gè)監(jiān)聽中處理各種事件。

具體代碼如下:

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,"從右往左滑動(dòng)",Toast.LENGTH_LONG).show();
      }else if(e2.getX()-e1.getX()>50){
        Toast.makeText(MainActivity.this,"從左往右滑動(dòng)",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>

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

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論