Android GestureDetector實(shí)現(xiàn)手勢(shì)滑動(dòng)效果
本文實(shí)例為大家分享了Android GestureDetector實(shí)現(xiàn)手勢(shì)滑動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
目標(biāo)效果:

程序運(yùn)行,手指在屏幕上從左往右或者從右往左滑動(dòng)超過(guò)一定距離,就會(huì)吐司輸出滑動(dòng)方向和距離。
1.activity_main.xml頁(yè)面放置一個(gè)ImageView控件。
activity_main.xml頁(yè)面:
<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"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/ivShow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
2.MainActivity.java頁(yè)面實(shí)現(xiàn)滑動(dòng)方法。
MainActivity.java頁(yè)面:
package com.example.gesturedetector;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView ivShow;
private GestureDetector gestureDetector;
class myGestureListener extends SimpleOnGestureListener{
@Override
/*識(shí)別滑動(dòng),第一個(gè)參數(shù)為剛開始事件,第二個(gè)參數(shù)為結(jié)束事件*/
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if(e1.getX()-e2.getX()>50){
Toast.makeText(MainActivity.this,"從右往左滑動(dòng)"+(e1.getX()-e2.getX()),Toast.LENGTH_LONG).show();
}else if(e2.getX()-e1.getX()>50){
Toast.makeText(MainActivity.this,"從左往右滑動(dòng)"+(e2.getX()-e1.getX()),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);
gestureDetector=new GestureDetector(MainActivity.this,new myGestureListener());
ivShow=(ImageView) findViewById(R.id.ivShow);
ivShow.setLongClickable(true); //view必須設(shè)置為true,否則手勢(shì)識(shí)別無(wú)法正確工作
ivShow.setOnTouchListener(new OnTouchListener() {
/*可以捕獲到觸摸屏幕發(fā)生的Event事件*/
@Override
public boolean onTouch(View arg0, MotionEvent event) {
gestureDetector.onTouchEvent(event);//轉(zhuǎn)發(fā)
return false;
}
});
}
}
3.程序較簡(jiǎn)單,運(yùn)行就可以顯示目標(biāo)效果了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android GestureDetector用戶手勢(shì)檢測(cè)實(shí)例講解
- android使用gesturedetector手勢(shì)識(shí)別示例分享
- Android GestureDetector手勢(shì)滑動(dòng)使用實(shí)例講解
- Android手勢(shì)識(shí)別器GestureDetector使用詳解
- Android自定義viewgroup可滾動(dòng)布局 GestureDetector手勢(shì)監(jiān)聽(5)
- Android自定義GestureDetector實(shí)現(xiàn)手勢(shì)ImageView
- Android編程使用GestureDetector實(shí)現(xiàn)簡(jiǎn)單手勢(shì)監(jiān)聽與處理的方法
- Android觸摸及手勢(shì)操作GestureDetector
- Android使用手勢(shì)監(jiān)聽器GestureDetector遇到的不響應(yīng)問(wèn)題
- Android如何使用GestureDetector進(jìn)行手勢(shì)檢測(cè)詳解
相關(guān)文章
加載頁(yè)面遮擋耗時(shí)操作任務(wù)頁(yè)面--第三方開源之AndroidProgressLayout
AndroidProgressLayout實(shí)現(xiàn)為界面添加圓形進(jìn)度條。調(diào)用setprogress()方法顯示和隱藏進(jìn)度條,這篇文章主要介紹了加載頁(yè)面遮擋耗時(shí)操作任務(wù)頁(yè)面--第三方開源之AndroidProgressLayout的相關(guān)資料,需要的朋友可以參考下2015-11-11
Android EditText實(shí)現(xiàn)輸入金額類型詳解
EditText是Android中一個(gè)非常實(shí)用的控件,有很多InputType,可以來(lái)達(dá)到不同的輸入效果,下面這篇文章主要給大家介紹了關(guān)于Android EditText實(shí)現(xiàn)輸入金額類型的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09
Android編程實(shí)現(xiàn)多列顯示的下拉列表框Spinner功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)多列顯示的下拉列表框Spinner功能,結(jié)合具體實(shí)例形式分析了Android多列表顯示功能的相關(guān)布局操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06
詳解android webView獨(dú)立進(jìn)程通訊方式
本篇文章主要介紹了android webView獨(dú)立進(jìn)程通訊方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
Android按鈕按下的時(shí)候改變顏色實(shí)現(xiàn)方法
這篇文章主要介紹了Android按鈕按下的時(shí)候改變顏色實(shí)現(xiàn)方法,有需要的朋友可以參考一下2014-01-01
Android之使用Android-query框架開發(fā)實(shí)戰(zhàn)(一)
這篇文章主要介紹了Android之使用Android-query框架開發(fā)實(shí)戰(zhàn)(一)的相關(guān)資料,需要的朋友可以參考下2015-10-10
Android同步屏障機(jī)制sync barrier實(shí)例應(yīng)用詳解
這篇文章主要介紹了Android同步屏障機(jī)制sync barrier實(shí)例應(yīng)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02
Android利用ZXing掃描二維碼的實(shí)例代碼解析
這篇文章主要介紹了Android利用ZXing掃描二維碼的實(shí)例解析,代碼簡(jiǎn)單易懂,非常不錯(cuò),需要的朋友可以參考下2016-12-12
Android實(shí)現(xiàn)ListView數(shù)據(jù)動(dòng)態(tài)加載的方法
這篇文章主要介紹了Android實(shí)現(xiàn)ListView數(shù)據(jù)動(dòng)態(tài)加載的方法,通過(guò)ListView控件綁定setOnScrollListener方法簡(jiǎn)單實(shí)現(xiàn)動(dòng)態(tài)加載數(shù)據(jù)的功能,需要的朋友可以參考下2016-01-01

