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

程序運行,手指在屏幕上從左往右或者從右往左滑動超過一定距離,就會吐司輸出滑動方向和距離。
1.activity_main.xml頁面放置一個ImageView控件。
activity_main.xml頁面:
<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頁面實現(xiàn)滑動方法。
MainActivity.java頁面:
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ù)為剛開始事件,第二個參數(shù)為結(jié)束事件*/
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if(e1.getX()-e2.getX()>50){
Toast.makeText(MainActivity.this,"從右往左滑動"+(e1.getX()-e2.getX()),Toast.LENGTH_LONG).show();
}else if(e2.getX()-e1.getX()>50){
Toast.makeText(MainActivity.this,"從左往右滑動"+(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,否則手勢識別無法正確工作
ivShow.setOnTouchListener(new OnTouchListener() {
/*可以捕獲到觸摸屏幕發(fā)生的Event事件*/
@Override
public boolean onTouch(View arg0, MotionEvent event) {
gestureDetector.onTouchEvent(event);//轉(zhuǎn)發(fā)
return false;
}
});
}
}
3.程序較簡單,運行就可以顯示目標(biāo)效果了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android GestureDetector用戶手勢檢測實例講解
- android使用gesturedetector手勢識別示例分享
- Android GestureDetector手勢滑動使用實例講解
- Android手勢識別器GestureDetector使用詳解
- Android自定義viewgroup可滾動布局 GestureDetector手勢監(jiān)聽(5)
- Android自定義GestureDetector實現(xiàn)手勢ImageView
- Android編程使用GestureDetector實現(xiàn)簡單手勢監(jiān)聽與處理的方法
- Android觸摸及手勢操作GestureDetector
- Android使用手勢監(jiān)聽器GestureDetector遇到的不響應(yīng)問題
- Android如何使用GestureDetector進(jìn)行手勢檢測詳解
相關(guān)文章
Android adb logcat 命令查看日志詳細(xì)介紹
這篇文章主要介紹了Android adb logcat 命令詳細(xì)介紹的相關(guān)資料,這里對logcat 命令進(jìn)行了詳細(xì)介紹,并介紹了過濾日志輸出的知識,需要的朋友可以參考下2016-12-12
基于android示例程序(bitmapfun) 高效加載圖片讓人無語地方
嘗試了使用git上的一個開源項目afinal(bitmapfun的封裝版)來加載圖片,但是在測試的時候發(fā)現(xiàn)了一個問題,新的圖片加載器(bitmapfun)比之前用的ImageDownloader要慢很多,特別是在網(wǎng)絡(luò)狀況不好的時候,那簡直是太讓人無語了2013-04-04
android手機(jī)獲取gps和基站的經(jīng)緯度地址實現(xiàn)代碼
android手機(jī)如何獲取gps和基站的經(jīng)緯度地址,疑問,于是網(wǎng)上搜集整理一些,拿出來和大家分享下,希望可以幫助你們2012-12-12
在Android中使用SQLite數(shù)據(jù)庫及其操作詳解
在?Android?開發(fā)中,使用?SQLite?數(shù)據(jù)庫是一種常見的持久化數(shù)據(jù)存儲方式,本文將通過代碼示例詳細(xì)講解如何在?Android?中創(chuàng)建數(shù)據(jù)庫表、插入數(shù)據(jù)、執(zhí)行查詢操作以及驗證查詢結(jié)果,需要的朋友可以參考下2024-08-08
Android源碼學(xué)習(xí)之觀察者模式應(yīng)用及優(yōu)點介紹
定義對象間一種一對多的依賴關(guān)系,使得當(dāng)一個對象改變狀態(tài),則所有依賴于它的對象都會得到通知并被自動更新等等,需要了解的朋友可以參考下2013-01-01
Android中Fragment與Activity的生命周期對比
這篇文章主要介紹了Android中Fragment與Activity的生命周期對比,Fragment是在Activity的基礎(chǔ)之上進(jìn)行設(shè)計的,比Activity多出幾個控制生命周期的回調(diào)函數(shù),需要的朋友可以參考下2016-02-02
Android自定義viewgroup 使用adapter適配數(shù)據(jù)(6)
這篇文章主要為大家詳細(xì)介紹了Android自定義viewgroup,使用adapter適配數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12

