GVKun编程网logo

如何为整个屏幕设置OnTouchListener?(屏幕设置overdrive)

27

本文将分享如何为整个屏幕设置OnTouchListener?的详细内容,并且还将对屏幕设置overdrive进行详尽解释,此外,我们还将为大家带来关于AndroidonGestureListener和

本文将分享如何为整个屏幕设置OnTouchListener?的详细内容,并且还将对屏幕设置overdrive进行详尽解释,此外,我们还将为大家带来关于Android onGestureListener和onTouchListner、android ontouchListener、Android OnTouchListener 触屏事件接口、Android OnTouchListener导致不稳定的拖动的相关知识,希望对你有所帮助。

本文目录一览:

如何为整个屏幕设置OnTouchListener?(屏幕设置overdrive)

如何为整个屏幕设置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 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

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 触屏事件接口

OSC 请你来轰趴啦!1028 苏州源创会,一起寻宝 AI 时代
OnTouchListener 接口是用来处理手机屏幕事件的监听接口,当为 View 的范围内触摸按下、抬起或滑动等动作时都会触发该事件。该接口中的监听方法签名如下。 Java 代码: public boolean onTouch (View v, MotionEvent event) 参数 v:参数 v 同样为事件源对象。 参数 event:参数 event 为事件封装类的对象,其中封装了触发事件的详细信息,同样包括事件的类型、触发时间等信息。 节中介绍了一个在屏幕中拖动矩形移动的案例,本节将继续采用该案例的思路,通过监听接口的方式实现在屏幕上拖动按钮移动的案例。开发步骤如下。 创建一个名为 Sample 的 Android 项目。 准备字符串资源,打开 strings.xml 文件,用下列代码替换原有代码。 Java 代码: <?xml version="1.0" encoding="utf-8"?> <!-- XML 的版本及编码方式 --> <resources> <string name="hello">Hello World, Sample</string> <!-- 定义 hello 字符串 --> <string name="app_name">Sample</string> <!-- 定义 app_name 字符串 --> <string name="location"> 位置 </string> <!-- 定义 location 字符串 --> </resources> 说明:与前面介绍的案例相同,对程序中用到的字符串资源进行定义。 开发布局文件。打开 res/layout 目录下的 main.xml,用下列代码替换其原有代码。 Java 代码: <?xml version="1.0" encoding="utf-8"?> <!-- XML 的版本及编码方式 --> <AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android=" http://schemas.android.com/apk/res/android"> <!-- XML 的版本及编码方式 --> <Button android:layout_y="123dip"android:layout_x="106dip"android:text="@string/location"android:layout_height="wrap_content"android:id="@+id/Button01"android:layout_width="wrap_content"/> <!-- XML 的版本及编码方式 --> </AbsoluteLayout> 说明:该布局文件非常简单,只是在一个绝对布局中添加一个按钮控件即可,需要注意的是应该为该按钮指定 ID,以便在 Java 代码中可以得到该按钮的引用。 接下来开始开发主要的逻辑代码。编写 Sample.java 文件,其代码如下所示。 Java 代码: package wyf.ytl; // 声明所在包 import android.app.Activity; // 引入相关类 // 该处省略了部分类的引入代码,读者可以自行查阅随书光盘中的源代码 \ import android.widget.Button; // 引入相关类 public class Sample extends Activity {final static int WRAP_CONTENT=-2; // 表示 WRAP_CONTENT 的常量 final static int X_MODIFY=4; // 在非全屏模式下 X 坐标的修正值 final static int Y_MODIFY=52; // 在非全屏模式下 Y 坐标的修正值 int xSpan; // 在触控笔点击按钮的情况下相对于按钮自己坐标系的 int ySpan; //X,Y 位置 public void onCreate (Bundle savedInstanceState) { // 重写的 onCreate 方法 super.onCreate (savedInstanceState); setContentView (R.layout.main); // 设置当前的用户界面 Button bok=(Button) this.findViewById (R.id.Button01); // 得到按钮的引用 bok.setOnTouchListener ( // 添加监听 new OnTouchListener (){ // 创建监听类 public boolean onTouch (View view, MotionEventevent) { // 重写的监听方法 switch (event.getAction ()){ // 监听事件 case MotionEvent.ACTION_DOWN: // 触控笔按下 xSpan=(int) event.getX (); // 得到 X 坐标 ySpan=(int) event.getY (); // 得到 Y 坐标 break; case MotionEvent.ACTION_MOVE: // 触控笔移动 Button bok=(Button) findViewById (R.id.Button01); // 让按钮随着触控笔的移动一起移动 ViewGroup.LayoutParams lp= new AbsoluteLayout.LayoutParams ( WRAP_CONTENT, WRAP_CONTENT,(int) event.getRawX ()-xSpan-X_MODIFY,(int) event.getRawY ()-ySpan-Y_MODIFY ) ; bok.setLayoutParams (lp); // 设置按钮的坐标 break; } return true; } } ); } public boolean onKeyDown (int keyCode, KeyEvent event){ // 键盘键按下的方法 Button bok=(Button) this.findViewById (R.id.Button01); // 得到按钮的引用 bok.setText (keyCode+"Down"); // 设置按钮的文字 return true; } public boolean onKeyUp (int keyCode,KeyEvent event){ // 键盘键抬起的方法 Button bok=(Button) this.findViewById (R.id.Button01); // 得到按钮的引用 bok.setText (keyCode+"Up"); // 设置按钮的文字 return true; } public boolean onTouchEvent (MotionEventevent){ // 让按钮随触控笔的移动一起移动 Button bok=(Button) this.findViewById (R.id.Button01); // 得到按钮引用 ViewGroup.LayoutParams lp= new AbsoluteLayout.LayoutParams ( // 创建 LayoutParams WRAP_CONTENT, WRAP_CONTENT, (int) event.getRawX ()-xSpan-X_MODIFY, //X 坐标 (int) event.getRawY ()-ySpan-Y_MODIFY //Y 坐标 ) ; bok.setLayoutParams (lp); return true; } } 第 6~10 行声明了程序中需要的一些变量。 第 11~40 行重写了 Activity 中的 onCreate 方法,在方法中设置当前的用户界面,然后得到按钮的引用并为其注册监听。第 16~38 行创建监听器类并重写 onTouch 方法,然后根据事件的类型执行不同的操作。 第 41~45 行重写了 onKeyDown 回调方法,在该方法中得到按钮的引用并设置按钮上的文字,第 46~50 行重写了 onKeyUp 回调方法,同样也是设置按钮上的文字。 第 51~61 行重写了 onTouchEvent 回调方法,用来处理屏幕事件的监听方法,在方法中得到按钮的引用,然后设置按钮的坐标。

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导致不稳定的拖动的相关知识,请在本站进行查询。

本文标签: