【问题标题】:i am getting "reacting on signal 3" on touch event我在触摸事件中得到“对信号 3 做出反应”
【发布时间】:2013-05-15 17:51:08
【问题描述】:

我需要制作一个 ImageView 来模拟从左到右滑动时的 3D 旋转,所以我使用这个布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:padding="@dimen/global_default_padding"
    android:background="@color/main_background"
    android:id="@+id/rotateImage_background">

        <Button         android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="BACK"
                        android:id="@+id/rotateImage_button_back"/>

        <ImageView      android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"

                        android:id="@+id/rotateImage_image_rotator"
                        android:src="@drawable/hardware_rotate"
                        />

</LinearLayout>

这是我的代码:

@Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_MOVE){

            if(event.getHistorySize() == 0){

            }
            else{
                if(mPreRotationCounter < 10){
                    mPreRotationCounter++;
                }
                else{
                    mPreRotationCounter = 0;
                    Log.e("Rotate", "Should rotate, lol");

                    if(event.getX() > event.getHistoricalX(event.getHistorySize() -1)){
                        updateRotation(true);
                    }
                    else{
                        updateRotation(false);
                    }
                }
            }
        }
        else if(event.getAction() == MotionEvent.ACTION_DOWN){

        }
        else if(event.getAction() == MotionEvent.ACTION_DOWN){

        }

        return true;
    }

private void updateRotation(boolean direction){
        if(direction){
            if(mCurrentImageLevel < 7){
                mCurrentImageLevel++;
            }
            else{
                mCurrentImageLevel = 0;
            }
        }
        else{
            if(mCurrentImageLevel > 0){
                mCurrentImageLevel--;
            }
            else{
                mCurrentImageLevel = 7;
            }
        }
        mRotator.setImageLevel(mCurrentImageLevel);

    }

但是当我尝试它时,我的 logCat 中出现了大量“对信号 3 做出反应”,它没有应有的敏感,但是当我调试时,它工作正常。 有趣的是,我在 android 2.1 上测试它时没有问题,但是当我在 4.0.3 或更高版本上测试它时,我就遇到了这个问题。

【问题讨论】:

    标签: android imageview logcat touch-event ontouchlistener


    【解决方案1】:

    event.getHistorySize() 等于 0 时,您什么也不做。由于您使用的是 -1 索引,因此当 event.getHistorySize() 为 0 时会出错。试试这个:

    if(event.getHistorySize() == 0) {
        return true;            
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 2014-11-13
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      相关资源
      最近更新 更多