本文将分享如何为整个屏幕设置OnTouchListener?的详细内容,并且还将对屏幕设置overdrive进行详尽解释,此外,我们还将为大家带来关于AndroidonGestureListener和
本文将分享如何为整个屏幕设置OnTouchListener?的详细内容,并且还将对屏幕设置overdrive进行详尽解释,此外,我们还将为大家带来关于Android onGestureListener和onTouchListner、android ontouchListener、Android OnTouchListener 触屏事件接口、Android OnTouchListener导致不稳定的拖动的相关知识,希望对你有所帮助。
本文目录一览:- 如何为整个屏幕设置OnTouchListener?(屏幕设置overdrive)
- Android onGestureListener和onTouchListner
- android ontouchListener
- Android OnTouchListener 触屏事件接口
- Android OnTouchListener导致不稳定的拖动
如何为整个屏幕设置OnTouchListener?(屏幕设置overdrive)
请看下面的代码。我只发布代码的重要部分。
activity_game.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fullView" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".Game" > <TextView android:id="@+id/numberOfQuestionsLeft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="20dp" android:text="TextView" /></RelativeLayout>
Game.java
public class Game extends Activity { private View fullView;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); fullView = (View)findViewById(R.id.fullView); fullView.setOnTouchListener(imageViewSwiped); } OnTouchListener imageViewSwiped = new OnSwipeTouchListener() { public boolean onSwipeRight() { //code removed } public boolean onSwipeLeft() { //code removed return true; } public boolean onSwipeBottom() { //code removed return true; } };}
OnSwipTouchListener.java
注意-我不拥有此类代码。它由另一位SO成员编写。
package game.Game; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; public class OnSwipeTouchListener implements OnTouchListener { private final GestureDetector gestureDetector = new GestureDetector(new GestureListener()); public boolean onTouch(final View v, final MotionEvent event) { return gestureDetector.onTouchEvent(event); } private final class GestureListener extends SimpleOnGestureListener { private static final int SWIPE_THRESHOLD = 100; private static final int SWIPE_VELOCITY_THRESHOLD = 100; @Override public boolean onDown(MotionEvent e) { return super.onDown(e); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { boolean result = false; try { float diffY = e2.getY() - e1.getY(); float diffX = e2.getX() - e1.getX(); if (Math.abs(diffX) > Math.abs(diffY)) { if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { if (diffX > 0) { result = onSwipeRight(); } else { result = onSwipeLeft(); } } } else { if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { if (diffY > 0) { result = onSwipeBottom(); } else { result = onSwipeTop(); } } } } catch (Exception exception) { exception.printStackTrace(); } return result; } } public boolean onSwipeRight() { return false; } public boolean onSwipeLeft() { return false; } public boolean onSwipeTop() { return false; } public boolean onSwipeBottom() { return false; } }
现在,我正在尝试使“整个活动”处于启用状态。请查看Game.java
以了解我的操作方式。但是问题是,它对任何触摸事件都没有反应!代码没有错,OnSwipTouchListener.java
因为我通过将侦听器设置为来使用了touch事件imageview
。
我试图使整个活动处于启用状态时肯定存在问题。通过说“启用整个活动触摸”,我需要在整个屏幕上滑动,而不是将监听器添加到imageview
。
我在这里做错了什么?
答案1
小编典典根据 发问者的 要求,解决方案是在具有相对布局的touchlistener-doesnt-
work上解决该 问题
的方法。)
Android onGestureListener和onTouchListner
我在android工作.我有一些功能可以通过以下方法完成: –
1. MotionEvent.ACTION_DOWN
2. MotionEvent.ACTION_UP
3. MotionEvent.ACTION_MOVE
我也想对那张图片做一个fling().
对于上面的要求我在我的应用程序中实现了onGestureListener和onTouchListener,但这些都无法正常工作. onTouchListener正在运行,但onGestureListener无法正常工作.当我删除onTouchListner代码然后onGestureListener正常工作.
所以请建议我该怎么做.我想在我的应用程序中实现这四种方法.
1. MotionEvent.ACTION_DOWN
2. MotionEvent.ACTION_UP
3. MotionEvent.ACTION_MOVE
4. onFling
解决方法:
您可以通过从活动中实现OnGestureListener来覆盖以下方法来实现此目的
// For touch
public boolean onSingleTapUp(MotionEvent event) { return false; }
// For Fling
public boolean onFling(MotionEvent e1, MotionEvent e2, float veLocityX,
float veLocityY) {
return false;
}
希望这可以帮助.
编辑1:详细说明:
1 GT;从您的活动中实现OnGestureListener
public class MyActivity implements OnGestureListener
2 – ;创建一个GestureDetector实例:
private GestureDetector gestureScanner;
在onCreate:
// Avoid a deprecated constructor
// (http://developer.android.com/reference/android/view/GestureDetector.html)
gestureScanner = new GestureDetector(this, this);
3 GT;覆盖以下方法:
@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureScanner.onTouchEvent(event);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float veLocityX,
float veLocityY) {
/* on scroll to the next page in story */
if (e2.getX() - e1.getX() > SWIPE_MIN_disTANCE
&& Math.abs(veLocityX) > SWIPE_THRESHOLD_VELociTY) {
// ...
}
/* on scroll to the prevIoUs page in story */
else if (e1.getX() - e2.getX() > SWIPE_MIN_disTANCE
&& Math.abs(veLocityX) > SWIPE_THRESHOLD_VELociTY) {
// ...
}
return false;
}
@Override
public boolean onSingleTapUp(MotionEvent event) {
return false;
}
编辑2:处理移动
覆盖onScroll方法看一下细节here
android ontouchListener
为什么我在监听了ontouchListener的时候,总是执行MotionEvent.ACTION_DOWN,找了好久,终于找到原因了。其中 在onTouch 代码中 如果返回 false 就不能捕捉到ACTION_MOVE 事件。
对于onTouchEvent 中onTouch返回值
1 、如果return false 说明还没有消费onTouch事件,在执行onTouch里代码后,onTouch事件并没有结束。
2、如果return true 说明消费了onTouch事件 onTouch事件结束了
但在实际操作中 除了ACTION_DOWN事件以外,其余的事件只有返回true的那个方法才能捕捉到。所以 返回false的时候只能捕捉到每次的第一个DOWN事件 后面的MOVE 和UP事件就捕捉不到了。
Android OnTouchListener 触屏事件接口

Android OnTouchListener导致不稳定的拖动
我有一个按钮(在代码中变量theButton),并希望能够在其父视图中拖动它.
这是它的OnTouchListener:
OnTouchListener touchBListener = new View.OnTouchListener()
{
public float offsetX;
public float offsetY;
@Override
public boolean onTouch(View v, MotionEvent event)
{
int theAction = event.getAction();
switch (theAction)
{
case MotionEvent.ACTION_DOWN:
// Button down
offsetX = theButton.getX() - event.getX();
offsetY = theButton.getY() - event.getY();
break;
case MotionEvent.ACTION_MOVE:
// Button moved
float newX = event.getX() + offsetX;
float newY = event.getY() + offsetY;
v.setX(newX);
v.setY(newY);
break;
case MotionEvent.ACTION_UP:
// Button up
break;
default:
break;
}
return true;
}
};
theButton.setonTouchListener(touchBListener);
当我拖动按钮时,它会在拖动的大致方向上移动,但它会在拖动之前的“当前”点和一个点之间来回跳跃.由于某种原因,当阻力向右上方移动时,跳跃会更大.
使用OnTouchListener是否可以“平滑”拖动,或者我是否需要使用OnDragListener?
解决方法:
我在另一篇文章中找到了答案 – 当我将event.getX()和event.getY()更改为event.getRawX()和event.getRawY()时,拖动是平滑的.
今天的关于如何为整个屏幕设置OnTouchListener?和屏幕设置overdrive的分享已经结束,谢谢您的关注,如果想了解更多关于Android onGestureListener和onTouchListner、android ontouchListener、Android OnTouchListener 触屏事件接口、Android OnTouchListener导致不稳定的拖动的相关知识,请在本站进行查询。
本文标签: