Android GestureDetector手勢(shì)滑動(dòng)使用實(shí)例講解
Gesture在 ViewGroup中使用
GestureDetector類(lèi)可以讓我們快速的處理手勢(shì)事件,如點(diǎn)擊,滑動(dòng)等。
使用GestureDetector分三步:
1. 定義GestureDetector類(lèi)
2. 初始化手勢(shì)類(lèi),同時(shí)設(shè)置手勢(shì)監(jiān)聽(tīng)
3. 將touch事件交給gesture處理
先來(lái)了解一下如何使用,后面會(huì)有示例:
package com.example.y2222.myview;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.LinearLayout;
/**
* Created by raise.yang on 2016/06/29.
*/
public class GestureDemoView extends LinearLayout {
//1,定義GestureDetector類(lèi)
private GestureDetector m_gestureDetector;
public GestureDemoView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public GestureDemoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//設(shè)置為可點(diǎn)擊
setClickable(true);
//2,初始化手勢(shì)類(lèi),同時(shí)設(shè)置手勢(shì)監(jiān)聽(tīng)
m_gestureDetector = new GestureDetector(context, onGestureListener);
//雙擊監(jiān)聽(tīng)-一般很少用到
m_gestureDetector.setOnDoubleTapListener(onDoubleTapListener);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//3,將touch事件交給gesture處理
m_gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
//初始化手勢(shì)監(jiān)聽(tīng)對(duì)象,使用GestureDetector.OnGestureListener的實(shí)現(xiàn)抽象類(lèi),因?yàn)閷?shí)際開(kāi)發(fā)中好多方法用不上
private final GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d("GestureDemoView", "onSingleTapUp() ");
return super.onSingleTapUp(e);
}
@Override
public void onLongPress(MotionEvent e) {
Log.d("GestureDemoView", "onLongPress() ");
super.onLongPress(e);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d("GestureDemoView", "onScroll() distanceX = " + distanceX);
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d("GestureDemoView", "onFling() velocityX = " + velocityX);
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public void onShowPress(MotionEvent e) {
Log.d("GestureDemoView", "onShowPress() ");
super.onShowPress(e);
}
@Override
public boolean onDown(MotionEvent e) {
Log.d("GestureDemoView", "onDown() ");
return super.onDown(e);
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("GestureDemoView", "onDoubleTap() ");
return super.onDoubleTap(e);
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
Log.d("GestureDemoView", "onDoubleTapEvent() ");
return super.onDoubleTapEvent(e);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d("GestureDemoView", "onSingleTapConfirmed() ");
return super.onSingleTapConfirmed(e);
}
@Override
public boolean onContextClick(MotionEvent e) {
Log.d("GestureDemoView", "onContextClick() ");
return super.onContextClick(e);
}
};
private final GestureDetector.OnDoubleTapListener onDoubleTapListener = new GestureDetector.OnDoubleTapListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d("GestureDemoView", "onSingleTapConfirmed() OnDoubleTapListener");
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("GestureDemoView", "onDoubleTap() OnDoubleTapListener");
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
Log.d("GestureDemoView", "onDoubleTapEvent() OnDoubleTapListener");
return false;
}
};
}
注意:setClickable(true);一定要加,不然只會(huì)收到下例3個(gè)事件,被這個(gè)整了好長(zhǎng)時(shí)間才找到原因.(⊙﹏⊙)b

對(duì)于單擊,雙擊,拖動(dòng)等事件調(diào)用見(jiàn)下圖:

