android 处理鼠标滚轮事件 【转】
android處理鼠標(biāo)滾輪事件,并不是如下函數(shù):
?
1) ?public boolean onKeyDown(int keyCode, KeyEvent event) ?
2) ? ??public boolean dispatchKeyEvent(KeyEvent event)?
3) ? ??public boolean onTouchEvent(MotionEvent event)
?
而是如下函數(shù)
?
publicboolean onGenericMotionEvent(MotionEvent event);
?
所有View和Activity都可重寫(xiě)該函數(shù),來(lái)自己處理滾輪事件,
如下代碼:
/**
* Implement this method to handle generic motion events.
* 實(shí)現(xiàn)該方法來(lái)處理一般的MotionEvent;
* 一般的motion events 描述,操縱桿的動(dòng)作,鼠標(biāo)honver、滾輪等
*
* @param event The generic motion event being processed.
* @return True if the event was handled, false otherwise.
*/
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
//The input source is a pointing device associated with a display.
//輸入源為可顯示的指針設(shè)備,如:mouse pointing device(鼠標(biāo)指針),stylus pointing device(尖筆設(shè)備)
if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
switch (event.getAction()) {
// process the scroll wheel movement...處理滾輪事件
case MotionEvent.ACTION_SCROLL:
//獲得垂直坐標(biāo)上的滾動(dòng)方向,也就是滾輪向下滾
if( event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0.0f){
Log.i("fortest::onGenericMotionEvent", "down" );
}
//獲得垂直坐標(biāo)上的滾動(dòng)方向,也就是滾輪向上滾
else{
Log.i("fortest::onGenericMotionEvent", "up" );
}
return true;
}
}
return super.onGenericMotionEvent(event);
}
轉(zhuǎn)載請(qǐng)注明出處,jiese1990
轉(zhuǎn)載于:https://www.cnblogs.com/luob/p/4757421.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的android 处理鼠标滚轮事件 【转】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mongodb地理空间索引原理阅读摘要
- 下一篇: 接口安全--签名验证