在Android“视图"上进行DoubleTap检测成分 [英] DoubleTap detection on Android "View" component
问题描述
我需要在这里实现如下 1)在PNG图像上叠加一个透明的点";用户点击的位置. 2)当用户长时间按住触摸时,叠加一个透明的大圆圈". 3)在Doble中,单击清除屏幕"
What I need to achieve here is as following 1) superimpose a transperent "dot" on the PNG image ; where user has clicked. 2) superimpose a transperent "Big circle", when user is holding touch for long. 3) upon Doble click "Clear the screen"
主要是我将"onTouchEvent"用于单击事件检测...,将"geastureDetector"用于doubleTap检测...但未获得期望的结果.
Primarily I am using "onTouchEvent" for click-event detection... and "geastureDetector" for doubleTap detection... but not getting the desired result.
这些是我正在尝试的2种实现
These are the 2 implementations I am trying out
第一种方法效果很好...但是生成的点击事件存在一定的偏差...我的意思是说,捕获的点击事件在Y方向上始终偏移约50个像素....我无法弄清楚为什么会发生这种情况.
This first approach works fine ... But the click event generated is with some deviation... I mean to say the click event captured is always Offset by around 50 pixels in Y direction.... I am not able to figure out why this should happen..
class Tileview extends Activity implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener {
onCreate(){
//add the View Thing here.
}
@Override
public void onLongPress(MotionEvent e) {
// "longPress Detected"
}
@Override
public boolean onDoubleTap(MotionEvent e) {
// "longPress Detected"
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getActionMasked();
switch(action){
case MotionEvent.ACTION_DOWN:
mClickX = event.getX();
mClickY = event.getY();
}
myTileView.invalidate();
return mGestureDetector.onTouchEvent(event);
}
private class MyTileView extends View{
protected void onDraw(Canvas canvas){
// Canvas & Paint stuff - for translucent cicle
c.drawCircle(mClickX, mClickY, 75.0f, p);
}
}
另一种方法是在适当的位置适当地叠加图像;但是,当我在视图"而不是Activity中添加DoubleTap实现时,doubleTap/geastureListeners将永远不会触发.. 在那种情况下,我所做的唯一更改是获取了onTouchEvent()-并在"view"类中进行了相应的实现....
The other approach is able to superimpose the image propely at proper location; but when I add the DoubleTap implementation in the "View" instead of Activity .. the doubleTap / geastureListeners would never fire .... Only change I did in that case was to get the onTouchEvent() - and corrsponding implementation in "view" class....
class TileActivity extends Activity {
OnCreate()
{
// Bla Bla Bla
}
private class TileView extends View implements OnDoubleTapListener, OnGestureListener{
@override
onTouchEvent(){
// get co-ordinates here from MotionEvent
}
@Override
public boolean onDoubleTap(MotionEvent e) {
// THIS WON'T EVEN GET FIRED
// In this case the doubleTap detection is not working
clearEntireScreen = true;
return false;
}
}
}
推荐答案
现在已解决...! 解决该问题的最佳方法似乎是创建gestureDetector类并实现"SimpleOnGestureListener"..
It is RESOLVED now...!! Seems the best way to tackle the issue was to create gestureDetector class and implement "SimpleOnGestureListener"..
我不得不稍微调整一下实现...但是最基本的代码与这篇博文中的解释非常相似... 在网格布局上翻转手势检测
I had to tweak the implementation a little bit... but the skeleton code is much more similar to what is explained here in this post... Fling gesture detection on grid layout
我必须重写LongPress/onDoubleTap/onSigleTapUp/onSingletap确认的方法才能获得所需的结果...
I had to override LongPress / onDoubleTap / onSigleTapUp / onSingletapconfirmed methods to achieve the desired results...
感谢您的帮助... !!
Thanks for the help... !!
这篇关于在Android“视图"上进行DoubleTap检测成分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!