根據(jù)上圖,每個(gè)方法大致都調(diào)用了,說(shuō)明幾個(gè)容易弄混的回調(diào)方法
1. onScroll()
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
e1:滑動(dòng)事件的起點(diǎn)(也就是說(shuō)onDown()的時(shí)候)
e2:當(dāng)前滑動(dòng)位置點(diǎn)(手指的位置)
distanceX:上次滑動(dòng)(調(diào)用onScroll)到這次滑動(dòng)的X軸的距離px,不是e1點(diǎn)到e2點(diǎn)的X軸的距離
distanceY:上次滑動(dòng)(調(diào)用onScroll)到這次滑動(dòng)的Y軸的距離px,不是e1點(diǎn)到e2點(diǎn)的Y軸的距離
2. onFling()
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
e1:拖動(dòng)動(dòng)事件的起點(diǎn)(也就是說(shuō)onDown()的時(shí)候)
e2:onFling()調(diào)用時(shí),手指的位置
velocityX:X軸上每秒滑動(dòng)像素值
velocityY:Y軸上每秒滑動(dòng)像素值
注意:當(dāng)拖動(dòng)速率velocityX或velocityY超過(guò)ViewConfiguration.getMinimumFlingVelocity()最小拖動(dòng)速率時(shí),才會(huì)調(diào)用onFling(),也就是如果只拖動(dòng)一點(diǎn),或是慢慢的拖動(dòng),是不會(huì)觸發(fā)該方法。
對(duì)應(yīng)源碼:
if ((Math.abs(velocityY) > mMinimumFlingVelocity)
|| (Math.abs(velocityX) > mMinimumFlingVelocity)){
handled = mListener.onFling(mCurrentDownEvent, ev, velocityX, velocityY);
}
實(shí)踐:使用GestureDetector實(shí)現(xiàn)左滑刪除
在很多ListView中都有該效果,現(xiàn)在自己實(shí)現(xiàn)下,順便熟悉GestureDetector的使用。
效果圖:

GestureDemoView.java:
package com.example.y2222.myview;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import com.example.y2222.myapplication.R;
/**
* Created by raise.yang on 2016/06/29.
*/
public class GestureDemoView extends LinearLayout {
//1,定義GestureDetector類(lèi)
private GestureDetector m_gestureDetector;
private int m_max_scrollX;
public GestureDemoView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public GestureDemoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//設(shè)置為可點(diǎn)擊
setClickable(true);
//2,初始化手勢(shì)類(lèi),同時(shí)設(shè)置手勢(shì)監(jiān)聽(tīng)
m_gestureDetector = new GestureDetector(context, onGestureListener);
LayoutInflater.from(context).inflate(R.layout.view_gesture, this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//3,將touch事件交給gesture處理
m_gestureDetector.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_UP) {
// GestureDetector沒(méi)有處理up事件的方法,只能在這里處理了。
int scrollX = getScrollX();
if (scrollX > m_max_scrollX / 2) {
show_right_view();
} else {
hide_right_view();
}
}
return super.onTouchEvent(event);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
//測(cè)量子view的寬高,?不測(cè)量,右側(cè)布局會(huì)不顯示,這里有點(diǎn)疑問(wèn)
measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
if (i == 1) {
m_max_scrollX = getChildAt(i).getMeasuredWidth();
}
}
}
//初始化手勢(shì)監(jiān)聽(tīng)對(duì)象,使用GestureDetector.OnGestureListener的實(shí)現(xiàn)抽象類(lèi),因?yàn)閷?shí)際開(kāi)發(fā)中好多方法用不上
private final GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d("GestureDemoView", "onScroll() distanceX = " + distanceX + " getScrollX = " + getScrollX() + " max_scrollX = " + m_max_scrollX);
int scrollX = getScrollX();
int minScrollX = -scrollX;
int maxScrollY = m_max_scrollX - scrollX;
// 對(duì)滑動(dòng)的距離邊界控制
if (distanceX > maxScrollY) {
distanceX = maxScrollY;
} else if (distanceX < minScrollX) {
distanceX = minScrollX;
}
scrollBy((int) distanceX, 0);
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d("GestureDemoView", "onFling() velocityX = " + velocityX);
if (velocityX < 0) {
//快速向左滑動(dòng)
show_right_view();
} else {
hide_right_view();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
};
private void show_right_view() {
scrollTo(m_max_scrollX, 0);
}
private void hide_right_view() {
scrollTo(0, 0);
}
}
view_gesture.xml
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="左側(cè)布局"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="收藏"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="刪除"/> </LinearLayout> </merge>
xml文件中根標(biāo)簽使用<merge>,可減少一層view樹(shù)嵌套,并且使用getChildCount()能得到我們想要的子view個(gè)數(shù)。
關(guān)于<merge>標(biāo)簽的使用,詳見(jiàn)郭神的blog:http://blog.csdn.net/guolin_blog/article/details/43376527
實(shí)現(xiàn)也很簡(jiǎn)單,在scroll和fling的時(shí)候,得到滑動(dòng)距離或滑動(dòng)速度,再調(diào)用view自己的scrollTo()或scrollBy()滑動(dòng)內(nèi)部元素即可。
從效果圖中,當(dāng)滑動(dòng)到一半松手時(shí),立即滑動(dòng)到最左邊,完全沒(méi)有動(dòng)畫(huà),這樣的體驗(yàn)很差,所以還需優(yōu)化。關(guān)于滑動(dòng)時(shí)增加動(dòng)畫(huà)效果,可以使用Scroller類(lèi)完成,準(zhǔn)備下期補(bǔ)上。
Gesture在 View中使用
和在viewgroup中一樣,在view中,同樣是經(jīng)過(guò)三步來(lái)實(shí)現(xiàn):
1. 定義GestureDetector類(lèi)
2. 初始化手勢(shì)類(lèi),同時(shí)設(shè)置手勢(shì)監(jiān)聽(tīng)
3. 將touch事件交給gesture處理
舉個(gè)荔枝:
做了一個(gè)小球跟隨手指移動(dòng)的效果,先繪制小球,當(dāng)手指放在小球上滑動(dòng)時(shí),會(huì)調(diào)用onScroll(),在這個(gè)方法中,修改圓心的位置進(jìn)行重繪,這樣小球就能移動(dòng)了。
這里有2個(gè)難點(diǎn):
1. 如何判斷手指落在了小球上;
2. 滑動(dòng)到邊界時(shí),不能超過(guò)邊界;
效果圖:

GestureView.java代碼:
package com.example.y2222.myview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by raise.yang on 2016/07/05.
*/
public class GestureView extends View {
private GestureDetector m_gestureDetector;
private Paint m_paint;
//小球的中心點(diǎn)
private float centerX;
private float centerY;
//小球的半徑
private int radius;
//是否touch在小球上
private boolean touch_bool;
public GestureView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public GestureView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// 初始畫(huà)筆
m_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
m_paint.setColor(getResources().getColor(android.R.color.holo_blue_light));
//設(shè)置為可點(diǎn)擊
setClickable(true);
//2,初始化手勢(shì)類(lèi),同時(shí)設(shè)置手勢(shì)監(jiān)聽(tīng)
m_gestureDetector = new GestureDetector(context, onGestureListener);
radius = 50;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//3,將touch事件交給gesture處理
m_gestureDetector.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//判斷手指落在了小球上
if (getDistanceByPoint((int) centerX, (int) centerY, (int) event.getX(), (int) event.getY()) < radius) {
touch_bool = true;
} else {
touch_bool = false;
}
}
return super.onTouchEvent(event);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// 默認(rèn)圓心在中心點(diǎn)
if (w > 0) {
centerX = w / 2;
}
if (h > 0) {
centerY = h / 2;
}
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(centerX, centerY, radius, m_paint);
}
GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (touch_bool) {
centerY -= distanceY;
centerX -= distanceX;
//處理邊界問(wèn)題
if (centerX < radius) {
centerX = radius;
} else if (centerX > getWidth() - radius) {
centerX = getWidth() - radius;
}
if (centerY < radius) {
centerY = radius;
} else if (centerY > getHeight() - radius) {
centerY = getHeight() - radius;
}
//修改圓心后,通知重繪
postInvalidate();
}
return true;
}
};
/**
* 計(jì)算兩點(diǎn)間的距離
*/
private int getDistanceByPoint(int x1, int y1, int x2, int y2) {
double temp = Math.abs((x2 - x1) * (x2 - x1) - (y2 - y1) * (y2 - y1));
return (int) Math.sqrt(temp);
}
}
在處理問(wèn)題1時(shí),我設(shè)置了一個(gè)boolean值,在用戶(hù)觸摸的時(shí)候去判斷,當(dāng)前點(diǎn)和圓心點(diǎn)的距離是否小于半徑,若小于,說(shuō)明在圓內(nèi)。這樣在滑動(dòng)的時(shí)候,就去判斷一下,是否需要滑動(dòng)小球。
控制邊界,其實(shí)就是控制圓心點(diǎn)的坐標(biāo),只要保證落在(radius,radius),(getWidth()-radius,getHeight()-radius)兩點(diǎn)矩形中即可。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 高仿微信朋友圈動(dòng)態(tài)支持雙擊手勢(shì)放大并滑動(dòng)查看圖片效果
- Android實(shí)現(xiàn)圖片自動(dòng)輪播并且支持手勢(shì)左右無(wú)限滑動(dòng)
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)和簡(jiǎn)單動(dòng)畫(huà)效果
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸放大縮小圖片效果
- Android手勢(shì)滑動(dòng)實(shí)現(xiàn)ImageView縮放圖片大小
- android中view手勢(shì)滑動(dòng)沖突的解決方法
- Android自定義View實(shí)現(xiàn)隨手勢(shì)滑動(dòng)控件
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸縮放平移圖片效果
- Android實(shí)現(xiàn)手勢(shì)滑動(dòng)多點(diǎn)觸摸縮放平移圖片效果(二)
- Android獲取觸摸手勢(shì)實(shí)現(xiàn)左右滑動(dòng)
相關(guān)文章
Android編程判斷網(wǎng)絡(luò)連接是否可用的方法
這篇文章主要介紹了Android編程判斷網(wǎng)絡(luò)連接是否可用的方法,實(shí)例分析了Android判定網(wǎng)絡(luò)連接的相關(guān)技巧與實(shí)現(xiàn)步驟,需要的朋友可以參考下2015-12-12
Android編程使用LinearLayout和PullRefreshView實(shí)現(xiàn)上下翻頁(yè)功能的方法
這篇文章主要介紹了Android編程使用LinearLayout和PullRefreshView實(shí)現(xiàn)上下翻頁(yè)功能的方法,涉及Android界面布局與邏輯處理相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Android中WebView加載的網(wǎng)頁(yè)被放大的解決辦法
這篇文章主要介紹了Android中WebView加載的網(wǎng)頁(yè)被放大的問(wèn)題的解決辦法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-12-12
Android項(xiàng)目實(shí)戰(zhàn)(二十八):使用Zxing實(shí)現(xiàn)二維碼及優(yōu)化實(shí)例
這篇文章主要介紹了Android項(xiàng)目實(shí)戰(zhàn)(二十八):使用Zxing實(shí)現(xiàn)二維碼及優(yōu)化實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
Android RecyclerView滾動(dòng)定位
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView滾動(dòng)定位的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android使用Recyclerview實(shí)現(xiàn)圖片水平自動(dòng)循環(huán)滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android使用Recyclerview實(shí)現(xiàn)圖片水平自動(dòng)循環(huán)滾動(dòng)效果,實(shí)現(xiàn)精彩的跑馬燈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android實(shí)現(xiàn)極簡(jiǎn)打開(kāi)攝像頭
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)極簡(jiǎn)打開(kāi)攝像頭,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Android碎片fragment實(shí)現(xiàn)靜態(tài)加載的實(shí)例代碼
這篇文章主要介紹了Android碎片fragment實(shí)現(xiàn)靜態(tài)加載的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
Android System fastboot 介紹和使用教程
Fastboot是Android快速升級(jí)的一種方法,Fastboot的協(xié)議fastboot_protocol.txt在源碼目錄./bootable/bootloader/legacy下可以找到,本文給大家介紹Android System fastboot 介紹和使用教程,感興趣的朋友一起看看吧2024-01-01

